C# udp 广播接收 发送
| 分类: VCC# | 
向255.255.255.255发送UPD数据包即为UDP广播,接收端只需绑定UDP广播的端口号即可得到数据
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- namespace Test
- {
- class Program
- {
- static void Main(string[] args)
- {
- UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Any, 0));
- IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 7788);
- byte[] buf = Encoding.Default.GetBytes("Hello from UDP broadcast");
- Thread t = new Thread(new ThreadStart(RecvThread));
- t.IsBackground = true;
- t.Start();
- while (true)
- {
- client.Send(buf, buf.Length, endpoint);
- Thread.Sleep(1000);
- }
- }
- static void RecvThread()
- {
- UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Any, 7788));
- IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 0);
- while (true)
- {
- byte[] buf = client.Receive(ref endpoint);
- string msg = Encoding.Default.GetString(buf);
- Console.WriteLine(msg);
- }
- }
- }
- }

 
 转载
转载 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号