13

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.

4
  • Check if the timeout is configured using System properties. Commented Oct 31, 2011 at 10:27
  • @HannoBinder, can u explain what u mean by System properties? Commented Oct 31, 2011 at 10:31
  • I mean something like sun.net.client.defaultReadTimeout, or whatever might apply to your client's way of connecting. Commented Oct 31, 2011 at 13:28
  • The socket timeouts are set by the client. 'Socket connection is not opened in the client side. Its passed on from the server' is not correct. Commented May 8, 2017 at 0:47

4 Answers 4

14
+50

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I decompiled and looked for the setSoTimeout calls but didnt find anything. It must be set somewhere else then?
@hs2d I think it uses the default value of int field 0
8

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.

3 Comments

@benez I don't agree. If the router regards the connection as such a precious resource that it times it out, you don't have any business tricking it into keeping it open. In any case you still have to deal with connection losses anyway. Adding keepalive pings is just a pointless pain in the neck really.
@EJP i haven't seen yet a proper implementation to handle connection losses. there are cases where you are waiting for realtime updates and you need two parties constantly connected to keep their data in sync. connection losses are not simply handled with a simple reconnect. you will miss an important message or get it to late. e.g. the stock markets always send heartbeats and the irc protocol requires you to answer to a ping with pong. and yes you have to deal with connection losses, but you might want to avoid them in some programs.
@EJP adding heartbeats can be crucial. Just picture the following scenario: After being idle for some time, one of the computers has a power failure. With a clean disconnect, the TCP/IP protocol notifies the other side about this, but by contrast when one of the computers has a power failure, then of course, nobody gets notified about it. And this won't be detected automatically. In such situations, according to the other computer the connection is still established and everything appears to be fine. It is only when the client tries to actually send something, that it will detect the issue.
0

Answer that 0 is default timeout is not quite true. Since for example Axis2 client has default 60 seconds timeout, so it will depend on what kind of implementation you are using to make the call.

Can you provide more details? Sorry for writing in answers section but I don't have enough reputation to do comments :)

2 Comments

Looking at the code here i can see only java.io.InputStream. So the connection must be done from the server to client?
well, yes, for Java the default is infinite, but a library may change that. Maybe is a hint, hs2d, do you have any jar included? Or maybe, you can take a different approach, just let it time out and retry if it fails.
0

And do you need to keep the connection alive? Why don't you rethink it to make it more asynchronous. That schema is not scaling at all. At one point all of your available threads could be waiting for a long time response from server.

1 Comment

He needs to keep the connection alive if he wants to read from it. Can't make head or tail of all this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.