I created a server and a client and it's working with localhost. But when using any IP except localhost for the client it won't work. I tested it with both LAN and Hamachi. When using my own Hamachi IP it also won't work, that's why I think it has nothing to do with port forwarding or something like that.
This is the initialisation code for my server:
public static void init() { try { serverSocket = new ServerSocket(port, 4, InetAddress.getByName("localhost")); new ClientThread(serverSocket); // The thread that's sending the data, not important for my problem, because I can't even connect to the server } catch (IOException e) { e.printStackTrace(); } }
And the client code:
public static void init() { try { clientSocket = new Socket(serverIP, port); System.out.println("Connected!\nBuffering..."); in = new ObjectInputStream(clientSocket.getInputStream()); out = new ObjectOutputStream(clientSocket.getOutputStream()); String name = "Client "+new Random().nextLong(); Client.name = name; System.out.println("Buffered\nPinging for 256 bytes..."); out.writeObject(name.getBytes()); out.flush(); long latency = in.readLong(); System.out.println("Latency: "+(System.currentTimeMillis()-latency)); System.out.println("Starting threads..."); new ThreadSend(out); new ThreadReceive(in); } catch (IOException e) { e.printStackTrace(); } }
Are there any errors in my code or is it something else?