. Advertisement .
..3..
. Advertisement .
..4..
Millions of devices, including smartphones, laptops, gaming consoles, medical equipment, and many more, employ software platform and the object-oriented programming language known as Java. Java’s syntax and principles are derived from the C and C++ languages. When users try to run the code, they easily face this warning ”java.net.SocketTimeoutException: Connection timed out“. What exactly is this, and how can it be fixed? We’ll go over everything with you.
How does the error ”java.net.SocketTimeoutException: Connection timed out“ occur?
When you run your program, you can easily encounter the following error:
Java.net.SocketTimeoutException: Connection timed out
Actuallly your Java socket times out, it indicates that it takes too long for the other device to answer and your request ends before you do, so the error ”java.net.SocketTimeoutException: Connection timed out“ happens.
How to solve the error ”java.net.SocketTimeoutException: Connection timed out“?
Method 1: The timeout option for client and server activities can be pre-set
The timeout option for client and server activities can be pre-set by a developer.
On the client side:
Setting a connection timeout on the client side and handling it later with a try/catch/finally block are both efficient solutions. You can adjust the timeout parameter using the connect(SocketAddress endpoint, int timeout) method:
Socket socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(host, port);
socket.connect(socketAddress, 12000); //12000 are milli seconds
Note: The procedure will throw a SocketTimeoutException if the timeout expires before it returns.
If OkHttp Client is being used, the following can be added:
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(60, TimeUnit.SECONDS);
client.setReadTimeout(60, TimeUnit.SECONDS);
client.setWriteTimeout(60, TimeUnit.SECONDS);
If OkHttp3 is being used, you must use 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();
Method 2: Retry to connect a few times
Another solution is retrying to connect a few times. This might work as a temporary solution until the real problem is resolved. Look at the following example to understand more about this method:
for (int retries = 0; retries < 3; retries++) {
try {
final HttpClient client = createHttpClientWithDefaultSocketFactory(null, null);
final HttpResponse response = client.execute(get);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
throw new IllegalStateException("GET Request on '" + get.getURI().toString() + "' resulted in " + statusCode);
} else {
return response.getEntity();
}
} catch (final java.net.SocketTimeoutException e) {
// connection timed out...let's try again
}
}
After doing that, the error ”java.net.SocketTimeoutException: Connection timed out“ will completely disappear and your program will run well without any errors. So, let’s do it to get your desired results.
Method 3: Utilize try/catch/finally
Utilizing try/catch/finally is not a bad way for you to solve the error ”java.net.SocketTimeoutException: Connection timed out“.
If you are a developer, you can handle the error in the catch and enclose the socket connection portion of your code in a try/catch/finally block. You might try connecting again, trying to connect to a different possible socket, or just cleanly exiting the program.
Set a timeout value from the server side by using the setSoTimeout(int timeout) method. How long the ServerSocket.accept() method will block is determined by the timeout value as following:
ServerSocket serverSocket = new new ServerSocket(port);
serverSocket.setSoTimeout(12000);
Conclusion
Above are the best solutions for the ”java.net.SocketTimeoutException: Connection timed out“ problem. We hope you will enjoy our article about the error. With this knowledge, we know that you can fix your error quickly by following these steps! If you still have any other questions about fixing this syntax error, please leave a comment below. Thank you for reading!
Read more
Leave a comment