socket小实例

服务端

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading;

namespace SocketDemo {

    class Program     {         static Socket serverSocket;         static Socket clientSocket;         static Thread thread;         static void Main(string[] args)         {             //将网络端点表示为IP地址和端口 用于socket侦听时绑定             IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 3001);             //使用指定的地址族、套接字类型和协议初始化socket类的新实例             serverSocket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);             //将所创建的Socket与IPEndPoint绑定             serverSocket.Bind(ipep);             //设置套接字为收听模式 定队列中最多可容纳的等待接受的传入链接数为10             serverSocket.Listen(10);             //在Socket上接收接入的连接             while (true)             {                 //serverSocket.Accept()接收客户端Socket的连接请求 当程序运行到serverSocket.Accept()时会等待直到有客户端Socket发起连接请求时 获取客户端Socket

                clientSocket = serverSocket.Accept();                 thread = new Thread(new ThreadStart(Program.doWork));                 thread.Start();             }             Console.Read();         }         private static void doWork()         {

            Socket clinet = clientSocket;             //客户端信息             IPEndPoint ipEndPoint = (IPEndPoint)clinet.RemoteEndPoint;             String address = ipEndPoint.Address.ToString();             String port = ipEndPoint.Port.ToString();             Console.WriteLine(address + ":" + port + " 连接过来了");             while (true)             {                 Byte[] inBuffer = new Byte[clinet.ReceiveBufferSize];                 Byte[] outBuffer=new Byte[1024];                 String inBufferStr;                 String outBufferStr;                 try                 {                     //如果接收的消息为空 阻塞 当前循环                     clinet.Receive(inBuffer, clinet.ReceiveBufferSize, SocketFlags.None);                 }                 catch(SocketException)                 {                     clinet.Close();                     return;                 }                 inBufferStr = Encoding.ASCII.GetString(inBuffer);                 Console.WriteLine(address + ":" + port + "说:");                 Console.WriteLine(inBufferStr.Trim());

                outBufferStr = Console.ReadLine().Trim();

                outBuffer = Encoding.ASCII.GetBytes(outBufferStr);                 clinet.Send(outBuffer, outBuffer.Length, SocketFlags.None);                             }

                  }     } }

客户端

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Net;

namespace clientSocket {     class Program     {         static Socket clientSocket;         static void Main(string[] args)         {             try             {                 //将网络端点表示为IP地址和端口,用于socket侦听时绑定                 IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3001);                 clientSocket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);                 //将socket连接到服务器                 clientSocket.Connect(ipep);                 String outBufferStr;                 Byte[] outBuffer = new Byte[1024];                 Byte[] inBuffer = new Byte[1024];                 //发送消息                 outBufferStr = Console.ReadLine().Trim();                 outBuffer = Encoding.ASCII.GetBytes(outBufferStr);                 clientSocket.Send(outBuffer, outBuffer.Length, SocketFlags.None);                 Console.WriteLine("服务器响应:");                 //接收服务器端信息                 clientSocket.Receive(inBuffer, 1024, SocketFlags.None);                 Console.WriteLine(Encoding.ASCII.GetString(inBuffer));                 Console.ReadLine();             }             catch(Exception exp)             {                 Console.WriteLine(exp.Message);                 Console.ReadLine();             }         }     } }

posted @ 2014-01-23 16:06  armyfai  阅读(423)  评论(0编辑  收藏  举报