c# - 使用recieveFrom获取组播数据包的源IP

时间:2016-01-05 14:45:02

标签: c# sockets networking

美好的一天

问题:

我正在尝试获取组播数据包的源IP,但我得到的只是0.0.0.0:80

我尝试了什么:

我尝试了这些网站中显示的方法,不确定我是否正确实现了它,但都返回了相同的IP,即0.0.0.0,this postthis one

这两个链接都指的是使用socket.recieveFrom()或socket.BeginRecieveMessageFrom()而不是socket.recieve()

        private void recieveText()
        {
            //initialise multicast group and bind to interface
            Socket _listener_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, _PORT);
            _listener_socket.Bind(ipep);
            IPAddress localip = IPAddress.Parse("224.5.6.7");
            _listener_socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(localip, IPAddress.Any));

            //recieve data to multicast group
            while (_listener_socket.IsBound)
            {
                updateLabel("listening...");
                byte[] b = new byte[1024];
                updateLabel("message recieved");
                updateRedBox("\n---------------------------------\n New Message :\n");
                EndPoint IPEPoint = (EndPoint)ipep;
                _listener_socket.BeginReceiveMessageFrom(b, 0, b.Length, 0, ref IPEPoint, null, null);
                updateRedBox(IPEPoint.ToString());
                char[] chars = new char[b.Length / sizeof(char)];
                System.Buffer.BlockCopy(b, 0, chars, 0, b.Length);

                string t = new string(chars).Trim();
                updateRedBox(t);
                updateRedBox("\n----------------------------------\n");
            }
        }

1 个答案:

答案 0 :(得分:1)

您应该在调用异步ReceiveMessageFrom

之后使用同步EndReceiveMessageFrom来电或致电BeginReceiveMessageFrom
private void recieveText()
{
    //initialise multicast group and bind to interface
    Socket _listener_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    IPEndPoint ipep = new IPEndPoint(IPAddress.Any, _PORT);
    _listener_socket.Bind(ipep);
    IPAddress localip = IPAddress.Parse("224.5.6.7");
    _listener_socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(localip, IPAddress.Any));

    //recieve data to multicast group
    while (_listener_socket.IsBound)
    {
        updateLabel("listening...");
        byte[] b = new byte[1024];
        updateLabel("message recieved");
        updateRedBox("\n---------------------------------\n New Message :\n");
        EndPoint IPEPoint = (EndPoint)ipep;
        var res = _listener_socket.BeginReceiveMessageFrom(b, 0, b.Length, 0, ref IPEPoint, null, null);
        SocketFlags flags = SocketFlags.None;
        IPPacketInformation packetInfo;
        _listener_socket.EndReceiveMessageFrom(res, ref flags, ref IPEPoint, out packetInfo);
        updateRedBox(IPEPoint.ToString());
        char[] chars = new char[b.Length / sizeof(char)];
        System.Buffer.BlockCopy(b, 0, chars, 0, b.Length);

        string t = new string(chars).Trim();
        updateRedBox(t);
        updateRedBox("\n----------------------------------\n");
    }
}

查看我添加的BeginReceiveMessageFrom后面的3行。除了远程IP地址之外,您还可以使用这些标志来确定此消息是否作为多播消息接收,并且可以从packetInfo获取多播组IP地址

相关问题