无法通过非默认NIC发送多播

Windows 7虚拟机上,我尝试使用第二个(非默认的)两个networking接口将UDP数据包发送到多播地址。 我可以使用/ INTF选项(不允许指定端口)使用mcast来实现此目的,但我的C#代码不起作用:

void run(string ipaddrstr, int port, string nicaddrstr) { int index = -1; // Create a socket for the UDP broadcast Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPAddress ipaddr = IPAddress.Parse(ipaddrstr); IPAddress nicAddr = IPAddress.Parse(nicaddrstr); int i = 0; foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) { if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet) { foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses) { if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { if (ip.Address.Equals(nicAddr)) { index = i; break; } } } if (index != -1) { break; } i++; } if (index == -1) { Console.Error.WriteLine("Couldn't find NIC with IP address '" + nicaddrstr + "'"); return; } socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ipaddr, nicAddr)); int multicastInterfaceIndex = (int)IPAddress.HostToNetworkOrder(index); socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, multicastInterfaceIndex); } socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 1); IPEndPoint endpoint = new IPEndPoint(ipaddr, port); socket.Connect(endpoint); // At this point, data can be send to the socket } 

当我将nicaddrstr指定为默认的networking接口IP地址时,数据按照预期在该接口上stream动。 但是,如果我将nicaddrstr指定为第二个(非默认)networking接口IP地址,则不会有任何数据stream(由Wiresharkvalidation),即使在任何函数调用中都没有发生错误。 有人能告诉我mcast正在做什么,允许非默认网卡接受UDP数据?

我尝试过路由表中的路由设置的各种组合,但内容似乎不影响此代码的行为。