Java: socket read time out exception

2019-02-08 17:42发布

I trying to make a call to a very heavy duty process. It's average work length is estimated by 9-10 minutes.

When I'm executing the process, I set the timeout for a ridiculously huge number: 99999999.

After 2 minutes, I get the following error:

java.net.SocketTimeoutException: Read timed out

I tried to mess with it some more, and I set the timeout to 3000, and after 3 seconds as anticipated I got the same error.

Do you have any idea on why socket.setSoTimeout(99999999) sets it to 120000 max?

3条回答
放我归山
2楼-- · 2019-02-08 17:46

I had the same problem and the solution was not use socket.shutdownInput(); socket.shutDownOutput(); until the last time of reading or writing data to the socket. This made the socket go to FIN_WAIT state thus waiting 2 minutes before closing. You can read more about it in this post

查看更多
等我变得足够好
3楼-- · 2019-02-08 18:04

Clearly you aren't setting the timeout you think you're setting, or someone else is changing it afterwards. You'll have to post some code to get further elucidation.

Note that according to W.R. Stevens in TCP/IP Illustrated, Vol II, #17.4, the timeout is held in a short as a number of 1000Hz ticks, so a timeout beyond 11 minutes isn't possible. This applies to the BSD code.

查看更多
我想做一个坏孩纸
4楼-- · 2019-02-08 18:04

I'm not sure how your application works, but try to set an infinite timeout to the socket

public void setSoTimeout(int timeout)
              throws SocketException

Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

If you provide more information about your call, i may improve the answer.

查看更多
登录 后发表回答