0

I've written an server-application that is supposed to send and listen to upnp packets on several specified interfaces (but the problem already existed when there was only one network card). The code is straight forward and quite simple, but I'm facing a very strange behavior.

I have a list of Endpoints (IPAddresses of the interfaces) the application should listen for and send messages to and then creating a UdpClient for each of them with this code:

private UdpClient c;
private IPEndPoint ep;
public MyClass(IPAddress ip)
{
    ep = new IPEndPoint(ip, 1900);
    c = new UdpClient(ep);
    c.JoinMulticastGroup(IPAddress.Parse("239.255.255.250"));
    c.BeginReceive(onReceive, null);
}

Every minute I send a packet, which works without any problems

byte[] msg = System.Text.Encoding.ASCII.GetBytes(discoverMessage);
c.Send(msg, msg.Length, new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900));

The clients respond to this and my receive function gets called.

 protected void onReceive(IAsyncResult r)
 {
     IPEndPoint rep = new IPEndPoint(IPAddress.Any, 0);
     string msg = Encoding.ASCII.GetString(c.EndReceive(r, ref rep));
     <-- do other things -->
     c.BeginReceive(onReceive, null);
 }

But from time to time it just does not receive any packets, i.e. my receive function is not fired at all, although they are definately coming in (I can see them with wireshark and I know the clients sent them to the network).

The "workaround" to solve this is then to restart the application, disable/enable interfaces, reboot the (guest)machine, change the endpoint-list (for example include 0.0.0.0) - honestly I haven't found THE solution/workaround but a combination of this seems to solve the problem. Once it is working again I can copy back the old config and everything works as before (so the configuration was fine, imho).

I'm using .NET 4.5 on Windows Server 2012, running in a Hyper-V guest on Windows 8, atm with 2 virtual network cards, one connected internal for management and on connected to my physical network card which is not shared with the host and is connected to the clientnetwork.

Has anyone experienced similar problems? I was thinking if Wireshark or winpcap could cause the problem as it sometimes happens when I'm using them to trace any problems. Or could it be a problem with the hyper-V virtual network cards? Or, what I would prefer, am I doing something wrong in my code?

2 Answers2

0

UDP is meant to be a "lossy" protocol. See here. If you want reliabilty, you need to either implement error control or switch to TCP/IP which has that built-in

Srdjan Grubor
  • 2,605
  • 15
  • 17
  • That's why the client send their packets twice :) But that is not the problem, according to wireshark the UDP Pakets arrive at the machine. – user2327792 Apr 27 '13 at 22:15
  • Hmm... now it becomes an interesting question. Are you checking this on the source or the destination machine? – Srdjan Grubor Apr 27 '13 at 22:16
  • and the problem is not that some packets get lost, when the problem begins ALL packets get "lost" for hours (means they are visible in wireshark but the receive function gets not called). – user2327792 Apr 27 '13 at 22:17
  • Are you using wireshark on the receiver? – Srdjan Grubor Apr 27 '13 at 22:18
  • I found something that might be of interest http://stackoverflow.com/a/12835032/1121879 – Srdjan Grubor Apr 27 '13 at 22:21
  • so if I got you right you think the buffer might be too small? at the moment there is one client that responds with 2 257Byte packets every minute.. even if the buffer would be very very very small, at least the first packet should be received :) additionally the packets are always the same in numbers and size no matter if they are seen by my application or not. – user2327792 Apr 27 '13 at 22:35
  • If the app is processing them too slowly, the driver could be discarding them since UDP packets don't have a guarantee to be delivered. I agree with you on the first packet though. – Srdjan Grubor Apr 27 '13 at 22:37
  • processing (time between begin of receive method and the beginreceive) takes 3ms. That could be improved as it's firing some events, but the buffer should by default be larger than 500Byte ;) – user2327792 Apr 27 '13 at 22:54
  • You add winpcap in the mix and you might get some unpredictable results – Srdjan Grubor Apr 27 '13 at 22:56
0

In the case where you have more than a single NIC, what network is the igmp join request going to ?

You may want to use the two parameter version of the JoinMulticastGroup method so that you can include the specific local interface IP address on which you want to look for the multicast traffic. This is particularly relevant in scenarios where the machine has more than a single physical NIC, providing access to multiple networks.

Walter Kelt
  • 2,199
  • 1
  • 18
  • 22