UdpClient 類提供了一些簡(jiǎn)單的方法,用于在阻止同步模式下發(fā)送和接收無連接 UDP 數(shù)據(jù)報(bào)。 因?yàn)?UDP 是無連接傳輸協(xié)議,所以不需要在發(fā)送和接收數(shù)據(jù)前建立遠(yuǎn)程主機(jī)連接。但您可以選擇使用下面兩種方法之一來建立默認(rèn)遠(yuǎn)程主機(jī):
可以使用在 UdpClient 中提供的任何一種發(fā)送方法將數(shù)據(jù)發(fā)送到遠(yuǎn)程設(shè)備。 使用 Receive 方法可以從遠(yuǎn)程主機(jī)接收數(shù)據(jù)。
UdpClient 方法還允許發(fā)送和接收多路廣播數(shù)據(jù)報(bào)。 使用 JoinMulticastGroup 方法可以將 UdpClient 預(yù)訂給多路廣播組。 使用 DropMulticastGroup 方法可以從多路廣播組中取消對(duì) UdpClient 的預(yù)訂。
/// <summary>/// 客戶端/// </summary>class UDPSender{  static void Main(string[] args)  {    //創(chuàng)建一個(gè)UdpClient對(duì)象,0表示系統(tǒng)自動(dòng)分配發(fā)送端口    //(若同時(shí)在本機(jī)運(yùn)行服務(wù)端和客戶端,則服務(wù)端接收和客戶端發(fā)送需要使用不同端口,否則兩個(gè)程序使用同一端口將引發(fā)沖突)    UdpClient udpSender = new UdpClient(0);    //連接到服務(wù)端并指定接收端口    udpSender.Connect("localhost", 11000);    //連接到子網(wǎng)廣播地址并指定接收端口    //udpSender.Connect("192.168.1.255", 11000);    //(在使用TCP/IP協(xié)議的網(wǎng)絡(luò)中,主機(jī)標(biāo)識(shí)段全為1的IP地址為廣播地址,廣播地址傳送給主機(jī)標(biāo)識(shí)段所涉及的所有計(jì)算機(jī)。    //例如,對(duì)于192.168.1.0(255.255.255.0)網(wǎng)段,其廣播地址為192.168.1.255(255的2進(jìn)制即為11111111),    //當(dāng)發(fā)出目的地址為192.168.1.255時(shí),它將分發(fā)給該網(wǎng)段上的所有計(jì)算機(jī)。)    //把消息轉(zhuǎn)換成字節(jié)流發(fā)送到服務(wù)端    byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");    udpSender.Send(sendBytes, sendBytes.Length);    //關(guān)閉鏈接    udpSender.Close();  }}/// <summary>/// 服務(wù)端/// </summary>class UDPReceive{  static void Main(string[] args)  {    //創(chuàng)建一個(gè)UdpClient對(duì)象,11000為接收端口    UdpClient udpReceive = new UdpClient(11000);    //設(shè)置遠(yuǎn)程主機(jī),(IPAddress.Any, 0)代表接收所有IP所有端口發(fā)送的數(shù)據(jù)    IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);//或 IPEndPoint remoteIpEndPoint = null;    //監(jiān)聽數(shù)據(jù),接收到數(shù)據(jù)后,把數(shù)據(jù)轉(zhuǎn)換成字符串并輸出    byte[] receiveBytes = udpReceive.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());     //關(guān)閉連接    udpReceive.Close();  }}備注:需要先運(yùn)行服務(wù)端,再運(yùn)行客戶端。否則客戶端在服務(wù)端運(yùn)行之前就已經(jīng)發(fā)出數(shù)據(jù),則服務(wù)端不會(huì)接收到數(shù)據(jù)。
下面是使用 UdpClient 類進(jìn)行多路廣播組的簡(jiǎn)單例子,加入相同的廣播組地址即可實(shí)現(xiàn)多播。多路廣播地址的范圍從 224.0.0.0 到 239.255.255.255 ,服務(wù)端和客戶端使用同一廣播地址即可實(shí)現(xiàn)多播。
/// <summary>/// 多路廣播組客戶端/// </summary>class MulticastGroupClient{  static void Main(string[] args)  {    //創(chuàng)建一個(gè)UdpClient對(duì)象,0表示系統(tǒng)自動(dòng)分配發(fā)送端口    var client = new UdpClient(0);    //將廣播地址添加到多路廣播組,生存期(路由器跳數(shù))為10    var ip = IPAddress.Parse("234.56.78.90");    client.JoinMulticastGroup(ip, 10);    //定義終結(jié)點(diǎn)(服務(wù)端IP和接收端口),把消息轉(zhuǎn)換成字節(jié)流后發(fā)送到服務(wù)端    var multicast = new IPEndPoint(ip, 7788);    byte[] bytes = Encoding.ASCII.GetBytes("Hello from multicast.");    client.Send(bytes, bytes.Length, multicast);  }}/// <summary>/// 多路廣播組服務(wù)端/// </summary>class MulticastGroupServer{  static void Main(string[] args)  {    //創(chuàng)建一個(gè)UdpClient對(duì)象,7788為接收端口    var client = new UdpClient(7788);    //將廣播地址添加到多路廣播組,生存期(路由器跳數(shù))為10    var ip = IPAddress.Parse("234.56.78.90");    client.JoinMulticastGroup(ip, 10);    //設(shè)置遠(yuǎn)程主機(jī),(IPAddress.Any, 0)代表接收所有IP所有端口發(fā)送的數(shù)據(jù)    var multicast = new IPEndPoint(IPAddress.Any, 0);//或 IPEndPoint multicast = null;    //監(jiān)聽數(shù)據(jù),接收到數(shù)據(jù)后,把數(shù)據(jù)轉(zhuǎn)換成字符串并輸出    byte[] bytes = client.Receive(ref multicast);    string msg = Encoding.ASCII.GetString(bytes);    Console.WriteLine(msg);  }}以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持武林網(wǎng)!
新聞熱點(diǎn)
疑難解答
圖片精選