Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying what network interface an UDP multicast should go to in .NET

On a computer with both an active Wireless Card and a LAN-Port with a crossover cable hooked up to another machine running the same application, we need to send a UDP multicast over the LAN wire to the other computer. Using C# Sockets, Windows seems to try to route the message over the WLAN adapter every time.

Is there a way to specify what network interface to send a UDP multicast on?

like image 289
Markus Roth Avatar asked Feb 03 '10 14:02

Markus Roth


2 Answers

You are probably looking for SocketOptionName.MulticastInterface. Here's an article on MSDN that might help you.

Other then that if you update your local routing table to have an exact entry matching the multicast address and pointing to the right interface it should just work.

like image 154
Nikolai Fetissov Avatar answered Sep 19 '22 14:09

Nikolai Fetissov


Just as addendum to Nikolai answer: the problem with KB318911 is a dirty trick that user must provide necessary adapter index. While looking how to retrieve this adapter index I figured out such recipe:

NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
  IPInterfaceProperties ip_properties = adapter.GetIPProperties();
  if (!adapter.GetIPProperties().MulticastAddresses.Any())
    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

  // now we have adapter index as p.Index, let put it to socket option
  my_sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, (int)IPAddress.HostToNetworkOrder(p.Index));
}

Full note at http://windowsasusual.blogspot.ru/2013/01/socket-option-multicast-interface.html

like image 42
Yury Schkatula Avatar answered Sep 20 '22 14:09

Yury Schkatula