How to know which servers are waiting for connection on my network?

Asked

Viewed 240 times

2

I’m developing a multiplayer (LAN) game with Sockets in JAVA. Players will start a lobby, and in this lobby there will be a list of servers (game rooms) waiting for connections.

How can I list the servers that are on my network so that players can connect?

3 answers

1

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.

  • 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.

1

I managed to solve my problem by making a few edits in a example that I used as a basis.

Code:

public void checkHosts(String subnet) throws IOException {
    for (int i = 1; i < 255; i++) {
        String host = subnet + "." + i;

        SocketAddress sockaddr = new InetSocketAddress(host, 3128);
        Socket socket = new Socket();
        boolean connected = false;

        try {
            socket.connect(sockaddr, timeout);
            connected = true;
        } catch (SocketTimeoutException ex) {
            System.out.println(host + " isn't reachable");
        } catch (ConnectException ex) {
            System.out.println(host + " is reachable, but hasn't server on port 3128");
        }

        if (connected) {
            System.out.println(host + " has a server on port 3128!!!");
        }

        socket.close();
    }
}

0

You can try to list the IP of the servers in that lobby. To get the IP of a server you can use the following function:

public String getIp() {

  InetAddress ip;

  try {

    ip = InetAddress.getLocalHost();

    return ip.getHostAddress();

  } catch(UnknownHostException ex) {}

  return "IP não encontrado!";

}

Try passing this string from the server to the lobby every time a new server is created. And when it’s closed, you have to remember to take the IP out of the lobby as well.

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.