0

I'm using a Raspberry Pi as a server in our network with IP: 172.20.X.X/16

The plan is to send a broadcast-message to the port 15555. The Server will receive the message and send me his eth0-IP.

As I'm not the best in networking, I have problems to make a script that sends the IP to the broadcaster, and especially let this script get executed when the Server receives anything on port 15555

I've read some things about the command "nc" but I don't really get if that solves my problem:

https://stackoverflow.com/a/4739260/11397900

So at the moment I have 2 scripts:

Script 1 contains

#!/bin/bash

while :
do
    nc -k -l 15555 > sendIP.sh
    sleep 1
done

Script 2 is called sendIP and contains atm

#!/bin/bash

echo "Success"

Last but not least, my C#-Code to send a broadcast is this one:

private static string getServerIP(int port)
    {
        string IPString = String.Empty;

        IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse("172.20.255.255"), port);
        byte[] messageInbytes = Encoding.ASCII.GetBytes("Hallo");

        UdpClient udpClient = new UdpClient();

        udpClient.Send(messageInbytes, messageInbytes.Length, iPEndPoint);
        udpClient.Client.ReceiveTimeout = 1000;
        while (true)
        {
            try
            {
                var recvBuffer = udpClient.Receive(ref iPEndPoint);
                break;
            }
            catch
            {

            }
        }

        return iPEndPoint.Address.ToString();
    }

The expectation is, that on the Raspberry, there will stand "Success" in the console, and the c#- program finishes, but in reality, the Program won't get a response

DudeWhoWantsToLearn
  • 751
  • 2
  • 10
  • 29
  • this seems like a very unique solution to a problem you did not describe. I guearantee you if you tell us what you want to achive on a bigger scale we can help you out – Denis Schaf Jun 18 '19 at 09:45
  • also to debug your issue install wireshark on all your devices and see if the packages actually arrive otherwise you gonna have a hard time debugging and bugfixing – Denis Schaf Jun 18 '19 at 09:49
  • why not just open a connection on that port to all possible IP addresses in the subnet (except your own, duh) in parallel? Set timeout to something like 200 ms and you are set. – Steffen Winkler Jun 18 '19 at 11:55
  • That is a misuse of broadcast. We have multicast for that. Broadcast interrupts every host on the LAN, but multicast only interrupts the hosts listening to the particular multicast group. In fact, IPv6 has eliminated broadcast, so you would not be able to use IPv6 with this solution, and many businesses will not approve an application that uses broadcast in such a way. – Ron Maupin Jun 18 '19 at 12:41
  • @DenisSchaf Generally I want to find out the unknown IP in my network. Let's say the Raspberry has the IP "172.20.31.54", then I want to "shout" into the network "Every host who listens on Port 15555: Give me your IP". Then the Raspberry gets the message and sends back "172.20.31.54". But I'm not very familliar with networking, so I don't know how to send a message to this Port or make the Pi listening on this Port – DudeWhoWantsToLearn Jun 18 '19 at 13:28
  • As soon as the pi gets active in the network you should have him in your arp table. Try reading about arp/rarp as your mac is fixed you will be able to find the pi using this protocol without implementing you own wonky solution – Denis Schaf Jun 18 '19 at 15:23

0 Answers0