C# 网络编程之UDP
一、概述
UDP和TCP是网络通讯常用的两个传输协议,C#一般可以通过Socket来实现UDP和TCP通讯,由于.NET框架通过UdpClient、TcpListener、TcpClient这几个类对Socket进行了封装,使其使用更加方便, 本文就通过这几个封装过的类讲解一下相关应用。
二、UDP基本应用
与TCP通信不同,UDP通信是不分服务端和客户端的,通信双方是对等的。为了描述方便,我们把通信双方称为发送方和接收方。
发送方:
首先创建一个UDP对象:
stringlocateIP="127.0.0.1";//本机IP intlocatePort=9001;//发送端口 IPAddresslocateIpAddr=IPAddress.Parse(locateIP); IPEndPointlocatePoint=newIPEndPoint(locateIpAddr,locatePort); UdpClientudpClient=newUdpClient(locatePoint);
发送数据:
stringremoteIP="127.0.0.1";//目标机器IP intremotePort=9002;//接收端口 IPAddressremoteIpAddr=IPAddress.Parse(remoteIP); IPEndPointremotePoint=newIPEndPoint(remoteIpAddr,remotePort); byte[]buffer=Encoding.UTF8.GetBytes(“hello”); udpClient.Send(buffer,buffer.Length,remotePoint);
以上就完成了一个发送任务,一个较完整的发送代码如下:
publicpartialclassFormServer:Form
{
privateUdpClientudpClient=null;
privatevoidbtnConnect_Click(objectsender,EventArgse)
{
stringlocateIP="127.0.0.1";
intlocatePort=9001;
IPAddresslocateIpAddr=IPAddress.Parse(locateIP);
IPEndPointlocatePoint=newIPEndPoint(locateIpAddr,locatePort);
udpClient=newUdpClient(locatePoint);
this.groupWork.Enabled=true;
}
privatevoidSend_Click(objectsender,EventArgse)
{
stringtext=this.txtSend.Text.Trim();
stringremoteIP="127.0.0.1";
intremotePort=9002;
byte[]buffer=Encoding.UTF8.GetBytes(text);
if(udpClient!=null)
{
IPAddressremoteIp=IPAddress.Parse(remoteIP);
IPEndPointremotePoint=newIPEndPoint(remoteIp,remotePort);
udpClient.Send(buffer,buffer.Length,remotePoint);
}
Debug.WriteLine("SendOK");
}
}
接收端:
首先创建一个UDP对象:
stringlocateIP="127.0.0.1"; intlocatePort=9002; IPAddresslocateIpAddr=IPAddress.Parse(locateIP); IPEndPointlocatePoint=newIPEndPoint(locateIpAddr,locatePort); UdpClientudpClient=newUdpClient(locatePoint);
接收数据:
IPEndPointremotePoint=newIPEndPoint(IPAddress.Parse("1.1.1.1"),1);
varreceived=udpClient.Receive(refremotePoint);
stringinfo=Encoding.UTF8.GetString(received);
stringfrom=$”{remotePoint.Address}:{remotePoint.Port}”;
注意两点:
1、remotePoint是获得发送方的IP信息,定义时可以输入任何合法的IP和端口信息;
2、Receive方法是阻塞方法,所以需要在新的线程内运行,程序会一直等待接收数据,当接收到一包数据时程序就返回,要持续接收数据需要重复调用Receive方法。
一个较完整的接收端代码如下:
publicpartialclassFormClent:Form
{
privateUdpClientudpClient=null;
privatevoidbtnConnect_Click(objectsender,EventArgse)
{
stringlocateIP="127.0.0.1";
intlocatePort=9002;
IPAddresslocateIpAddr=IPAddress.Parse(locateIP);
IPEndPointlocatePoint=newIPEndPoint(locateIpAddr,locatePort);
udpClient=newUdpClient(locatePoint);
IPEndPointremotePoint=newIPEndPoint(IPAddress.Parse("1.1.1.1"),1);
Task.Run(()=>
{
while(true)
{
if(udpClient!=null)
{
varreceived=udpClient.Receive(refremotePoint);
stringinfo=Encoding.UTF8.GetString(received);
stringfrom=$”{remotePoint.Address}:{remotePoint.Port}”;
}
}
});
}
}
三、丢包和乱序问题
当发送端发送一包数据时,不管对方是否接收都是发送成功的,UDP协议本身并不会对发送的可靠性进行验证。(这里的可靠性是指是否接收到,如果对方接收到数据包,其内容还是可靠的,这个在链路层进行了保证。)同时,由于网络延时等因素,先发送的包并不能确定先被接收到,所以由于这两个原因,UDP通信存在丢包和乱序的情况。
某些业务场景下,比如实时状态监控,可能对丢包和乱序情况并不敏感,可以不用处理,但大部分情况下还是介意丢包的,简单的处理办法就是把包的头部固定长度的空间拿出来存放核对信息,比如包编号,如果有缺失,可以要求发送方重发,也可以进行排序。
四、将数据接收包装为事件
我们对UdpClent又进行一次封装,启用一个线程进行接收数据,将接收到的数据包通过事件发布出来,这样使用起来就更方便了。
namespaceCommunication.UDPClient
{
publicclassUdpStateEventArgs:EventArgs
{
publicIPEndPointremoteEndPoint;
publicbyte[]buffer=null;
}
publicdelegatevoidUDPReceivedEventHandler(UdpStateEventArgsargs);
publicclassUDPClient
{
privateUdpClientudpClient;
publiceventUDPReceivedEventHandlerUDPMessageReceived;
publicUDPClient(stringlocateIP,intlocatePort)
{
IPAddresslocateIp=IPAddress.Parse(locateIP);
IPEndPointlocatePoint=newIPEndPoint(locateIp,locatePort);
udpClient=newUdpClient(locatePoint);
//监听创建好后,创建一个线程,开始接收信息
Task.Run(()=>
{
while(true)
{
UdpStateEventArgsudpReceiveState=newUdpStateEventArgs();
if(udpClient!=null)
{
IPEndPointremotePoint=newIPEndPoint(IPAddress.Parse("1.1.1.1"),1);
varreceived=udpClient.Receive(refremotePoint);
udpReceiveState.remoteEndPoint=remotePoint;
udpReceiveState.buffer=received;
UDPMessageReceived?.Invoke(udpReceiveState);
}
else
{
break;
}
}
});
}
}
}
具体使用办法:
privatevoidbtnConnect_Click(objectsender,EventArgse)
{
stringlocateIP="127.0.0.1";
intlocatePort=9002;
UDPClientudpClient=newUDPClient(locateIP,locatePort);
udpClient.UDPMessageReceived+=UdpClient_UDPMessageReceived;
}
privatevoidUdpClient_UDPMessageReceived(UdpStateEventArgsargs)
{
varremotePoint=args.remoteEndPoint;
stringinfo=Encoding.UTF8.GetString(args.buffer);
}
传送门:
C#网络编程入门系列包括三篇文章:
(一)C#网络编程入门之UDP
(二)C#网络编程入门之TCP
(三)C#网络编程入门之HTTP
以上就是C#网络编程之UDP的详细内容,更多关于C#网络编程UDP的资料请关注毛票票其它相关文章!