3

Good day all

Problem:

I am trying to get the source IP of multicast packets, however all that I get is 0.0.0.0:80

What I have tried:

I tried methods shown in these sites, not sure if I correctly implemented it, but all return the same IP which is 0.0.0.0, this post and this one

Both links refer to using socket.recieveFrom() or socket.BeginRecieveMessageFrom() instead of 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");
            }
        }
Community
  • 1
  • 1
CybeX
  • 2,060
  • 3
  • 48
  • 115
  • 2
    `BeginReceiveMessageFrom` is an *async* operation. At the point when it returns, it's not necessarily done *anything*. If you need async, wait until it's complete before accessing that value. Otherwise, switch to the synchronous `ReceiveMessageFrom`. Not saying that'll fix your issue, but it's an obvious fault with the current code. – Damien_The_Unbeliever Jan 05 '16 at 15:03
  • @Damien_The_Unbeliever thanks for the tip, will change it :p, when would be an appropriate time to use the BeginRecieveMessageFrom()? – CybeX Jan 05 '16 at 15:31

1 Answers1

1

You should use the synchronous ReceiveMessageFrom call or call EndReceiveMessageFrom after calling the asynchronous 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");
    }
}

Look at the 3 lines following BeginReceiveMessageFrom that I have added. In addition to the Remote IP Address, you can use the flags to find out whether this message was received as a Multicast message and the multicast group IP Address can be obtained from packetInfo

Vikhram
  • 4,294
  • 1
  • 20
  • 32
  • You my good sir, are a a genius. I had a look into the async recievefrom but I was not sure about the callback, what exactly does the "callback" do, what is it's purpose (and what should generally be contained in the callback)? – CybeX Jan 12 '16 at 07:14
  • @KGCybeX You should read-up on [Asynchronous Programming Model](https://msdn.microsoft.com/en-us/library/ms228963(v=vs.110).aspx). The basic ideas is this: BeginXXX calls start an asynchronous operation. To be notified when the operation terminates, you should register using the callback delegate. Once you can no longer proceed asynchronously, you call the EndXXX method to potentially block and then retrieve the results of the operation XXX – Vikhram Jan 12 '16 at 13:14