13

I am trying to get some simple UDP communication working on my local network.

All i want to do is do a multicast to all machines on the network

Here is my sending code

    public void SendMessage(string message)
    {
        var data = Encoding.Default.GetBytes(message);
        using (var udpClient = new UdpClient(AddressFamily.InterNetwork))
        {
            var address = IPAddress.Parse("224.100.0.1");
            var ipEndPoint = new IPEndPoint(address, 8088);
            udpClient.JoinMulticastGroup(address);
            udpClient.Send(data, data.Length, ipEndPoint);
            udpClient.Close();
        }
    }

and here is my receiving code

    public void Start()
    {
        udpClient = new UdpClient(8088);
        udpClient.JoinMulticastGroup(IPAddress.Parse("224.100.0.1"), 50);

        receiveThread = new Thread(Receive);
        receiveThread.Start();
    }

    public void Receive()
    {
        while (true)
        {
            var ipEndPoint = new IPEndPoint(IPAddress.Any, 0);
            var data = udpClient.Receive(ref ipEndPoint);

            Message = Encoding.Default.GetString(data);

            // Raise the AfterReceive event
            if (AfterReceive != null)
            {
                AfterReceive(this, new EventArgs());
            }
        }
    }

It works perfectly on my local machine but not across the network.

-Does not seem to be the firewall. I disabled it on both machines and it still did not work.

-It works if i do a direct send to the hard coded IP address of the client machine (ie not multicast).

Any help would be appreciated.

Simon
  • 33,714
  • 21
  • 133
  • 202

3 Answers3

8

Does your local network hardware support IGMP?

It's possible that your switch is multicast aware, but if IGMP is disabled it won't notice if any attached hardware subscribes to a particular multicast group so it wouldn't forward those packets.

To test this, temporarily connect two machines directly together with a cross-over cable. That should (AFAICR) always work.

Also, it should be the server half of the code that has the TTL argument supplied to JoinMulticastGroup(), not the client half.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
5

I've just spent 4 hours on something similar (I think), the solution for me was:

client.Client.Bind(new IPEndPoint(IPAddress.Any, SSDP_PORT));
client.JoinMulticastGroup(SSDP_IP,IP.ExternalIPAddresses.First());
client.MulticastLoopback = true;

Using a specific (first external) IP address on the multicast group.

  • 2
    I was having trouble getting it to work on LAN too. Without the second argument of an explicit IP address to listen on when calling JoinMulticastGroup(), it was just arbitrarily picking some virtual ethernet driver I had installed as part of Docker/Hyper-V and joining that to the multicast group instead of my real ethernet adapter. If you have more than one network adapter you'll want to specify the IP address to join to the multicast group explicitly with the method overload that takes an IP address as the second argument. – MEverett Nov 06 '17 at 23:27
  • 3
    What is `IP`? I don't see anyone else mentioning a variable named `IP` in this question, so your variable names are very confusing and seem to be missing some context. – Deantwo Oct 11 '19 at 08:50
0

I can't see a TTL specified anywhere in the code. Remember that TTL was originally meant to be in unit seconds, but is has become unit hops. This means that by using a clever TTL you could eliminate passing through the router. The default TTL on my machine is 32 - I think that should be more than adequate; but yours may actually be different (UdpClient.Ttl) if your system has been through any form of a security lockdown.

I can't recommend the TTL you need - as I personally need to do a lot of experimentation.

If that doesn't work, you could have a look at these articles:

All-in-all it looks like there has been success with using Sockets and not UdpClients.

Your chosen multicast group could also be local-only. Try another one.

Your physical network layer could also be causing issues. I would venture to question switches and direct (x-over) connections. Hubs and all more intelligent should handle them fine. I don't have any literature to back that, however.

Jonathan C Dickinson
  • 7,181
  • 4
  • 35
  • 46