Socket通信(一)

1、服务器端:

  一般编写服务器端的流程:

先创建Socket-->调用BInd绑定ip地址和端口号---->调用Listen等待客户端连接---->在while死循环中调用Accept接收客户端的连接,并回应消息

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Net;
 7 using System.Net.Sockets;
 8 using System.IO;
 9 
10 namespace Socket_Test
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             Console.WriteLine("你好,这边是服务器端");
17 
18             Socket sockets = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
19             IPAddress ipaddress = IPAddress.Parse("127.0.0.1");
20             IPEndPoint ipendPoint = new IPEndPoint(ipaddress, 1234);
21 
22             sockets.Bind(ipendPoint);           //切记,一定要进行Bind
23             sockets.Listen(100);         //限定监听100个客户端
24 
25             Console.WriteLine("服务器开始监听");
26 
27             while (true)
28             {
29                 Socket connfd = sockets.Accept();        //监听的是客户端的信息
30 
31                 //revc
32                 Console.WriteLine("服务器开始接收信息");
33                 byte[] buffer = new byte[1024];
34                 int connfdLength = connfd.Receive(buffer);          //接收到信息,然后得出信息的长度
35                 string connfdString =System.Text.Encoding.UTF8.GetString(buffer, 0, connfdLength);
36                 Console.WriteLine("接收到信息"+connfdString);
37 
38                 //send:
39 
40                 string sendString = DateTime.Now.ToString();   //把当前的时间进行传递
41                 byte[] sendByte = System.Text.Encoding.Default.GetBytes(sendString);
42             }
43         }
44     }
45 }

 2、客户端:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Net;
 7 using System.Net.Sockets;
 8 
 9 //客户端
10 namespace Socket_Client
11 {
12     class Program
13     {
14 
15         private Socket socket;
16         private string ipAddress = "127.0.0.1";     //IP地址
17         private int port = 1234;                    //端口号
18 
19         private const int buffer_Size = 1024;       //一次的接收大小
20         byte[] readBuffer = new byte[buffer_Size];          //用来接收数据的数组
21 
22 
23         private void Connetion()
24         {
25             socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);           //建立套接字
26 
27             //connect
28             socket.Connect(ipAddress, port);
29 
30             //send:
31             string Send_str = "Unity";
32             byte[] bytes = System.Text.Encoding.Default.GetBytes(Send_str);
33             socket.Send(bytes);
34 
35             //recv:
36             int count = socket.Receive(readBuffer);
37             string Recv_str = System.Text.Encoding.UTF8.GetString(readBuffer, 0, count);
38             Console.WriteLine("客户端接收到信息:"+Recv_str);
39 
40             //close:关闭连接;
41             socket.Close();
42 
43         }
44 
45         static void Main(string[] args)
46         {
47             Program p = new Program();
48             p.Connetion();
49         }
50     }
51 }

猜你喜欢

转载自www.cnblogs.com/zhh19981104/p/9181487.html