您好,我的PC上有两个网络适配器,希望将udp多播发送到所选网络接口上的组239.0.0.222端口9050。但它只适用于第一个接口,当选择另一个NIC时,不会发送数据。
localIP是来自所选适配器的本地Ip
发送者代码:
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:
适配器本地IP代码:
NetworkInterface.GetAllNetworkInterfaces()[adapterIndex].GetIPProperties().UnicastAddresses[0].Address;EDIT2,5:
我还使用相同的reuslt Wireshark显示了第二个适配器上多播组的正确加入
udpclient.JoinMulticastGroup(multicastaddress);
udpclient.Client.Bind(remoteep);EDIT3:
我现在尝试在另一台PC上尝试,但同样的问题再次发生,Adapter1运行,在所有其他计算机上没有发送任何东西。
我尝试过的另一件事是,在windows xp配置中切换前两个适配器的顺序,然后再一次,新的第一个适配器可以工作,但新的第二个适配器不发送任何消息。
发布于 2014-04-01 22:46:05
默认情况下,只有第一个适配器加入给定的多播组。从操作系统的角度来看,这是绝对相关的,因为无论适配器如何使用多播流,组都会提供相同的内容。如果您计划在每个适配器上侦听多播,则必须遍历它们,并在每个适配器上放置适当的套接字选项:
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));
}附注:是的,我是@lukebuehler提到的“这个人”:http://windowsasusual.blogspot.ru/2013/01/socket-option-multicast-interface.html
发布于 2013-07-06 04:07:12
我认为这个人有答案,你必须迭代网络接口并找到支持多播的接口。
http://windowsasusual.blogspot.ru/2013/01/socket-option-multicast-interface.html
https://stackoverflow.com/questions/13604757
复制相似问题