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?

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.
- Server is slow and default timeout is less, so just put timeout value according to you.
- Server is working fine but timeout value is for less time. so change the timeout value.
Related Topics
- Reached end of file while parsing
- Unreachable statement error in Java
- Int Cannot Be Dereferenced Error - Java
- How to fix java.lang.classcastexception
- How to fix java.util.NoSuchElementException
- How to fix "illegal start of expression" error
- How to resolve java.net.SocketException: Connection reset
- Non-static variable cannot be referenced from a static context
- Error: Could not find or load main class in Java
- How to Handle OutOfMemoryError in Java
- How to handle the ArithmeticException in Java?