Hi,
I've two network cards(ethernet). They have their own sub-network.
I've to do some discovery on those subnetwork. This discovery will be made with a c# implementation of DNS-SD(Bonjour).
I've currently an issue with socket/UdpClient of c#: If I bind the socket or udpClient on IPAddress.Any, I receive only messages coming from one network card.
Here is a small example of what I tried:
-
private static void ReceiveMessagesOneClient(int port, string ip)
-
{
-
Task.Factory.StartNew(() =>
-
{
-
using (var mUdpClientReceiver = new UdpClient())
-
{
-
var mReceivingEndPoint = new IPEndPoint(IPAddress.Any, port);
-
mUdpClientReceiver.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
-
mUdpClientReceiver.Client.Bind(mReceivingEndPoint);
-
mUdpClientReceiver.JoinMulticastGroup(IPAddress.Parse(ip), 255);
-
while (true)
-
{
-
byte[] data = mUdpClientReceiver.Receive(ref mReceivingEndPoint);
-
-
Console.WriteLine("Message received from {0, 18}", mReceivingEndPoint);
-
}
-
}
-
})
-
;
-
}
-
I can guarantee 100% that I receive mDNS traffic on each cards(controlled with wireshark for each network adapter). I can also guarantee that I'm the only one listening on this port( netstats -ano | find "5353" ).
I've made another test. If I specify the IP on which I wasn't receving any data before:
-
//I'm using the ip 224.0.0.251 and port 5353, which are for mDNS
-
private static void ReceiveMessagesOneClientOnOneIp(int port, string ip, String localIp)
-
{
-
Task.Factory.StartNew(() =>
-
{
-
using (var mUdpClientReceiver = new UdpClient())
-
{
-
var mReceivingEndPoint = new IPEndPoint(IPAddress.Parse(localIp), port);
-
mUdpClientReceiver.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
-
mUdpClientReceiver.Client.Bind(mReceivingEndPoint);
-
mUdpClientReceiver.JoinMulticastGroup(IPAddress.Parse(ip), 255);
-
while (true)
-
{
-
byte[] data = mUdpClientReceiver.Receive(ref mReceivingEndPoint);
-
-
Console.WriteLine("Message received from {0, 18}", mReceivingEndPoint);
-
}
-
}
-
});
-
}
-
I still receive nothing, but if I deactivate the network cards from which I receive data, I'm getting traffic on my card, without making any changes.
What could I've done wrong, that could be affected by disabling a network card that I'm not listening???
I could find a very similar issue
here which is response less since 7 years.
I tried very various things, which had no effects on my issue:
- Using several SocketOptions(MulticastInterface, DontRoute, Broadcast, NoDelay)
- Use directly socket instead of UdpClient
- Bind to IPAddress.Any, IPAddress.Parse("0.0.0.0"), IPAddress.Parse("my-local-ip")
Another test that I made: when listening, I'm sending myself a multicast packet with a socket on the multicast ip/port, and I receive it! For me, it looks like that the OS isn't delivering packages to my socket, no idea why, and no idea about how to find a workaround.
I would appreciate any help since it makes already several day that I'm loosing on this.