如何在OSX上的VirtualBox Guest OS中使用Multicast

时间:2013-04-25 16:31:48

标签: c# udp virtualbox multicast

我试图让我的头发试图让Multicast在OSX主机和Win7客户端的虚拟盒子下工作。我的多播代码一般在Windows7机器的真实网络上运行良好,但我需要能够远离它进行开发。

我读到有可能VB正在创建额外的接口,其中一个是我的组播数据包无法重复它,所以我编写了下面的代码来尝试处理它,但它仍然在IntraNetwork下的VB中失败或采用HostOnly。

我是否需要通过物理连接到网络(而不是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);

1 个答案:

答案 0 :(得分:1)

摘自上述博客

可能还有其他方法,并非所有这些步骤都可能是必要的,但这对我有用:

  1. 从Oracle VM VirtualBox下载并安装最新的VirtualBox,也下载并安装Oracle VirtualBox扩展包(可能没有必要,但我做了。)

  2. 将Win7的副本安装到VirtualBox VM中。

  3. 克隆VM以创建第二台计算机。 非常重要:克隆以与其父级相同的MAC地址开始。这让我搞砸了很久。进入网络设置 - >高级克隆并请求通过按MAC旁边的圆形箭头为其分配新的MAC。 (这仅在虚拟机关闭时才有效。)

  4. 将两台计算机的网络类型设置设置为“内部网络”。 (请注意,这会将它们与您的主机和互联网隔离开来。)

  5. 在主机的命令行上,启用dhcp(这也可能不是绝对必要的,但这就是我所做的。作为替代方案,您也可以为两台机器手动配置IP)。假设您使用默认的内部网络名称“intnet”,您可以这样做(全部在一行):
    dhcpserver add --netname intnet --ip 10.10.10.1 --netmask 255.255.255.0 --enable --lowerip 10.10.10.10 --upperip 10.10.10.128

  6. 非常重要:我 在VirtualBox创建的环境中,有多个声称具有多播功能的NIC。然而,他们在说谎。此外,由于VirtualBox分配指标的方式,您的传出套接字将被分配给一个骗子,他将愉快地吞噬您的多播消息而不发送它们。 解决方案是在声称支持多播的每个NIC上专门打开一个传出套接字,并将传出的数据包发送给所有这些套接字。下面是我打开套接字并列表的代码。发送数据包时,只需在列表中进行操作即可。

    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];
       for(int i=0;i<nics.Length;i++){
                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);
            }