通过socket实现UDP 通信小例子

服务端

 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.Sockets;
 7 using System.Net;
 8 using System.Threading;
 9 namespace UDP_Server
10 {
11     class Program
12     {
13         static Socket server;
14         static void Main(string[] args)
15         {
16             server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
17             server.Bind(new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6001));//绑定端口号和IP
18             Console.WriteLine("服务端已经开启");
19             Thread t = new Thread(ReciveMsg);//开启接收消息线程
20             t.Start();
21             Thread t2 = new Thread(sendMsg);//开启发送消息线程
22             t2.Start();
23 
24 
25         }
26         /// <summary>
27         /// 向特定ip的主机的端口发送数据报
28         /// </summary>
29         static void sendMsg()
30         {
31             EndPoint point = new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6000);
32             while (true)
33             {
34                 string msg = Console.ReadLine();
35                 server.SendTo(Encoding.UTF8.GetBytes(msg), point);
36             }
37 
38 
39         }
40         /// <summary>
41         /// 接收发送给本机ip对应端口号的数据报
42         /// </summary>
43         static void ReciveMsg()
44         {
45             while (true)
46             {
47                 EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号
48                 byte[] buffer = new byte[1024];
49                 int length = server.ReceiveFrom(buffer, ref point);//接收数据报
50                 string message = Encoding.UTF8.GetString(buffer,0,length);
51                 Console.WriteLine(point.ToString()+ message);
52 
53             }
54         }
55 
56 
57     }
58 }
View Code

客户端

 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.Threading;
 9 namespace UDP_client
10 {
11     class Program
12     {
13         static Socket client;
14         static void Main(string[] args)
15         {
16             client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
17             client.Bind(new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6000));
18             Thread t = new Thread(sendMsg);
19             t.Start();
20             Thread t2 = new Thread(ReciveMsg);
21             t2.Start();
22             Console.WriteLine("客户端已经开启");
23         }
24         /// <summary>
25         /// 向特定ip的主机的端口发送数据报
26         /// </summary>
27         static void sendMsg()
28         {
29             EndPoint point = new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6001);
30             while(true){
31                 string msg = Console.ReadLine();
32                 client.SendTo(Encoding.UTF8.GetBytes(msg), point);
33             }
34 
35 
36         }
37 
38         /// <summary>
39         /// 接收发送给本机ip对应端口号的数据报
40         /// </summary>
41         static void ReciveMsg()
42         {
43             while (true)
44             {
45                 EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号
46                 byte[] buffer = new byte[1024];
47                 int length = client.ReceiveFrom(buffer, ref point);//接收数据报
48                 string message = Encoding.UTF8.GetString(buffer, 0, length);
49                 Console.WriteLine(point.ToString() + message);
50             }
51         }
52 
53     }
54 }
View Code
原文地址:https://www.cnblogs.com/anyihen/p/12832782.html