Java: socket read time out exception

跟風遠走 提交于 2019-12-18 14:54:02

问题


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?


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/12406628/java-socket-read-time-out-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!