C#使用UdpClient类进行简单通信的实例
程序员文章站
2022-04-15 09:32:19
udpclient 类提供了一些简单的方法,用于在阻止同步模式下发送和接收无连接 udp 数据报。 因为 udp 是无连接传输协议,所以不需要在发送和接收数据前建立远程主机...
udpclient 类提供了一些简单的方法,用于在阻止同步模式下发送和接收无连接 udp 数据报。 因为 udp 是无连接传输协议,所以不需要在发送和接收数据前建立远程主机连接。但您可以选择使用下面两种方法之一来建立默认远程主机:
- 使用远程主机名和端口号作为参数创建 udpclient 类的实例。
- 创建 udpclient 类的实例,然后调用 connect 方法。
可以使用在 udpclient 中提供的任何一种发送方法将数据发送到远程设备。 使用 receive 方法可以从远程主机接收数据。
udpclient 方法还允许发送和接收多路广播数据报。 使用 joinmulticastgroup 方法可以将 udpclient 预订给多路广播组。 使用 dropmulticastgroup 方法可以从多路广播组中取消对 udpclient 的预订。
/// <summary> /// 客户端 /// </summary> class udpsender { static void main(string[] args) { //创建一个udpclient对象,0表示系统自动分配发送端口 //(若同时在本机运行服务端和客户端,则服务端接收和客户端发送需要使用不同端口,否则两个程序使用同一端口将引发冲突) udpclient udpsender = new udpclient(0); //连接到服务端并指定接收端口 udpsender.connect("localhost", 11000); //连接到子网广播地址并指定接收端口 //udpsender.connect("192.168.1.255", 11000); //(在使用tcp/ip协议的网络中,主机标识段全为1的ip地址为广播地址,广播地址传送给主机标识段所涉及的所有计算机。 //例如,对于192.168.1.0(255.255.255.0)网段,其广播地址为192.168.1.255(255的2进制即为11111111), //当发出目的地址为192.168.1.255时,它将分发给该网段上的所有计算机。) //把消息转换成字节流发送到服务端 byte[] sendbytes = encoding.ascii.getbytes("is anybody there?"); udpsender.send(sendbytes, sendbytes.length); //关闭链接 udpsender.close(); } }
/// <summary> /// 服务端 /// </summary> class udpreceive { static void main(string[] args) { //创建一个udpclient对象,11000为接收端口 udpclient udpreceive = new udpclient(11000); //设置远程主机,(ipaddress.any, 0)代表接收所有ip所有端口发送的数据 ipendpoint remoteipendpoint = new ipendpoint(ipaddress.any, 0);//或 ipendpoint remoteipendpoint = null; //监听数据,接收到数据后,把数据转换成字符串并输出 byte[] receivebytes = udpreceive.receive(ref remoteipendpoint); string returndata = encoding.ascii.getstring(receivebytes); console.writeline("this is the message you received " + returndata.tostring()); console.writeline("this message was sent from " + remoteipendpoint.address.tostring() + " on their port number " + remoteipendpoint.port.tostring()); //关闭连接 udpreceive.close(); } }
备注:需要先运行服务端,再运行客户端。否则客户端在服务端运行之前就已经发出数据,则服务端不会接收到数据。
下面是使用 udpclient 类进行多路广播组的简单例子,加入相同的广播组地址即可实现多播。多路广播地址的范围从 224.0.0.0 到 239.255.255.255 ,服务端和客户端使用同一广播地址即可实现多播。
/// <summary> /// 多路广播组客户端 /// </summary> class multicastgroupclient { static void main(string[] args) { //创建一个udpclient对象,0表示系统自动分配发送端口 var client = new udpclient(0); //将广播地址添加到多路广播组,生存期(路由器跳数)为10 var ip = ipaddress.parse("234.56.78.90"); client.joinmulticastgroup(ip, 10); //定义终结点(服务端ip和接收端口),把消息转换成字节流后发送到服务端 var multicast = new ipendpoint(ip, 7788); byte[] bytes = encoding.ascii.getbytes("hello from multicast."); client.send(bytes, bytes.length, multicast); } }
/// <summary> /// 多路广播组服务端 /// </summary> class multicastgroupserver { static void main(string[] args) { //创建一个udpclient对象,7788为接收端口 var client = new udpclient(7788); //将广播地址添加到多路广播组,生存期(路由器跳数)为10 var ip = ipaddress.parse("234.56.78.90"); client.joinmulticastgroup(ip, 10); //设置远程主机,(ipaddress.any, 0)代表接收所有ip所有端口发送的数据 var multicast = new ipendpoint(ipaddress.any, 0);//或 ipendpoint multicast = null; //监听数据,接收到数据后,把数据转换成字符串并输出 byte[] bytes = client.receive(ref multicast); string msg = encoding.ascii.getstring(bytes); console.writeline(msg); } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
推荐阅读