I’m trying to build a netowrk app. I have succesfully made a server that sends and receives packages. So far, testing has been done on one computer pointing to 127.0.0.1 (No place like home). Now I want to switch to the network. How can I find computers on a LAN network that are listening to my specific port?
Share
The service will need to listen for broadcast messages on a known port (if you want to be really well behaved you can register the program and port number with the IANA), when it hears a broadcast message it replies to the sender the server’s IP and what port the service is listening for incoming connections on.
Here is a simple example from the link above, this just prints to the console who connected and on what port, but you can use this information to establish a TCP or UDP connection between the two endpoints.
As a psudo example here is the sequence of events on how I would do it.
For this example lets say we have a network with a IP of
192.168.1.0and a subnet of255.255.255.0. We have two servers,Server1at192.168.1.2with the service listening on port1234, andServer2at192.168.1.3with a port of4567for the service. Both are listing on port3000for broadcast messages. The client connecting will be at the IP192.168.1.4192.168.1.255:3000) using the same port to send as he is listening on. He sends some kind of payload so the servers only send back to your clients, instead of someone else who happened to use the same port as you. (lets say it sends the stringSend me your info for XYZ app!)Send me your info for XYZ app!and sends the UDP messageName:Server1 IP:192.168.1.2 Port:1234back to the senders source port and IP combination (192.168.1.4:50123)Send me your info for XYZ app!and sends the UDP messageName:Server2 IP:192.168.1.3 Port:4567message back to the senders source port and IP combination (192.168.1.4:50123)