可能重复:
How do I determine the source IP of a multicast packet in C#?
在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);
}
}