【问题标题】:Get the IpAddress on which we received an UDP Multicast request获取我们收到 UDP 多播请求的 IP 地址
【发布时间】:2013-03-04 13:33:31
【问题描述】:

我正在使用UdpClient 从多播组中读取数据。

它是这样配置的:

m_udpClientReceiver = new UdpClient();
m_receivingEndPoint = new IPEndPoint(IPAddress.Any, m_port);
m_udpClientReceiver.ExclusiveAddressUse = false;
m_udpClientReceiver.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
m_udpClientReceiver.ExclusiveAddressUse = false;
m_udpClientReceiver.Client.Bind(m_receivingEndPoint);
m_udpClientReceiver.JoinMulticastGroup(m_multicastAddress, 255);

我读了它:

Byte[] data = m_udpClientReceiver.Receive(ref m_receivingEndPoint);

我有几个网卡(两个 LAN,一个 wifi),绑定在不同的子网上。我需要知道在哪个网卡(实际上是哪个 ip)上收到了请求。

我怎样才能做到这一点?

谢谢!

【问题讨论】:

  • 我认为你可以在数据包数据/标题中读取它
  • ?在哪里?标头将包含多播地址
  • 用接收方ip地址计算的udp校验和si,所以它一定在某个地方
  • 嗨,也许这可以帮助你stackoverflow.com/questions/29890/…
  • @ChristopherCabezudoRodriguez :它不适用于同一子网络上的 wifi+LAN 网络。

标签: c# networking network-programming multicast udpclient


【解决方案1】:

作为替代方案,您是否考虑过不加入多播组?您可以使用标准的 UDPClient 类轻松地发送和接收多播数据包。即

UdpClient.Send(byte[] dgram, int bytes, IPEndPoint endPoint)

在哪里endPoint = new IPEndPoint(IPAddress.Broadcast, <port number>)。并且在接收时仍在使用:

Byte[] data = m_udpClientReceiver.Receive(ref m_receivingEndPoint);

现在在哪里正确设置了 m_receivingEndPoint?我刚刚对此进行了测试,效果很好。

【讨论】:

  • 我不是专家,但我猜如果这个方法存在,它必须做一些比接收方法更多的事情,不是吗?
【解决方案2】:

我终于使用BeginReceive 方法(异步),我将其绑定的 ip 作为上下文提供

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多