单播,组播,广播代码,以及广播,组播地址的计算usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;usingSystem.Timers;usingSystem.Threading;usingSystem.Net.Sockets;usingSystem.Net;namespaceBCast{publicpartialclassMainWindow:Window{publicMainWindow(){InitializeComponent();ThreadHandle();t.Elapsed+=newElapsedEventHandler(t_Elapsed);t.AutoReset=true;t.Enabled=true;t.Start();}voidt_Elapsed(objectsender,ElapsedEventArgse){ClientSender();}Threadthread;System.Timers.Timert=newSystem.Timers.Timer(1000);publicvoidThreadHandle(){thread=newThread(newThreadStart(ClientGeter));thread.IsBackground=true;thread.Start();}UdpClientudpgeter=newUdpClient(10881);IPAddressiap=IPAddress.Parse(127.0.0.1);publicvoidClientGeter(){IPEndPointiep=newIPEndPoint(iap,10881);//udpgeter.JoinMulticastGroup(multicastIP);while(true){byte[]b=udpgeter.Receive(refiep);stringstr=Encoding.UTF8.GetString(b);Console.WriteLine(str);(看看控制台是否得到了你想要的数据)}}UdpClientmulticastUDP=newUdpClient();组播的地址是保留的D类地址从224.0.0.0—239.255.255.255,而且一些地址有特定的用处如,224.0.0.0—244.0.0.255只能用于局域网中路由器是不会转发的,并且224.0.0.1是所有主机的地址,224.0.0.2所有路由器的地址,224.0.0.5所有ospf路由器的地址,224.0.13事PIMv2路由器的地址;239.0.0.0—239.255.255.255是私有地址(如192.168.x..x);224.0.1.0—238.255.255.255可以用与Internet上的。//IPAddressmulticastIP=IPAddress.Parse(224.110.10.1);(组播)所谓广播地址指同时向网上所有的主机发送报文,也就是说,不管物理网络特性如何,internet网支持广播传输。如136.78.255.255就是B类地址中的一个广播地址,你将信息送到此地址,就是将信息送给网络号为136.78的所有主机。计算方法:首先计算网络地址=IP地址逻辑与子网掩码先把IP,子网掩码转为2进制,然后进行逻辑与运算,得出网络地址例:IP192.168.1.3子网掩码255.255.0.0IP转二进制11000000.10100100.00000001.00000011子网掩码11111111.11111111.00000000.00000000与运算后11000000.10100100.00000000.00000000192.168.0.0这就是网络地址,计出来后把网络地址后面的0全部改为255就是广播地址了,那么此例中广播地址就为192.168.255.255如果不会算的就在cmd中输入ipconfig,然后在计算机中用程序员模式计算就OK了IPAddressmulticastIP=IPAddress.Parse(192.168.1.255);(广播)intmulticastPort=10881;publicvoidClientSender(){IPEndPointmulticastIep=newIPEndPoint(multicastIP,multicastPort);multicastUDP.EnableBroadcast=true;stringinfo=string.Format(xxxxxxxxxxxxxxxxxxxxxxxxx);byte[]bInfo=Encoding.UTF8.GetBytes(info);multicastUDP.Send(bInfo,bInfo.Length,multicastIep);}}}