Client can't connect to server when client/server are different projects
-
I was doing some
Socket
tests, and while usinglocalhost
everything ran as I wanted. But then I tried to switch it toIP address
, and well then things got weird. While the code was in the same class (Code_1 bellow), the simplified test from below was running fine, but as soon as I moved the server code in another project, even another package or class in same package, then when I'm running client code I'm gettingjava.net.ConnectException: Connection timed out: connect
. If when the server is in another project, and for client I'm switching tolocalhost
, then this time it is working. It doesn't want to work when they are in different projects/packages, and the client is usingIP address
and notlocalhost
, but they work if both are in same project even if client hasIP address
orlocalhost
.public static void main(String\[\] args) throws Exception { Thread thread = new Thread(new Runnable() { @Override public void run() { try { ServerSocket server = new ServerSocket(port); Socket socket = server.accept(); System.out.println(new ObjectInputStream(socket.getInputStream()).readObject()); socket.close(); server.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); thread.start(); Socket socket = new Socket("IP\_ADDRESS", port); new ObjectOutputStream(socket.getOutputStream()).writeObject("Test"); socket.close(); }
What is the problem, and what can I do to fix it?
-
I was doing some
Socket
tests, and while usinglocalhost
everything ran as I wanted. But then I tried to switch it toIP address
, and well then things got weird. While the code was in the same class (Code_1 bellow), the simplified test from below was running fine, but as soon as I moved the server code in another project, even another package or class in same package, then when I'm running client code I'm gettingjava.net.ConnectException: Connection timed out: connect
. If when the server is in another project, and for client I'm switching tolocalhost
, then this time it is working. It doesn't want to work when they are in different projects/packages, and the client is usingIP address
and notlocalhost
, but they work if both are in same project even if client hasIP address
orlocalhost
.public static void main(String\[\] args) throws Exception { Thread thread = new Thread(new Runnable() { @Override public void run() { try { ServerSocket server = new ServerSocket(port); Socket socket = server.accept(); System.out.println(new ObjectInputStream(socket.getInputStream()).readObject()); socket.close(); server.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); thread.start(); Socket socket = new Socket("IP\_ADDRESS", port); new ObjectOutputStream(socket.getOutputStream()).writeObject("Test"); socket.close(); }
What is the problem, and what can I do to fix it?
I found the problem, and it was because the antivirus was blocking network not filtered. I reinstalled it and forgat to add the rule again. But that being said, why was it working when it was in the same class? Was it automatically using
localhost
in that situation even tho it had a specific IP given? -
I was doing some
Socket
tests, and while usinglocalhost
everything ran as I wanted. But then I tried to switch it toIP address
, and well then things got weird. While the code was in the same class (Code_1 bellow), the simplified test from below was running fine, but as soon as I moved the server code in another project, even another package or class in same package, then when I'm running client code I'm gettingjava.net.ConnectException: Connection timed out: connect
. If when the server is in another project, and for client I'm switching tolocalhost
, then this time it is working. It doesn't want to work when they are in different projects/packages, and the client is usingIP address
and notlocalhost
, but they work if both are in same project even if client hasIP address
orlocalhost
.public static void main(String\[\] args) throws Exception { Thread thread = new Thread(new Runnable() { @Override public void run() { try { ServerSocket server = new ServerSocket(port); Socket socket = server.accept(); System.out.println(new ObjectInputStream(socket.getInputStream()).readObject()); socket.close(); server.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); thread.start(); Socket socket = new Socket("IP\_ADDRESS", port); new ObjectOutputStream(socket.getOutputStream()).writeObject("Test"); socket.close(); }
What is the problem, and what can I do to fix it?
Your client creates a new socket, writes some data to it, and closes it, but that is not how sockets should work. You need to establish a connection in the same way you make a telephone call: call - wait for an answer - talk - say goodbye - close. So both sides should establish the connection first, then run a loop that exchanges data. Only when both sides agree should you close the sockets.
-
Your client creates a new socket, writes some data to it, and closes it, but that is not how sockets should work. You need to establish a connection in the same way you make a telephone call: call - wait for an answer - talk - say goodbye - close. So both sides should establish the connection first, then run a loop that exchanges data. Only when both sides agree should you close the sockets.
Then how this signalling should be done? In that simple example the server was only expecting to receive a single object and then it knew it could close because that was all. And on client side the same thing, it knew it only had to send an object and it wasn't goin to receive anything back, so it could close it as that was all.
-
Then how this signalling should be done? In that simple example the server was only expecting to receive a single object and then it knew it could close because that was all. And on client side the same thing, it knew it only had to send an object and it wasn't goin to receive anything back, so it could close it as that was all.
Most systems have a protocol that both ends use. So each message has some indicator or flag to tell the peer what type of message is contained in the payload. For example transferring a file between two systems then the receiving station needs to know when the last block is received.
-
Most systems have a protocol that both ends use. So each message has some indicator or flag to tell the peer what type of message is contained in the payload. For example transferring a file between two systems then the receiving station needs to know when the last block is received.
OK, so there isn't something to specifically tell it, like socket.ImDone(), but in the context they speak to each other. In that case, I'm using something like that in main project. The example in main thread was something simple, to only address
java.net.ConnectException: Connection timed out
problem, which was in fact caused by antivirus firewall. -
I found the problem, and it was because the antivirus was blocking network not filtered. I reinstalled it and forgat to add the rule again. But that being said, why was it working when it was in the same class? Was it automatically using
localhost
in that situation even tho it had a specific IP given?I think you are on the right track here. In your ServerSocket, you did not bind to a specific address, so it likely defaulted to localhost on IPv4. This should also be addressable as 127.0.0.1 which is likely different than the IP used for your local network. Try “127.0.0.1” for your IP_ADDRESS and see if it works. There should be a variation of ServerSocket that will let you specify your local network IP address. Using the same IP for server and client should make it work. Some important commands you can learn: > route print > netstat > ipconfig