C#广播消息处理

  1. //C#发送广播信息
  2. Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
  3. sock.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.Broadcast,2);
  4. IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast,9095);
  5. byte[] data = System.Text.Encoding.ASCII.GetBytes(textBox1.Text.Trim());
  6. sock.SendTo(data,iep);
  7. sock.Close();
  8. //C#获取网络上的广播信息
  9. Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
  10.    IPEndPoint iep = new IPEndPoint(IPAddress.Any,9050);
  11.    sock.Bind(iep);
  12.    EndPoint ep = (EndPoint)iep;
  13.    listBox1.Items.Add("ready to receive...");
  14.    byte[] data = new byte[1024];
  15.    int recv = sock.ReceiveFrom(data,ref ep);
  16.    string stringData = System.Text.Encoding.ASCII.GetString(data,0,recv);
  17.    string infomation = "received: " + stringData + " from: " + ep.ToString();  
  18.    listBox1.Items.Add(infomation);
  19.    sock.Close();
  20. //下列范例使用 UdpClient,在通讯端口11000传送UDP 资料包至多点传送位址群组 224.268.100.2。它传送命令列上指定的信息字串。
  21. using System;  
  22. using System.Net;  
  23. using System.Net.Sockets;  
  24. using System.Text;  
  25. public class UDPMulticastSender {  
  26. private static IPAddress GroupAddress =  
  27. IPAddress.Parse("224.168.100.2");  
  28. private static int GroupPort = 11000;  
  29. private static void Send( String message) {  
  30. UdpClient sender = new UdpClient();  
  31. IPEndPoint groupEP = new IPEndPoint(GroupAddress,GroupPort);  
  32. try {  
  33. Console.WriteLine("Sending datagram : {0}", message);  
  34. byte[] bytes = Encoding.ASCII.GetBytes(message);  
  35. sender.Send(bytes, bytes.Length, groupEP);  
  36. sender.Close();  
  37. catch (Exception e) {  
  38. Console.WriteLine(e.ToString());  
  39. }  
  40. }  
  41. public static int Main(String[] args) {  
  42. Send(args[0]);  
  43. return 0;  
  44. }  
  45. //下列范例使用 UdpClient,在通讯端口11000监听广播到多点传送位址群组 224.168.100.2 的UDP资料包。
  46. using System;  
  47. using System.Net;  
  48. using System.Net.Sockets;  
  49. using System.Text;  
  50. public class UDPMulticastListener {  
  51. private static readonly IPAddress GroupAddress =  
  52. IPAddress.Parse("224.168.100.2");  
  53. private const int GroupPort = 11000;  
  54. private static void StartListener() {  
  55. bool done = false;  
  56. UdpClient listener = new UdpClient();  
  57. IPEndPoint groupEP = new IPEndPoint(GroupAddress,GroupPort);  
  58. try {  
  59. listener.JoinMulticastGroup(GroupAddress);  
  60. listener.Connect(groupEP);  
  61. while (!done) {  
  62. Console.WriteLine("Waiting for broadcast");  
  63. byte[] bytes = listener.Receive( ref groupEP);  
  64. Console.WriteLine("Received broadcast from {0} :/n {1}/n",  
  65. groupEP.ToString(),  
  66. Encoding.ASCII.GetString(bytes,0,bytes.Length));  
  67. }  
  68. listener.Close();  
  69. catch (Exception e) {  
  70. Console.WriteLine(e.ToString());  
  71. }  
  72. }  
  73. public static int Main(String[] args) {  
  74. StartListener();  
  75. return 0;  
  76. }  
  77. }  
  78. //在网络编程中,通过广播和多播可以实现发送端发送一个数据包,有多个接收端接收的情况。
  79. /*广播
  80. 由于Tcp是有连接的,所以不能用来发送广播消息。发送广播消息,必须用到Udp,Udp可以不用建立连接而发送消息。广播消息的目的IP地址是一种特殊IP地址,称为广播地址。广播地址由IP地址网络前缀加上全1主机后缀组成,如:192.168.1.255是192.169.1.0这个网络的广播地址;130.168.255.255是130.168.0.0这个网络的广播地址。向全部为1的IP地址(255.255.255.255)发送消息的话,那么理论上全世界所有的联网的计算机都能收得到了。但实际上不是这样的,一般路由器上设置抛弃这样的包,只在本地网内广播,所以效果和向本地网的广播地址发送消息是一样的。
  81. C#中发送广播消息的过程如下,注意要调用SetSockOption函数,不然要抛出异常:*/
  82.    Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
  83.                 ProtocolType.Udp);
  84.    IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 9050);//255.255.255.255
  85.    IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("192.168.1.255"), 9050);
  86.    string hostname = Dns.GetHostName();
  87.    byte[] data = Encoding.ASCII.GetBytes(hostname);
  88.    sock.SetSocketOption(SocketOptionLevel.Socket,
  89.               SocketOptionName.Broadcast, 1);
  90.    sock.SendTo(data, iep1);
  91.    sock.SendTo(data, iep2);
  92.    sock.Close();
  93. //C#中接收广播消息的过程如下,没有什么特别的:
  94.    Socket sock = new Socket(AddressFamily.InterNetwork,
  95.            SocketType.Dgram, ProtocolType.Udp);
  96.    IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
  97.    sock.Bind(iep);
  98.    EndPoint ep = (EndPoint)iep;
  99.    Console.WriteLine("Ready to receive…");
  100.    byte[] data = new byte[1024];
  101.    int recv = sock.ReceiveFrom(data, ref ep);
  102.    string stringData = Encoding.ASCII.GetString(data, 0, recv);
  103.    Console.WriteLine("received: {0} from: {1}",
  104.               stringData, ep.ToString());
  105.    data = new byte[1024];
  106.    recv = sock.ReceiveFrom(data, ref ep);
  107.    stringData = Encoding.ASCII.GetString(data, 0, recv);
  108.    Console.WriteLine("received: {0} from: {1}",
  109.               stringData, ep.ToString());
  110.    sock.Close();
  111. /*Attentions:
  112. 1 广播可以由客户程序来通知子网内的服务程序,自己的位置。
  113. 2 发送广播消息时指定的端口也是有作用的,接收端的Udp Socket如果Bind到此接口的话,就能够接收到消息。(如果不Bind的话,就能接收所有端口的消息??)
  114. 3 接收端接到的包中显示的远端IP地址是发送端的地址。就是说广播地址不会显示到一个包的源IP地址位置(LoopBack地址不会显示到包的目的IP地址位置)
  115. 4 可以用线程不停地发送和接收广播消息,确认两端的位置和证明自己的存在*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值