首页 > 编程语言 >C# 处理UDP数据的类

C# 处理UDP数据的类

时间:2022-09-06 14:12:53浏览次数:60  
标签:UDP udpClient C# ip public 处理 new port

  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         public static 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         public void ReceiveMessages()
 57         {
 58             IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);
 59             while (true)
 60             {
 61                 try
 62                 {
 63                     // 接收udp
 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 SendCmd(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         public void DealData(string ip, int port, byte[] data)
 98         {
 99 
100         }
101     }
102 }

 

标签:UDP,udpClient,C#,ip,public,处理,new,port
From: https://www.cnblogs.com/smartnn/p/16661551.html

相关文章

  • cmake语法手册及教程
    一,cmake变量引用的方式:前面我们已经提到了,使用${}进行变量的引用。在IF等语句中,是直接使用变量名而不通过${}取值二,cmake自定义变量的方式:主要有隐式定义和显......
  • C# 处理多语言的基础类
    1usingNewtonsoft.Json;2usingNewtonsoft.Json.Linq;3usingSystem.IO;4usingSystem.Text;56namespaceTestDemo7{8///<summary>9......
  • Check if a string is null or empty in XSLT
    多条件查询string.Format("/root/deviceList//item/guid[{0}]",strBuilder.ToString())"/root/deviceList//item/guid[text()=\"h\"ortext()=\"a\"ortext()=\"c\"......
  • 苹果Mac原型设计:Axure RP9如何查看和共享您的原型?
    今天就给大家带来了AxureRP如何查看和共享您的原型的教程,你快速完成操作,快来跟小编看看吧!为您带来Mac上好用的原型设计软件AxureRP9中文正式版,axuremac是专为UX专业......
  • 安装PowerCLI
    1.使用powershell直接安装Install-ModuleVMware.PowerCLI-ScopeCurrentUser2.下载安装包后解压,将模块复制到powershell的模块目录1在官网下载ZIP包:https://devel......
  • 最新资讯|2022年8月29日,IECEE发布电池认证CTL协议DSH1037A!
    2022年8月29日,IECEE发布电池认证CTL协议DSH1037A,涉及标准IEC62133:2002,IEC62133:2012,IEC62133-1:2017,IEC62133-2:2017,IEC62619:2017,IEC62660-3:2016,CTL协议DSH103......
  • jerry99的序列 --binary search, math
      #include<bits/stdc++.h>usingnamespacestd;usingi64=longlong;constintN=1e5+10;constintM=N-10;inttot,vis[N],prime[N];//#define......
  • html+css
    第一章<html><!--解释器--><!DOCTYPEhtml><head> <!--字符集--> <metahttp-equiv="content-type"content="text/html;charset=utf-8"/> <!--刷新跳转--> <meta......
  • 服务端挂了,客户端的 TCP 连接还在吗?
    作者:小林coding计算机八股文网站:https://xiaolincoding.com大家好,我是小林。如果「服务端挂掉」指的是「服务端进程崩溃」,服务端的进程在发生崩溃的时候,内核会发送FI......
  • docker-compose
    docker-compose简介dockercpmpose是给容器做单机编排的Docker-Compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排。docker-compose将所管理的容......