C# 处理UDP数据的类

  1 using System;
  2 using System.Net;
  3 using System.Net.Sockets;
  4 using System.Runtime.InteropServices;
  5 using System.Threading;
  6 
  7 namespace TestDemo
  8 {
  9     /// <summary>
 10     /// 处理UDP数据的类
 11     /// </summary>
 12     public class UDP
 13     {
 14         /// <summary>
 15         /// UDP连接对象
 16         /// </summary>
 17         private UdpClient udpClient;
 18 
 19         /// <summary>
 20         /// 创建UDP连接对象
 21         /// </summary>
 22         public void CreateUdpClient(string ip = null, int port = 0)
 23         {
 24             // 创建UDP连接
 25             if (ip != null)
 26             {
 27                 IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
 28                 udpClient = new UdpClient(iPEndPoint);
 29             }
 30             else
 31             {
 32                 IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Any, port);
 33                 udpClient = new UdpClient(iPEndPoint);
 34             }
 35             // 判断操作系统
 36             if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
 37             {
 38                 // Windows 相关逻辑
 39                 uint IOC_IN = 0x80000000;
 40                 uint IOC_VENDOR = 0x18000000;
 41                 uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
 42                 udpClient.Client.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);
 43             }
 44             else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
 45             {
 46                 // Linux 相关逻辑
 47             }
 48             // 开启多线程接收
 49             Thread threadReceive = new Thread(ReceiveMessages);
 50             threadReceive.Start();
 51         }
 52 
 53         /// <summary>
 54         /// UDP接收线程
 55         /// </summary>
 56         private void ReceiveMessages()
 57         {
 58             IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);
 59             while (true)
 60             {
 61                 try
 62                 {
 63                     // 接收数据
 64                     byte[] receiveBytes = udpClient.Receive(ref remoteIPEndPoint);
 65                     // IP
 66                     string ip = remoteIPEndPoint.Address.ToString();
 67                     // 端口
 68                     int port = remoteIPEndPoint.Port;
 69                     // 处理数据(新建线程)
 70                     Thread threadReceive = new Thread(new ParameterizedThreadStart(delegate { DealData(ip, port, receiveBytes); }));
 71                     threadReceive.Start();
 72                 }
 73                 catch (Exception ex)
 74                 {
 75                     Log.WriteError(ex);
 76                 }
 77             }
 78         }
 79 
 80         /// <summary>
 81         /// 发送数据
 82         /// </summary>
 83         /// <param name="ip"></param>
 84         /// <param name="port"></param>
 85         /// <param name="data"></param>
 86         public void SendData(string ip, int port, byte[] data)
 87         {
 88             udpClient.Send(data, data.Length, ip, port);
 89         }
 90 
 91         /// <summary>
 92         /// 处理数据
 93         /// </summary>
 94         /// <param name="ip"></param>
 95         /// <param name="port"></param>
 96         /// <param name="data"></param>
 97         private void DealData(string ip, int port, byte[] data)
 98         {
 99 
100         }
101     }
102 }

 

posted @ 2022-09-06 14:35  Mr_Xul  阅读(102)  评论(0编辑  收藏  举报