- new UdpClient(11000)就是用Udp方式侦听11000端口,侦听任何发送到11000端口的消息都会接收到。
- <div class="blockcode"><blockquote>UdpClient udpClient = new UdpClient(11000);
- try {
- IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
- Byte[] receiveBytes = udpClient.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());
- udpClient.Close();
- }
- catch (Exception e) {
- Console.WriteLine(e.ToString());
- }
- <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><!--
- Code highlighting produced by Actipro CodeHighlighter (freeware)
- http://www.CodeHighlighter.com/
- --><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);">);
- </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);">
- {
- 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);">);
- 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);">);
- udpClient.Send(sendBytes, sendBytes.Length);
- udpClient.Close();
- }
- </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- </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>
- <ins style="margin: 0px; padding: 0px; border: currentColor; width: 728px; height: 90px; display: inline-table; visibility: visible; position: relative;">
- <ins style="margin: 0px; padding: 0px; border: currentColor; width: 728px; height: 90px; display: block; visibility: visible; position: relative;" id="aswift_1_anchor">
- <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">
- </iframe>
- </ins>
- </ins>
复制代码 |
|