马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

Udp Client发送局域网广播消息

[复制链接]
查看5008 | 回复2 | 2012-12-5 16:12:54 | 显示全部楼层 |阅读模式
  1. new UdpClient(11000)就是用Udp方式侦听11000端口,侦听任何发送到11000端口的消息都会接收到。
  2. <div class="blockcode"><blockquote>UdpClient udpClient = new UdpClient(11000);
  3. try {     
  4. IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);  
  5. Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);  
  6. string returnData = Encoding.ASCII.GetString(receiveBytes);   
  7.   Console.WriteLine("This is the message you received " +                                  returnData.ToString());   
  8. Console.WriteLine("This message was sent from " +                                 RemoteIpEndPoint.Address.ToString() +                        
  9.         " on their port number " +                           
  10.   RemoteIpEndPoint.Port.ToString());
  11.     udpClient.Close();
  12. }
  13. catch (Exception e) {
  14.     Console.WriteLine(e.ToString());
  15. }
  16. <p>然后写个发Udp的服务器</p><p> </p><div class="cnblogs_code"><span class="cnblogs_code_collapse">代码</span> :<div style="display: block;" id="cnblogs_code_open_7f65fb5c-36d6-4545-bb7a-62f606976cee" class="cnblogs_code_hide" jquery1260496893593="7"><pre><div><!--
  17. Code highlighting produced by Actipro CodeHighlighter (freeware)
  18. http://www.CodeHighlighter.com/
  19. --><span style="color: rgb(0, 0, 0);">UdpClient udpClient </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> UdpClient(</span><span style="color: rgb(128, 0, 128);">11001</span><span style="color: rgb(0, 0, 0);">);
  20. </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);">
  21. {
  22.     udpClient.Connect(IPAddress.Parse(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">192.168.0.255</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">), </span><span style="color: rgb(128, 0, 128);">11000</span><span style="color: rgb(0, 0, 0);">);  
  23.     Byte[] sendBytes </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Encoding.ASCII.GetBytes(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Is anybody thereA?</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
  24.     udpClient.Send(sendBytes, sendBytes.Length);
  25.     udpClient.Close();
  26. }
  27. </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e)
  28. {
  29.     Console.WriteLine(e.ToString());
  30. }
  31. </span></div></pre></div></div><p>其中192.168.0.255是你的内网广播地址,11000是客户端的端口。</p><p>广播地址是通过你的子网掩码获得的例如你的网关是192.168.0.1,掩码是255.255.255.0,那么你的广播地址就是192.168.0.255.</p>
  32. <ins style="margin: 0px; padding: 0px; border: currentColor; width: 728px; height: 90px; display: inline-table; visibility: visible; position: relative;">
  33. <ins style="margin: 0px; padding: 0px; border: currentColor; width: 728px; height: 90px; display: block; visibility: visible; position: relative;" id="aswift_1_anchor">
  34. <iframe style="left: 0px; top: 0px; position: absolute;" id="aswift_1" vspace="0" height="90" marginHeight="0" frameBorder="0" width="728" allowTransparency="true" name="aswift_1" marginWidth="0" scrolling="no" hspace="0">
  35. </iframe>
  36. </ins>
  37. </ins>
复制代码
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
chao2332601 | 2013-6-16 02:10:01 | 显示全部楼层
谢谢分享!!!
chao2332601 | 2013-6-16 04:53:31 | 显示全部楼层
谢谢分享!!!
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则