0

I am trying to do discovery via UDP, my code sends a multicast message and other devices on the network reply back to me. I am using UdpClient on .NET 4.5.2, binding it on a random port and sending my message to a multicast address that the devices are listening on (e.g. 233.255.255.237:8003). The devices reply to me from the multicast port 8003, but some reply from their own ip (e.g. 10.0.23.66) and some from local broadcast ip (e.g. 10.0.23.255). This runs just fine on Windows, I can see replies from all devices, but when running on Mono (version 5.2.0.224), I can only see the messages sent from the devices that don't use the local broadcast ip.

When I do tcpdump and open it in Wireshark I can clearly see the UDP messages reaching me, even those with Source = 10.0.23.255 they have the correct destination ip and port(my random port), but the code never picks them up...

I have searched SO and the web and tried literally everything over the past 2 days, different UdpClient constructors, 2 UdpClients on the same port(otherwise no replies are be picked up, receiving code has to listen on the same port as sending code is using) - one for sending and one for receiving, using a plain socket for receiving, setting EnableBroadcast = true, binding to a specific port(multicast port and other), using JoinMulticastGroup(shouldn't matter though, I am only sending messages to multicast address, not receiving the, the replies come directly to my local andpoint), and then some, but nothing works and I'm at the end of my wits... Maybe there is a mono bug / different behavior, or some arcane setting that can be used, I would appreciate any help.

Here is how my code looks like(omitting parts like cleanup when disposing, etc.):

client = new UdpClient { MulticastLoopback = false, EnableBroadcast = true };
client.Client.Bind(new IPEndPoint(IPAddress.Any, 0));
client.BeginReceive(EndReceive, null);

private void EndReceive(IAsyncResult ar)
{
try
   {
      var source = new IPEndPoint(IPAddress.Any, 0);
      var data = client.EndReceive(ar, ref source);
      Console.WriteLine("{0} received msg:\r\n{1}", source.Address, Encoding.UTF8.GetString(data));
   }
   catch (Exception e)
   {
       Console.WriteLine(e);
   }
   client.BeginReceive(EndReceive, null);
}

For sending the multicast message I use client.Send() in try catch as well, the messages are definitely sent and clients are responding as seen on Wireshark, just under Windows I get all the responses written out to Console, under Linux/Mono only the ones that respond from Source=10.0.23.XXX and and the ones from 10.0.23.255 seem to be filtered out (I compared the messages carefully in Wireshark, they are the same whether my code runs on Win or Linux/Mono).

Martin Sykora
  • 415
  • 5
  • 5

0 Answers0