简体   繁体   中英

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.

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.

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.

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 :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM