java.net.SocketTimeoutException: Read timed out = Connection to server closed?

Question:

If you set the socket to a maximum waiting time for receiving data of 5 seconds

Socket ms = new Socket("127.0.0.1", 1234);
ms.setSoTimeout(5000);

then, on error SocketTimeoutException

java.net.SocketTimeoutException: Read timed out

will the connection to the server be closed? That is, the socket will be closed after such an exception?

And if the timeout is not set on the socket, then such an error will never occur at all?

Answer:

Judging by the documentation it will not be closed

If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid.

\

And if the timeout is not set on the socket, then such an error will never occur at all?

Yes, it will wait until the OS drops the connection.

Scroll to Top