【问题标题】:Receiving UDP Multicast Messages on a Multihomed Windows 10 Machine在多宿主 Windows 10 计算机上接收 UDP 多播消息
【发布时间】:2018-08-15 14:24:11
【问题描述】:

我有一个 C# .NET 客户端,它监听 UDP 多播消息。我需要在单个网络接口上接收消息。有时我不会看到收到的消息。当我禁用其他接口时,它可以工作。

我尝试使用此站点上类似问题的代码将套接字选项设置为特定接口,但是,我不确定这是否只会影响发送多播消息而不影响接收它们?

经过我的研究,我发现路由表会导致这种行为,一种解决方案是更改路由表,但我不想走那条路。

在所有接口上加入多播组更好吗?我将如何使用 UdpClient 来做到这一点。

这是用于设置我的 UdpClient 的代码:

获取接口:

public static IEnumerable<NetworkInterface> GetAvailableMulticastInterfaces()
{
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    List<NetworkInterface> availableInterfaces = new List<NetworkInterface>();

    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

        availableInterfaces.Add(adapter);
    }

    return availableInterfaces;
}

设置界面:

NetworkInterface networkInterface = Common.Utilities.Network.GetAvailableMulticastInterfaces().Where(nic => nic.Id == attributes.SelectedNetworkInterfaceId).FirstOrDefault();

获取接口索引:

networkInterfaceIndex  = (int)IPAddress.HostToNetworkOrder(networkInterface.GetIPProperties().GetIPv4Properties().Index);

绑定 UdpClient:

public void Listen(string multicastGroupAddress, int port, int? networkInterfaceIndex = null)
{
    IpAddress = multicastGroupAddress;
    Port = port;

    _multicastEndPoint = new IPEndPoint(IPAddress.Parse(multicastGroupAddress), port);

    Listener = new UdpClient();
    Listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

    if (networkInterfaceIndex != null)
        Listener.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, networkInterfaceIndex.Value);

    var localEndpoint = new IPEndPoint(IPAddress.Any, port);
    Listener.Client.Bind(localEndpoint);
    Listener.JoinMulticastGroup(IPAddress.Parse(multicastGroupAddress));

    while (true)
        Process();
}

【问题讨论】:

    标签: c# .net windows networking network-programming


    【解决方案1】:

    好的,所以我回到了这个并找到了正确的操作。一个人必须加入多个接口使用

    var availableMulticastNics = Utils.GetAvailableMulticastInterfaces();
    var listener = new UdpClient();
    listener.Client.Bind(new IPEndPoint(IPAddress.Any, endpoint.Port));
    
    foreach (var ni in availableMulticastNics)
    {
        listener.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(multicastEndPoint.Address, ni.GetIPProperties().GetIPv4Properties().Index));
    }
    // Ready to read socket
    

    而不是listener.JoinMulticastGroup(ni.GetIPProperties().GetIPv4Properties().Index, multicastEndPoint.Address)。否则,我会收到一个 SocketException,告诉我无效的参数,如果可以的话,有人可以解释一下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 2012-06-05
      • 2010-09-08
      • 2018-11-07
      相关资源
      最近更新 更多