C#实现UDP组播

http://blog.csdn.net/u011555996/article/details/53112871

C#实现UDP组播

  1952人阅读  评论(1)  收藏  举报
  分类:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net;  
  6. using System.Net.Sockets;  
  7. using System.Threading;  
  8.   
  9. namespace Test  
  10. {  
  11.     class Program  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.             UdpClient client = new UdpClient(5566);  
  16.             client.JoinMulticastGroup(IPAddress.Parse("234.5.6.7"));  
  17.             IPEndPoint multicast = new IPEndPoint(IPAddress.Parse("234.5.6.7"), 7788);  
  18.             byte[] buf = Encoding.Default.GetBytes("Hello from multicast");  
  19.             Thread t = new Thread(new ThreadStart(RecvThread));  
  20.             t.IsBackground = true;  
  21.             t.Start();  
  22.             while (true)  
  23.             {  
  24.                 client.Send(buf, buf.Length, multicast);  
  25.                 Thread.Sleep(1000);  
  26.             }  
  27.         }  
  28.   
  29.         static void RecvThread()  
  30.         {  
  31.             UdpClient client = new UdpClient(7788);  
  32.             client.JoinMulticastGroup(IPAddress.Parse("234.5.6.7"));  
  33.             IPEndPoint multicast = new IPEndPoint(IPAddress.Parse("234.5.6.7"), 5566);  
  34.             while (true)  
  35.             {  
  36.                 byte[] buf = client.Receive(ref multicast);  
  37.                 string msg = Encoding.Default.GetString(buf);  
  38.                 Console.WriteLine(msg);  
  39.             }  
  40.         }  
  41.     }  
  42. }  
[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net;  
  6. using System.Net.Sockets;  
  7. using System.Threading;  
  8.   
  9. namespace Test  
  10. {  
  11.     class Program  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.             UdpClient client = new UdpClient(5566);  
  16.             client.JoinMulticastGroup(IPAddress.Parse("234.5.6.7"));  
  17.             IPEndPoint multicast = new IPEndPoint(IPAddress.Parse("234.5.6.7"), 7788);  
  18.             byte[] buf = Encoding.Default.GetBytes("Hello from multicast");  
  19.             Thread t = new Thread(new ThreadStart(RecvThread));  
  20.             t.IsBackground = true;  
  21.             t.Start();  
  22.             while (true)  
  23.             {  
  24.                 client.Send(buf, buf.Length, multicast);  
  25.                 Thread.Sleep(1000);  
  26.             }  
  27.         }  
  28.   
  29.         static void RecvThread()  
  30.         {  
  31.             UdpClient client = new UdpClient(7788);  
  32.             client.JoinMulticastGroup(IPAddress.Parse("234.5.6.7"));  
  33.             IPEndPoint multicast = new IPEndPoint(IPAddress.Parse("234.5.6.7"), 5566);  
  34.             while (true)  
  35.             {  
  36.                 byte[] buf = client.Receive(ref multicast);  
  37.                 string msg = Encoding.Default.GetString(buf);  
  38.                 Console.WriteLine(msg);  
  39.             }  
  40.         }  
  41.     }  
  42. }  

组播地址为 224.0.0.0 ~ 239.255.255.255,其中 224.0.0.0~224.255.255.255 不建议在用户程序中使用,因为它们一般都有特殊用途。
  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值