Controlling UDP multicast output on a system with multiple NICs RRS feed

  • Question

  • I am using the UDP multicast sample code to send multicast UDP (WS-Discovery) messages.  With tiny mods, the sample works fine, and has been for months.  I have one problem.


    If there is more than NIC on the system, I can't seem to figure out how to control what NIC the messages go out on.


    I have tried SetSocketOption(SocketOptionLevel.IP,SocketOptionName.MulticastInterface,val) where val is the interface number (0 or 1).  But it does seem to have any effect, the message still goes out on the default NIC.


    Help! If you have a code sample of doing this, I would really appreciate it.


    Thanks in advance.



    Tuesday, July 29, 2008 1:38 AM


  • This is a known issue and we are thinking about this. For now the UDP multicast does not work with multiple NIC's


    Monday, August 4, 2008 11:42 PM

All replies

  • Hi,

     Instead of binding to nic-address1 or nic-address2 - could you try to bind to ?

    Tuesday, July 29, 2008 6:52 AM
  • This is a known issue and we are thinking about this. For now the UDP multicast does not work with multiple NIC's


    Monday, August 4, 2008 11:42 PM

    This is more of a System.Net question. Look around http://msdn.microsoft.com/en-us/library/system.net.networkinformation.aspx, maybe it has examples. I do have a sample. No guarantees, but it should give you some ideas of where to look.



    Code Snippet

    using System.Net;

    using System.Net.NetworkInformation;

    using System.Net.Sockets;



    Code Snippet

    // find the adapter you want

    NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();

    NetworkInterface multicastAdapter = null;

    foreach (NetworkInterface adapter in adapters)


    // use whatever criteria you want, right now we choose based on multicast support

    if (adapter.SupportsMulticast)


    multicastAdapter = adapter;




    if (multicastAdapter == null)


    throw new InvalidOperationException("Muticast NIC not found.");


    // lets say we are listening over IPv4. (i.e. multicastAdapter.Supports(NetworkInterfaceComponent.IPv4) returns true)

    IPInterfaceProperties properties = multicastAdapter.GetIPProperties();

    IPv4InterfaceProperties v4Properties = properties.GetIPv4Properties();

    if (v4Properties != null)


    int port = 3096; // whatever port you are binding to

    int interfaceIndex = v4Properties.Index;

    IPEndPoint localEndpoint = new IPEndPoint(IPAddress.Any, port);

    Socket listenSocket = new Socket(localEndpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);

    listenSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(interfaceIndex));


    IPAddress ipAddress = null; // whatever ip address you need

    MulticastOption multicastGroup = new MulticastOption(ipAddress, interfaceIndex);

    listenSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, multicastGroup);

    // use socket




    Tuesday, August 5, 2008 8:16 PM

    The problem is probably that you aren't converting the interface Index number from host byte order to network order.  If you look closely at the code that Margaret pasted above, it is calling IPAddress.HostToNetworkOrder on the value passed to the SetSocketOption call.
    Thursday, August 7, 2008 5:18 PM
  • Also, be careful not to call HostToNetworkOrder on the value passed to the

    MulticastOption when joining the multicast group.  It does not expect the interface index to be in network byte order.

    Thursday, August 7, 2008 5:20 PM