通过组播组客户端接收UDP数据包的源

时间:2012-12-11 03:49:34

标签: c# udp

  

可能重复:
  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);
        }
    }

0 个答案:

没有答案