C# UDP广播客户端/服务器不起作用

发布于 2024-07-27 23:20:07 字数 1561 浏览 10 评论 0原文

我正在使用 .NET 2.0 并创建了一个相当简单的 udp 广播应用程序和 UDP 侦听器。

侦听器代码:

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();

服务器代码:

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 );

服务器只是一个简单的 Windows 应用程序,带有 3 个单选按钮、按钮和文本框。

当我在单独的计算机上运行服务器并选择 radioButton3 时,我在客户端侦听器(在 IP 地址 172.16.75.15 上运行)上正常收到消息。 但是,如果我选择第一个或第二个单选按钮(创建广播或 255.255.255.255 作为 IP 地址),我什么也得不到。 现在,如果我在与服务器相同的电脑上运行客户端,我可以使用这两个选择接收消息。

我不确定我做错了什么,或者是否有某种防火墙阻止 LAN 上的 UDP 消息。 有任何想法吗?

谢谢,

克雷格

I'm using .NET 2.0 and have created a fairly simple udp broadcast app and UDP listener.

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.

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.

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?

Thanks,

Craig

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

澉约 2024-08-03 23:20:07

仅尝试在本地子网上进行广播。 IE 如果您的子网是 255.255.255.0,请尝试 172.16.75.255 的广播。 作为预防措施,Windows、路由器甚至网卡可能会自动阻止通用广播。

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.

葬花如无物 2024-08-03 23:20:07

客户端和服务器是否在同一个物理网络上? 如果没有,您将无法执行本地广播 (255.255.255.255),并且需要执行定向子网广播。 您必须先启用路由器以允许定向子网广播 (172.16.75.255),然后才能正常工作。

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.

筱果果 2024-08-03 23:20:07

我遇到了类似的问题,我在这里看到的所有代码似乎都不起作用。
当我启动程序时,会弹出一个防火墙窗口,告诉您防火墙已阻止某些功能。

我的问题是我点击允许访问而没有勾选“私人网络,像我的家庭或工作网络一样糟糕”的复选框。 当然,您可以稍后在防火墙设置中更改此设置并勾选该复选框。 服务器和另一台机器都必须选中该复选框。 或者至少这就是我的工作的动力。

此外,我还必须将广播 IP 地址更改为 192.168.1.255。 我的路由器确实阻止了我的书推荐的224.0.0.0 - 239.255.255.255;

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.

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;

她比我温柔 2024-08-03 23:20:07

请注意,但如果您正在从头开始编写新应用程序,那么您确实应该使用多播而不是广播。

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.

寄与心 2024-08-03 23:20:07

我刚刚尝试了你的代码并为我工作。 我唯一改变的是 IP 地址以匹配我的服务器本地 IP 地址。

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.

七度光 2024-08-03 23:20:07

克雷格,我开发了一个 C# udp 客户端/服务器工具,使用广播,它可以工作(数据报由同一子网上的所有客户端接收)。 只是避免255.255.255.255,Windows不能始终走正确的路线。 您必须帮助 Windows 指定正确的子网。 服务器:将udp套接字绑定到172.16.75.15,并发送到本地广播地址(您确定是172.16.75.255吗?检查您的子网掩码)。

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 ).

陌生 2024-08-03 23:20:07

在我的例子中,255.255.255.255 由于网络连接中的 Teamviewer VPN 驱动程序而无法工作。 我使用子网掩码是因为我必须找到设备而不是静态 IP,因为 DHCP 的 IP 经常更改。

In my case 255.255.255.255 not working because of Teamviewer VPN driver ,In Network Connections. I am using Subnet mask because of i have to find the device rather than static ip, because ip is often changed for DHCP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文