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();
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.