在套接字读取时获取操作超时异常 [英] Getting operation timed out exception on socket read

查看:162
本文介绍了在套接字读取时获取操作超时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我设置mSocket.setSoTimeout(1000);我会得到

java.net.SocketTimeoutException:读取超时

java.net.SocketTimeoutException: Read timed out

等待一秒钟后等待mSocket.getInputStream().read().

但是,如果我这样做mSocket.setSoTimeout(0);我会得到

But, if I do mSocket.setSoTimeout(0); I get

java.net.SocketException:操作超时

java.net.SocketException: Operation timed out

等待大约一分钟(在我的大部分跑步中为55秒)之后,这令人费解.

after around a minute(55 seconds in most of my runs) of wait which is puzzling.

我尝试将Mac OS El Capitan上的sysctl tcp设置增加无济于事.如果将这些设置的超时时间限制在一分钟左右,那么我应该已经获得了与以前相同的读取超时异常.此操作超时异常是什么?

I have tried increasing sysctl tcp settings on my Mac OS El Capitan to no avail. If those settings were restricted the timeout to around a minute, I should have gotten the same read timed out exception as before. What's this operation timed out exception?

可能值得一提的是,我故意使用pfctl关闭网络,并且由于我在tcp上使用了websockets,因此我希望在给定较大超时的情况下连接不会中断,并且一旦我将网络启动一次,流量将继续再次.

Its probably worth mentioning that I am deliberately bringing the network down using pfctl and since I am using websockets over tcp I expect the connection to not break given a large timeout and the flow to continue once I bring the network up once again.

两个异常的堆栈跟踪相同,即

The stack trace for both exceptions is the same ie

at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:150)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at java.net.SocketInputStream.read(SocketInputStream.java:203)
at org.jwebsocket.kit.WebSocketProtocolAbstraction.protocolToRawPacket(WebSocketProtocolAbstraction.java:220)

推荐答案

当应用程序调用setSoTimeout(0)时,操作系统实际上可能永远等待数据.但是无论SO_TIMEOUT值如何,都有几种事件会中断读取操作.

When an application calls setSoTimeout(0) then OS may actually wait for data forever. But there are several kinds of events that interrupts the read operation regardless the SO_TIMEOUT value.

如果OS破坏了套接字后面的TCP连接,则读操作将被中断,因为TCP连接不再存在,以后也没有机会接收任何数据.即使对等方稍后发送了数据,本地TCP堆栈也会丢弃它(并可能回答TCP-RST).

If the OS destroys the TCP connection behind the socket then the read operation is interrupted because the TCP connection does not exist anymore and there is no chance to receive any data in future. Even if the peer sent a data later the local TCP stack would drop it (and perhaps answer TCP-RST).

操作系统破坏了TCP连接,因为它发送了数据并且没有收到任何ACK.因此,OS尝试重新传输数据,但此后不久它放弃了,并破坏了连接.

The OS destroyed the TCP connection because it sent data and did not receive any ACK. So OS tried to retransmit data and it gave up after while and destroyed the connection.

这篇关于在套接字读取时获取操作超时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆