How to handle java.net.SocketTimeoutException

Your Java socket is timing out (throws java.net.SocketTimeoutException: Connection timed out) means that it takes too long to get respond from other device and your request expires before getting response.

How to solve?

A developer can pre-set the timeout option for both client and server operations.

From Client side:

You can effectively handle it from client side by define a connection timeout and later handle it by using a try/catch/finally block. You can use the connect(SocketAddress endpoint, int timeout) method and set the timeout parameter:
Socket socket = new Socket(); SocketAddress socketAddress = new InetSocketAddress(host, port); socket.connect(socketAddress, 12000); //12000 are milli seconds

Note: If the timeout elapses before the method returns, it will throw a SocketTimeoutException.

If you are using OkHttp Client then you can add:
OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(60, TimeUnit.SECONDS); client.setReadTimeout(60, TimeUnit.SECONDS); client.setWriteTimeout(60, TimeUnit.SECONDS);
If you are using OkHttp3 then you must do it using the builder:
OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.connectTimeout(60, TimeUnit.SECONDS); builder.readTimeout(60, TimeUnit.SECONDS); builder.writeTimeout(60, TimeUnit.SECONDS); client = builder.build();

Using try/catch/finally

If you are a developer, so you can surround the socket connection part of your code in a try/catch/finally and handle the error in the catch. You might try connecting a second time, or try connecting to another possible socket, or simply exit the program cleanly.

From Server side:

From server side you can use the setSoTimeout(int timeout) method to set a timeout value. The timeout value defines how long the ServerSocket.accept() method will block:
ServerSocket serverSocket = new new ServerSocket(port); serverSocket.setSoTimeout(12000);

How to Java SocketTimeoutException?


How do I resolve a java.net.SocketTimeoutException
A socket is one end-point of a logical link between two computer applications. In order to establish a connection to the server from the remote client, the socket constructor is invoked, which instantiates a socket object. This operation blocks all other processes until a successful connection is made. However, if the connection isn't successful after a period of time, the program throws a ConnectionException with a message:
java.net.SocketTimeoutException: Connection timed out

This exception is occurring on following condition.

  1. Server is slow and default timeout is less, so just put timeout value according to you.
  2. Server is working fine but timeout value is for less time. so change the timeout value.
It is important to note that after this exception is thrown the socket remains valid , so you can retry the blocking call or do whatever you want with the valid socket. So to avoid this exception in another way, you should keep the connection be alive using the method Socket.setKeepAlive() method although possibly you have not used the method setTimeout() , meaning asking the socket to unlimited block to receive.