0

I am trying to create a multiplayer java game and have decided to use a java socket server. The "client" is able to connect and communicate to the server flawlessly when they are both ran on my computer, but when I send the client file to another computer (that is connected to the same network it) is unable to connect to the server, and I can't figure out why. I ran cmd and used to the code netstat -a and was able to confirm that the server is listening on 127.0.0.1:3251 so I believe the problem is with the client.

This is how the server is created:

    try {
        this.serverSocket = new ServerSocket();
        this.serverSocket.bind(new InetSocketAddress("localhost",port)); //port is 3251
        window.show("SERVER: " + "Created On Port: " + port);
    } catch (IOException e) {
        window.show("SERVER: " + e.getMessage());
        window.show("SERVER: " + "Unable To Create Server :(");
    }

and this is how the client connects:

    try {
        socket = new Socket("localhost", 3251);
    } catch (IOException e) {
        e.printStackTrace();
    }

When the server accepts a connection it creates a new thread:

        Socket socket = this.serverSocket.accept();
        ServerThread serverThread = new ServerThread(socket);
        serverThread.start();

2 Answers2

2
    try {
    socket = new Socket("localhost", 3251);
} catch (IOException e) {
    e.printStackTrace();
}

"localhost" means your client machine is looking for the server on the same computer. This should be the server ip i.e. 192.168.0.2 or something.

Nick Eu
  • 61
  • 3
  • thank you very much! Changing "localhost" to my ipv4 address fixed everything. –  Sep 16 '18 at 13:17
2

You have two problems:

  1. On the server you are creating a this.serverSocket.bind(new InetSocketAddress("localhost",port));. This creates a server socket on the IP address that localhost resolves to. This can be a problem. On most hosts the localhost will resolve to 127.0.0.1 a loopback address and your server socket will only listen on that. To be able to listen to all NICs on that machine use new InetSocketAddress(port) see the java doc for this construcotr. Or discover and use the specific host name of the server.
  2. Like @Nick Eu pointed out on the client you are trying to connect to the server on localhost. You need to have a way for your client to be configured so that it knows where the server is and use that.

To Clarify this further: A socket is a IP:Port that is used for communication. Say a host has two NIC's with IP's 192.168.2.1 and 10.0.0.2. Now if you bind server socket (say 8000) to the 192.168.2.1 IP. This port will be only accessible by using this combination 192.168.2.1:8000, similarly if you bind it to the 10.0.0.2 IP it will be accessible only through that IP. And if you bind localhost (aka Loopback address) it will be only accessible for clients on that host. This is useful if you want to run a server that is restricted to only clients on that host.

Yogesh_D
  • 17,656
  • 10
  • 41
  • 55