2

I am working on a Device finder at the moment and i am really struggeling with the code because normally i write microcontroller code.

I hope somebody could help me out.

The goal is to receive the client IP from an UDP Broadcast to a manufacturer specific port. This is working fine for me. I send the Broadcast and i got back the data send from Device:

00-00-00-F7-00-20-A0-06-58-39-30-12-63-16-00-00-62-A7-52-0B-FF-00-00-00-00-80-A3-BE-2F-XX

Its include the MAC Adress of the device (last 6 HEX snippets)

But i cant find the Senders(Client) IP address

How can i save the whole packedge send from the client? including the IP? Because in Wireshark i see the whole package (Send from: 192.xxx...)

Thank you so much! Best regards

My code is:


Byte[] data = { 0x00, 0x00, 0x00, 0xF6 };

        //string s1 = Encoding.UTF8.GetString(data);

        int port = 30718;
        string Antwort;

        // Socket definieren
        Socket bcSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        //bcSocket.Connect(new IPEndPoint(IPAddress.Broadcast, port));

        // EndPoint definieren bzw. Ziel des Broadcastes
        IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, port);

        // Optionen auf den Socket binden
        bcSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);

        // Broadcast senden
        bcSocket.SendTo(data, iep);

        bcSocket.ReceiveTimeout = 5000;
        byte[] test = new byte[1024];
        bcSocket.Receive(test);

        Antwort = System.Text.Encoding.Default.GetString(test).Trim(new char[] { '\0' });
        textBox_IPAdresse.Text = Antwort;
        string antworthex = BitConverter.ToString(test);
        textBox1.Text = antworthex;


        // Socket schliessen, nach erfolgreichem Senden des Broadcastes
        bcSocket.Close();

2 Answers2

1

You have to use .ReceiveFrom()

    byte[] test = new byte[1024];
    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
    EndPoint senderRemote = (EndPoint)sender;
    bcSocket.Receive(test, ref senderRemote);

Now the remote IP is available in the sender variable.

nos
  • 223,662
  • 58
  • 417
  • 506
  • One more question, is there any variable to see how many answers are incoming? Because we have more than one device of this type in our network. And i want to separate them, to get all answers. Regards! – NetflixAndChill Feb 09 '18 at 08:49
1

Its 100% working! You saved my day. Thanks!

Here the Code for those who search:

(I use it to find Lantronix Xport Device in my Network and get his IP)


Byte[] data = { 0x00, 0x00, 0x00, 0xF6 };

        //string s1 = Encoding.UTF8.GetString(data);

        int port = 30718;
        string Antwort;

        // Socket definieren
        Socket bcSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        //bcSocket.Connect(new IPEndPoint(IPAddress.Broadcast, port));

        // EndPoint definieren bzw. Ziel des Broadcastes
        IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, port);

        // Optionen auf den Socket binden
        bcSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);

        // Broadcast senden
        bcSocket.SendTo(data, iep);



        bcSocket.ReceiveTimeout = 5000;
        byte[] test = new byte[1024];
        IPEndPoint _sender = new IPEndPoint(IPAddress.Any, 0);
        EndPoint senderRemote = (EndPoint)_sender;
        bcSocket.ReceiveFrom(test, ref senderRemote); //IP steht in senderRemote! / IP is in senderRemote!



        Antwort = System.Text.Encoding.Default.GetString(test).Trim(new char[] { '\0' });
        textBox_IPAdresse.Text = senderRemote.ToString(); //Ausgabe der RemoteIP
        string antworthex = BitConverter.ToString(test);
        textBox1.Text = antworthex;



        // Socket schliessen, nach erfolgreichem Senden des Broadcastes
        bcSocket.Close();