Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, increase the socket timeout

I have a simple server client application. Everything works, but at some stage it takes more than 5 minutes to get response from server (which is normal and it needs to be like this). The problem is that if it takes more than 5 minutes I keep getting this exception: java.net.SocketTimeoutException: Read timed out.

So I was wondering if there is some default socket timeout on windows or on the Java virtual machine I could set? I can't change the client code so setSoTimeout() is not an option for me.

Using Windows XP..

EDIT: As i understand now is that the socket connection is not opened in the client side. Its passed on from the server. So i decompiled allso the server jar file. But still cant find anything about the timeout.

like image 556
hs2d Avatar asked Oct 31 '11 08:10

hs2d


People also ask

What is the default Java socket timeout?

To avoid this problem, the Java™ Plug-in has added a default value to the network timeout of 2 minutes for all HTTP connections. You can override the default by setting this property.

How do I fix Java net Sockettimeoutexception read timed out?

A possible solution for this problem within the Tomcat web application is to modify the CONTEXT. XML file, and modify the CONNECTOR definition that governs the workstation browser connectivity to the Tomcat server. Specifically, modify the connectionTimeout value. Increase this value to suppress the error condition.


2 Answers

The default socket timeout is 0, which means never timeout. If it actually time out after 5 minutes, it means it has been set within the code. If the source isn't available, and there isn't a configuration, you can try to decompile the class, and look for setSoTimeout calls.

Since the comments, and the fact that searches didn't find any setSoTimeout() calls, you can take a different approach. Just let it time out, and write a small script that will retry the call in case it does. If you can call your client from the command line, you can parse the output, if it goes on stderr.

like image 76
stivlo Avatar answered Oct 09 '22 09:10

stivlo


Keeping unused TCP connection open for long time without any traffic isn't that trivial. Many firewalls/routers close the unused ports after some timeout.

One option could be to implement simple keepalive protocol to send dummy-packets every now and then, but if you can't touch the client code this is probably not an option.

Edit: I am not aware of any way to override setSoTimeout() set in the client code.

like image 22
Lycha Avatar answered Oct 09 '22 10:10

Lycha