简体   繁体   English

Java:套接字读取超时异常

[英]Java: socket read time out exception

I trying to make a call to a very heavy duty process. 我试图打电话给一个非常重的过程。 It's average work length is estimated by 9-10 minutes. 它的平均工作时间估计为9-10分钟。

When I'm executing the process, I set the timeout for a ridiculously huge number: 99999999. 当我执行该过程时,我将超时设置为一个非常庞大的数字:99999999。

After 2 minutes, I get the following error: 2分钟后,我收到以下错误:

java.net.SocketTimeoutException: Read timed out java.net.SocketTimeoutException:读取超时

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. 我试图把它弄得更糟,我把超时设置为3000,并且在预期的3秒后我得到了同样的错误。

Do you have any idea on why socket.setSoTimeout(99999999) sets it to 120000 max? 您是否知道为什么socket.setSoTimeout(99999999)将其设置为最大120000?

I had the same problem and the solution was not use socket.shutdownInput(); socket.shutDownOutput(); 我有同样的问题,解决方案是不使用socket.shutdownInput(); socket.shutDownOutput(); 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. 这使得套接字进入FIN_WAIT状态,因此在关闭前等待2分钟。 You can read more about it in this post 您可以在这篇文章中阅读更多相关信息

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 WR 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. 请注意,根据WR Stevens在TCP / IP Illustrated,第二卷,#17.4中的说法,超时保持为1000Hz刻度的短暂,因此超时不能超过11分钟。 This applies to the BSD code. 这适用于BSD代码。

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. 使用指定的超时启用/禁用SO_TIMEOUT ,以毫秒为单位。 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. 如果将此选项设置为非零超时,则与此Socket关联的InputStream上的read()调用将仅阻止这段时间。 If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. 如果超时到期,则引发java.net.SocketTimeoutException ,尽管Socket仍然有效。 The option must be enabled prior to entering the blocking operation to have effect. 必须在进入阻止操作之前启用该选项才能生效。 The timeout must be > 0 . 超时必须> 0 A timeout of zero is interpreted as an infinite timeout. 超时为零被解释为无限超时。

If you provide more information about your call, i may improve the answer. 如果您提供有关通话的更多信息,我可以改进答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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