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.
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