udp广播包如何被wireshark收到但socket监听器收不到
我有一个 C# 应用程序可以在多台机器上运行,但由于某种原因不能在另一台机器上运行。都是Windows XP。
我只需打开一个端口并监听:
void Open()
{
var myIpAddress = UdpComm.GetPcIpAddress(target);
listenEndPoint = new IPEndPoint(myIpAddress, RemotePort);
System.Windows.Forms.MessageBox.Show("Creating listener: " + target.ToString() + " - " + listenEndPoint.ToString());
_client = new UdpClient(listenEndPoint);
_client.EnableBroadcast = true;
_client.BeginReceive(ReceiveCallback, null);
}
public void ReceiveCallback(IAsyncResult ar)
{
System.Windows.Forms.MessageBox.Show("Data received");
}
当我运行程序时,我看到 Open 方法成功运行,并且地址和端口看起来正确。
当我在 Wireshark 上查看此内容时,我还看到从远程地址正确发送的数据,但我从未在回调中看到消息框。
我没有抛出任何错误。有什么想法可能会导致数据显示在 Wireshark 上,但不显示在我的应用程序中吗?
I have a C# app that works on several machines, but for some reason not on another. All are Windows XP.
I simply open up a port and listen:
void Open()
{
var myIpAddress = UdpComm.GetPcIpAddress(target);
listenEndPoint = new IPEndPoint(myIpAddress, RemotePort);
System.Windows.Forms.MessageBox.Show("Creating listener: " + target.ToString() + " - " + listenEndPoint.ToString());
_client = new UdpClient(listenEndPoint);
_client.EnableBroadcast = true;
_client.BeginReceive(ReceiveCallback, null);
}
public void ReceiveCallback(IAsyncResult ar)
{
System.Windows.Forms.MessageBox.Show("Data received");
}
When I run the program, I see that the Open method runs successfully, and that the addresses and ports look correct.
When I look at this on Wireshark I also see that the data being sent from the remote address correctly, but I never see a message box from the callback.
I don't have any errors being thrown. Any ideas of what could cause the data to show up on Wireshark, but not in my app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Wireshark 捕获所有内容,而您的应用程序仅获取过滤后的内容。
问题可能出在发件人方面。本质上,子网掩码定义了地址的哪一部分定义了网络以及哪个节点。因此,子网掩码为 255.255.252.0 的网络地址长度为 22 位。
假设您的客户端地址为 10.0.16.100\22。出于广播目的,保留具有最高可能地址的节点地址。许多应用程序期望网络掩码为 24 位长 (255.255.255.0) 并广播到 10.0.16.255。这是错误的,因为只设置了最后 8 位。该子网中正确的广播地址为 10.0.19.255
Wireshark captures EVERYTHING, while your application only gets what gets after filtering.
The problem might be in sender's side. In essence, subnet mask define which part of address defines network and which node. Therefore, with subnet mask being 255.255.252.0 network address is 22 bits long.
Let's say your client is at 10.0.16.100\22. For broadcasting purposes node address with highest possible address is reserved. Many applications expect netmask to be 24 bits long (255.255.255.0) and would broadcast to 10.0.16.255. Which is wrong, because only 8 last bits are set. Proper broadcast address in such subnet would be 10.0.19.255
当我将 NIC 的子网掩码更改为 255.255.255.0 而不是 255.255.252.0 时,我的回调开始被调用。
我不确定为什么wireshark可以看到流量,但看不到UdpClient,但这种变化似乎产生了影响。
My callback began being called once I changed the subnet mask of the NIC to be 255.255.255.0 instead of 255.255.252.0.
I am not sure why wireshark could see the traffic, but not the UdpClient, but that change seemed to have made the difference.
您必须结束异步接收进程才能捕获传入数据。当您调用 _client.BeginReceive() 时,它会生成一个线程来为您接收传入数据。为了捕获此数据,您应该将以下代码添加到您的 ReceiveCallback 中。然后您将能够根据需要使用传入的 byte[]。
此外,您可以通过以下链接引用 MSDN 上的 UdpClient 类:
http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.endreceive.aspx
You must end the asynchronous receive process in order to capture the incoming data. When you call _client.BeginReceive() it spawns a thread that receives the incoming data for you. In order to capture this data you should add the following code below to your ReceiveCallback. Then you will be able to use the incoming byte[] as you see fit.
Additionally, you can reference the UdpClient class on MSDN at the following link:
http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.endreceive.aspx