首页 文章

UDP多播问题疑难解答

提问于
浏览
0

我试图将UDP数据报广播到本地网络上的多播地址 . 除了一台特定的机器外,这在几十台机器上运行得非常好 .
这个特定的机器 is able 从多播地址接收数据报,但是不能发送消息 .

这是我正在使用的代码:

using (UdpClient client = new UdpClient())
{
    client.Send(bytes, bytes.Length, remoteEP);
    client.Client.Shutdown(SocketShutdown.Both);
    client.Client.Close();
}

其中 remoteEP 是组播组的IP地址和端口, bytes 是有效数据 .

  • 没有引发异常,消息根本没有传递 .

  • 127.0.0.1 在同一台机器上的环回中收到消息 is .

  • 消息是在Wireshark传出流量中显示的 not .

  • 该计算机是网络中唯一运行Windows 8的计算机 .

  • Windows防火墙已禁用 .

  • 机器与监听机器位于同一子网中 .

  • 此计算机上只有一个活动网络接口 .

  • 我试过了:

  • client.BroadcastEnabled = true; .

  • 在客户端加入组播组 .

  • 使用 BeginSend 而不是 Send .

欢迎任何调试的想法 .

1 回答

  • 0

    具有讽刺意味的是,在搜索了一整天的解决方案后,我发现了SO问题后2分钟 .

    this文章中的方法有所帮助 . 我想有一些我不知道的网络接口 .

    这是修改后的代码:

    using (UdpClient client = new UdpClient())
    {
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface adapter in nics)
        {
            IPInterfaceProperties ip_properties = adapter.GetIPProperties();
            if (adapter.GetIPProperties().MulticastAddresses.Count == 0)
                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
            client.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, (int)IPAddress.HostToNetworkOrder(p.Index));
            break;
        }
    
        client.Send(bytes, bytes.Length, remoteEP);
        client.Client.Shutdown(SocketShutdown.Both);
        client.Client.Close();
    }
    

相关问题