I have a program that receives a data package via socket from a client.
The program detects the IP and client port to send to client a mensage confirmation.
But, when the program send the message back into its socket, it receives the following error message:
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0 (Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect (DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect (AbstractPlainSocketImpl.java:339)
There are two issues. First, to get the port for the client you should be calling getPort() - see Error recognizing IP and Port from Client ... . Second, to reply to the client, write to the output stream for the same socket you read from - s.getOutputStream() - don't try and open a new socket connection back to the client (unlikely the client is listening for connections anyway).