1

可能重复:
如何在 C# 中确定多播数据包的源 IP?

在 C# 客户端中,我正在接收发送到多播组的数据包,如何获取发送该数据包到多播组的源的 IP 地址?

字节数据很好,所以代码有效,但我需要找到源IP地址..

  Byte[] data = client.Receive(ref localEp);

完整的代码片段如下所示

        UdpClient client = new UdpClient();
        client.ExclusiveAddressUse = false;
        IPEndPoint localEp = new IPEndPoint(IPAddress.Any, 4446);
        client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);            
        client.ExclusiveAddressUse = false;
        client.Client.Bind(localEp);    
        IPAddress multicastaddress = IPAddress.Parse("230.0.0.1");            
        client.JoinMulticastGroup(multicastaddress);
        Console.WriteLine("Listening this will never quit so you will need to ctrl-c it");

        while (true)
        {
            Byte[] data = client.Receive(ref localEp);
            string strData = Encoding.ASCII.GetString(data);
            Console.WriteLine(strData);
        }
    }
4

0 回答 0