获取具有receiveFrom的多播数据包的源IP

本文关键字:数据包 IP 多播 获取 receiveFrom | 更新日期: 2023-09-27 18:29:05

祝一切顺利

问题:

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

我尝试过的:

我尝试了这些网站中显示的方法,不确定我是否正确实现了它,但所有这些都返回了相同的IP,即0.0.0.0,这篇文章和这篇

这两个链接都指使用socket.receiveFrom()或socket。BeginRecieveMessageFrom()而不是socket.receive()

        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");
            }
        }

获取具有receiveFrom的多播数据包的源IP

您应该使用同步ReceiveMessageFrom调用或在调用异步BeginReceiveMessageFrom 后调用EndReceiveMessageFrom

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地址