C#使用UdpClient发送广播消息介绍
来源:互联网 发布:如何安装java软件 编辑:程序博客网 时间:2024/04/29 21:07
首先写个接受消息的客户端。这里偷了点懒,new UdpClient(11000)就是用Udp方式侦听11000端口,侦听任何发送到11000端口的消息都会接收到。
代码 :
UdpClient udpClient = new UdpClient(11000); try { IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); Byte[] receiveBytes = udpClient.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()); udpClient.Close(); } catch (Exception e) { Console.WriteLine(e.ToString()); }
然后写个发Udp的服务器
代码 :
UdpClient udpClient = new UdpClient(11001); try { udpClient.Connect(IPAddress.Parse("192.168.0.255"), 11000); Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody thereA?"); udpClient.Send(sendBytes, sendBytes.Length); udpClient.Close();} catch (Exception e) { Console.WriteLine(e.ToString()); }
其中192.168.0.255是你的内网广播地址,11000是客户端的端口。
广播地址是通过你的子网掩码获得的例如你的网关是192.168.0.1,掩码是255.255.255.0,那么你的广播地址就是192.168.0.255.
0 0
- C#使用UdpClient发送广播消息介绍
- C#使用UdpClient发送和接收UDP数据示例
- C#使用UdpClient发送和接收UDP数据示例 16进制与字符串互转
- C#使用UdpClient发送和接收UDP数据示例 16进制与字符串互转
- 使用UDPClient发送和接收数据
- C# UdpClient使用Receive和BeginReceive接收消息时的不同写法
- C# UdpClient使用Receive和BeginReceive接收消息时的不同写法
- 发送广播消息zz
- Intent发送广播消息
- 发送广播消息BroadcastRecevice
- c# 使用UDPClient实现异步通信
- Android开发之使用Intent发送广播消息(1)
- C#广播消息处理
- c#收发广播消息
- wince 驱动发送广播消息
- wince 驱动发送广播消息
- unity3d 发送广播与消息
- Unity 广播和发送消息
- 141 趙憲 重峯 彰吾母過
- WebView Js注入
- flash 中matrix
- [LeetCode]98.Validate Binary Search Tree
- source Insight常用设置和快捷键大全
- C#使用UdpClient发送广播消息介绍
- JAVA经典算法40题
- Can we declare a class as static
- matlab 读取MPEG-4视频
- CAZyme注释
- UIImagePickerController详解
- 串行通信
- 使用对象流实现序列化
- 支持不同设备(3)支持不同版本