C# UDP 广播客户端/服务器不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1156065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# UDP broadcast client/server does not work
提问by Craig
I'm using .NET 2.0 and have created a fairly simple udp broadcast app and UDP listener.
我正在使用 .NET 2.0 并创建了一个相当简单的 udp 广播应用程序和 UDP 侦听器。
Listener code:
监听器代码:
Socket listener = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
IPEndPoint localEndPoint = new IPEndPoint( IPAddress.Any, 11000 );
listener.Bind( localEndPoint );
EndPoint ep = (EndPoint)localEndPoint;
Console.WriteLine("Ready to receive…");
byte[] data = new byte[1024];
int recv = listener.ReceiveFrom(data, ref ep);
string stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString());
listener.Close();
Server code:
服务器代码:
int groupPort = 11000;
IPEndPoint groupEP = new IPEndPoint( IPAddress.Parse( "255.255.255.255" ), groupPort );
if ( radioButton2.Checked )
{
groupEP = new IPEndPoint( IPAddress.Broadcast, groupPort );
}
else if ( radioButton3.Checked )
{
groupEP = new IPEndPoint( IPAddress.Parse( "172.16.75.15" ), groupPort );
}
Socket socket = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
socket.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1 );
socket.SendTo( System.Text.ASCIIEncoding.ASCII.GetBytes( testTextBox.Text ), groupEP );
The server is just a simple windows app with 3 radio buttons, button and a textbox.
服务器只是一个简单的 Windows 应用程序,带有 3 个单选按钮、按钮和一个文本框。
When I run the server on a separate computer and choose radioButton3 I receive the message just fine on my client listener (which is running on ip address 172.16.75.15). However if I choose the first or second radiobutton (which creates Broadcast or 255.255.255.255 as the ip address) I get nothing. Now if I run the client on the same pc as the server I can receive the message using those two choices.
当我在单独的计算机上运行服务器并选择 radioButton3 时,我在我的客户端侦听器(在 IP 地址 172.16.75.15 上运行)上收到了很好的消息。但是,如果我选择第一个或第二个单选按钮(创建广播或 255.255.255.255 作为 IP 地址),我什么也得不到。现在,如果我在与服务器相同的 PC 上运行客户端,我可以使用这两个选项接收消息。
I'm not sure what I'm doing wrong or if there could be some kind of firewall preventing UDP messages on the LAN. Any ideas?
我不确定我做错了什么,或者是否有某种防火墙阻止 LAN 上的 UDP 消息。有任何想法吗?
Thanks,
谢谢,
Craig
克雷格
回答by Spencer Ruport
Try a broadcast on the local subnet only. IE if your subnet is 255.255.255.0 try a broadcast of 172.16.75.255. It may be that windows, a router or even network card automatically block universal broadcasts as a preventative measure.
仅在本地子网上尝试广播。IE 如果您的子网是 255.255.255.0,请尝试广播 172.16.75.255。作为预防措施,Windows、路由器甚至网卡可能会自动阻止通用广播。
回答by Robert S. Barnes
Just a note, but if you are writing a new app from the ground up then you should really be using multicast instead of broadcast.
请注意,但如果您正在从头开始编写新应用程序,那么您确实应该使用多播而不是广播。
回答by tvanfosson
Is the client on the same physical network as the server? If not, you won't be able to do a local broadcast (255.255.255.255) and will need to do a directed subnet broadcast. You will have to enable your router to allow a directed subnet broadcast (172.16.75.255) before that will work.
客户端是否与服务器在同一物理网络上?如果没有,您将无法进行本地广播 (255.255.255.255),并且需要进行定向子网广播。在此之前,您必须启用路由器以允许定向子网广播 (172.16.75.255)。
回答by Bitmask
I just tried your code and worked for me. The only thing i changed was the IPAddress to match my local IP Address for the server.
我刚刚尝试了您的代码并为我工作。我唯一更改的是 IPAddress 以匹配服务器的本地 IP 地址。
回答by Massimo
Craig, I develop a C# udp client/server tool, using broadcasts, it works ( datagrams are received by all the clients on the same subnet ). Just avoid 255.255.255.255, windows cannot always take the right route. You have to help windows specifying the right subnet. Server : bind the udp socket to 172.16.75.15, and sendto the local broadcast address ( are you sure about 172.16.75.255 ? check your subnet mask ).
Craig,我开发了一个 C# udp 客户端/服务器工具,使用广播,它可以工作(数据报由同一子网上的所有客户端接收)。只要避开 255.255.255.255,窗户就不能总是走正确的路线。您必须帮助 Windows 指定正确的子网。服务器:将 udp 套接字绑定到 172.16.75.15,并发送到本地广播地址(您确定是 172.16.75.255 吗?检查您的子网掩码)。
回答by user3130012
I had the similar problem, nothing seemed to work in all the code I saw here. when I started my program there was this firewall window that pops up telling you the firewall has blocked some features.
我遇到了类似的问题,我在这里看到的所有代码似乎都不起作用。当我启动我的程序时,会弹出这个防火墙窗口,告诉您防火墙阻止了某些功能。
my problem was I was clicking allow access without ticking the check box that says "Private networks, suck as my home or work network". you can change this later of course in your firewall setting and tick that check box. both de server and the other machine must have that check box checked. Or at least that's what makes my mine work.
我的问题是我点击了允许访问而不勾选“私人网络,就像我的家庭或工作网络一样糟糕”的复选框。当然,您可以稍后在防火墙设置中更改此设置并勾选该复选框。de server 和另一台机器都必须选中该复选框。或者至少这就是我的工作的原因。
Also i had to change my broadcast IP address to for example 192.168.1.255. My router does block the recommended by my book 224.0.0.0 - 239.255.255.255;
此外,我不得不将我的广播 IP 地址更改为例如 192.168.1.255。我的路由器确实阻止了我书中推荐的 224.0.0.0 - 239.255.255.255;