问题
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?
回答1:
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
回答2:
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.
回答3:
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.
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] incomingBytes = _client.EndReceive(ar, ref endPoint);
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
来源:https://stackoverflow.com/questions/7478732/how-can-a-udp-broadcast-packet-be-received-by-wireshark-but-not-socket-listener