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?