Question

I am trying to broadcast a UDP datagram to a multicast address on the local network. This works perfectly fine across dozens of machines, except for one specific machine.
This particular machine is able to receive datagrams from the multicast address, but is not able to send messages.

This is the code I am using:

using (UdpClient client = new UdpClient())
{
    client.Send(bytes, bytes.Length, remoteEP);
    client.Client.Shutdown(SocketShutdown.Both);
    client.Client.Close();
}

where remoteEP is the IP address and port of the multicast group, and bytes is valid data.

  • There is no exception being raised, the message simply is not delivered.
  • The message is received in the loopback on the same machine from 127.0.0.1.
  • The message is not shown in Wireshark outgoing traffic.
  • The machine is the only one in the network which is running Windows 8.
  • The Windows firewall is disabled.
  • The machine is in the same subnet as the listening machines.
  • There is only one active network interface on this machine.
  • I've tried:
    • client.BroadcastEnabled = true;.
    • Joining the multicast group on the client side.
    • using BeginSend instead of Send.

Any ideas for debugging are welcome.

Was it helpful?

Solution

Ironically, after searching for a solution all day, I find it 2 minutes after posting an SO question.

The method in this article helped. I guess there were network interfaces I was not aware of.

This is the revised code:

using (UdpClient client = new UdpClient())
{
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkInterface adapter in nics)
    {
        IPInterfaceProperties ip_properties = adapter.GetIPProperties();
        if (adapter.GetIPProperties().MulticastAddresses.Count == 0)
            continue; // most of VPN adapters will be skipped
        if (!adapter.SupportsMulticast)
            continue; // multicast is meaningless for this type of connection
        if (OperationalStatus.Up != adapter.OperationalStatus)
            continue; // this adapter is off or not connected
        IPv4InterfaceProperties p = adapter.GetIPProperties().GetIPv4Properties();
        if (null == p)
            continue; // IPv4 is not configured on this adapter
        client.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, (int)IPAddress.HostToNetworkOrder(p.Index));
        break;
    }

    client.Send(bytes, bytes.Length, remoteEP);
    client.Client.Shutdown(SocketShutdown.Both);
    client.Client.Close();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top