Search code examples
javasocketsserverconnectionclient

Java Server Socket connection over different routers


I am currently developing a client and a server for a small game. The client which connects to the server establishes the connection with this method:

// This method is called, passing on an ipv6 address and port number 6666
        public void startConnection(String ip, int port) throws IOException {
            try {
                clientSocket = new Socket(ip, port);
                out = new PrintWriter(clientSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                //some other code handling responses
            } catch (IOException e) {
                LOG.debug("Error when initializing connection", e);
                throw new IOException();
            }

        }

The Server I built accepts connections using this method:

 public void start(int port) {
        try {
            serverSocket = new ServerSocket(port); //port = 6666
//This part is used to handle multiple connections at once
        while (b){
            try {
                map.add(new EchoClientHandler(serverSocket.accept())); //EchoClientHandler is a class used to send and receive data instructions 
                x = map.size() - 1;
                System.out.println("Establishing connection from port " + port);
                map.get(x).start();
                System.out.println("Connection established");
            } catch (SocketTimeoutException e) {
                
            }
        }
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }

Both methods work fine and establish a stable connection between the client and the server, but when i try and establish a connection from different routers or general internet connections (like via cellular data) it doesn't work. Is there a way to establish connections without both the client and the server having to connect from the same router?

Edit: Here is the error i get from the client, the server doesn't show anything:

18:03:24.288 [AWT-EventQueue-0] DEBUG dorusblanken.werwolfclient.Client - Error when initializing connection

java.net.SocketException: Network is unreachable: connect

Solution

  • "Network is unreachable" means there is no way to get to the destination network from the current network.

    You mentioned that you are trying to establish a connection via the Internet. For that to work, the destination host (your server) must be connected to the Internet, it must have a public IP address, and the clients need to use the public IP address when connecting.

    That is the simplest configuration. Most companies don't actually put their servers directly on the Internet. Instead, the public IP frequently belongs to a CDN or DDoS mitigation layer, which forwards connections to a load balancer, and the load balancer forwards connections to servers.