0
votes

Hello i got 2 Networkadapters on my PC and want to send udp multicasts to group 239.0.0.222 Port 9050 on the selected Network interface. But it only works with the first interface, when choosing another NIC no data is sent.

The localIP is the local Ip from the selected adapter

The senders code:

 IPAddress localIP = getLocalIpAddress();
 IPAddress multicastaddress = IPAddress.Parse("239.0.0.222");
 IPEndPoint remoteep = new IPEndPoint(multicastaddress, 9050);
 UdpClient udpclient = new UdpClient(9050);
 MulticastOption mcastOpt = new MulticastOption(multicastaddress,localIP);

 udpclient.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOpt);
 udpclient.Send(data, data.Length, remoteep);

EDIT1:
Code for adapters local IP:

NetworkInterface.GetAllNetworkInterfaces()[adapterIndex].GetIPProperties().UnicastAddresses[0].Address;

EDIT2,5:
Also tried both of with same reuslt Wireshark displays me the correct join of the multicast group on the second adapter

udpclient.JoinMulticastGroup(multicastaddress);
udpclient.Client.Bind(remoteep);

EDIT3:
I now tried on another PC but the same problem happens again, Adapter1 runs, on all others nothing is sent.
Another thing i tried out, is to switch the order of the first two adapters in the windows xp config, then again the new first adapter works but the new second sends nothing.

2
Show us your code for getLocalIpAddress()Anthill
added the code. Its the estimated ipAddress btw.Gobliins
Just to note: udpClient has JoinMulticastGroup method.L.B
Hard to determine what you're doing wrong. You don't show us the criteria by which you choose the adapterIndex. Since there may be more than one UnicastAddress per interface you need to ensure that the address family of the UnicastAddress is compatible with InterNetwork since you seem to be using IPV4.Anthill
How are you determining that you're sending to the wrong network interface? Since you say you're binding your udp client to the right local endpoint why do you mention binding to the wrong interface?Anthill

2 Answers

1
votes

By default, only first adapter joins to given multicast group. From OS perspective, it's absolutely relevant because the group would provide the same content whatever adapter consume the multicast stream. If you plan to listen the multicast on each of your adapters, you have to iterate over them and place appropriate socket option on each:

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
  my_sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, (int)IPAddress.HostToNetworkOrder(p.Index));
}

P.S. Yep, I'm "this guy" mentioned by @lukebuehler: http://windowsasusual.blogspot.ru/2013/01/socket-option-multicast-interface.html

1
votes

I think this guy has the answer, you have to iterate over the network interfaces and find the one that supports multicast.

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