我正在努力让 Multicast 在 OSX 主机和 Win7 来宾的虚拟机下工作。我的多播代码通常在 Windows7 机器的真实网络上运行良好,但我需要能够远离它进行开发。
我读到 VB 可能正在创建其他接口,其中一个接口正在获取无法重复的多播数据包,因此我编写了以下代码来尝试处理该问题,但在 InternalNetwork 或 HostOnly 下的 VB 中仍然失败。
我是否需要通过物理连接到网络(而不是 wifi)才能工作?还是我还在做其他错事?
编辑:稍微简化了我的代码,仍然不起作用。
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
// Get netowrk info
int defaultPort = 5050;
string localName = Dns.GetHostName();
IPHostEntry hostEntry = new IPHostEntry();
hostEntry = Dns.GetHostByName(localName);
IPAddress localAddress = hostEntry.AddressList[0];
// create a socket pair for every interafce
for(int i=0;i<nics.Length;i++)
{
#region Make Outgoing Socket on Interface
if (!nics[i].SupportsMulticast)
{
continue; // skip this one
}
Console.WriteLine("Adding socket to nic: " + nics[i].Name);
// output interface
Socket mcastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// Bind the socket to default IP address and port.
mcastSocket.Bind(new IPEndPoint(localAddress,4568+i));
// Select Adapter for outgoing Multicast packets );
int optionValue = (int)IPAddress.HostToNetworkOrder(i);
// Multicast Address - To add membership : ");
IPAddress mcastAddress = IPAddress.Parse("224.5.6.7");
// Port number - Where Multicast members are listening : ");
int mcastPort = 4567;
MulticastOption mcastOpt = new MulticastOption(mcastAddress,localAddress);
// Add membership to the group.
mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOpt);
// Set the required interface for outgoing multicast packets.
mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, optionValue);
mcastSocket.Connect(new IPEndPoint(mcastAddress,4567));
// add to transmission list
transmissionList.Add(mcastSocket);
#endregion
}
#region Make Incoming Socket on Interface
// make socket to read for incoming multicast
Socket inSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
inSocket.Bind(new IPEndPoint(localAddress, 4567));
inSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(multicastIpAddr));
receptionList.Add(inSocket);