Good night, João
The way to do this could be by trying connections to the servers of your IP range on the chosen port. I don’t recommend adopting this idea, because you would need to know the machine’s IP (a machine can respond to multiple IP’s), the track within the network mask they use and iterate over a huge amount of IP’s to get that list. You would try the connection and, if there is a "Connection Refused" means that the other machine is not expecting to receive anything at that port.
A more interesting way would be for one of the servers to centralize the list with the IP/hostnames of the other game servers. Whenever a game server starts it would send a message to the centralizing server informing its current IP.
All lobbies then would always ask this centralizing server which game servers exist and are active, get the IP and then make the connection to the game.
With this technique you request in a colossal way and establishes a simple process to determine to which IP to connect.
It is a good solution, but for this project it is not feasible to use a central server. I did what this reply suggested. It works, but I can’t connect to a specific port.
– JLuann
This example only shows how to test the ability to reach the host. Jluann’s response has an example of a socket on a specific port.
– D. Melo