简体   繁体   English

尽管未插入LAN电缆,但OutputStream不会引发IOException

[英]OutputStream does not throw IOException although LAN-cable is not plugged

I established a TCP Connection with Java. 我与Java建立了TCP连接。 The Server sends an "ALIVE"-Message to the Client every second to detect a broken connection. 服务器每秒向客户端发送“ ALIVE”消息,以检测断开的连接。

but when I plug of the LAN-cable the IOException is thrown exactly after 23seconds, and not immediately after the send attempt. 但是当我插入LAN电缆时,IOException恰好在23秒后抛出,而不是在发送尝试后立即抛出。

My code to send looks like that 我发送的代码如下所示

// out is an OutputStream
try {
    out.write(String.format("%s\r\n", encryptedCommand).getBytes(Charset.forName("UTF-8")));
} catch (IOException e) {
    fireConnectionClosed();
}

I'm pretty sure that this was working already, but now it doesn't any more 我很确定这已经在起作用,但是现在不再起作用了

When a TCP packet fails to get through, the TCP stack resends the packet a few times. 当TCP数据包无法通过时,TCP堆栈将重新发送该数据包几次。 This allows the connection to stay up even if an occasional packet is lost. 即使偶尔丢失数据包,这也可以保持连接状态。 That prevents the problem of connections dropping whenever there's the least bit of congestion on the network. 这样可以防止在网络上出现最少量拥塞时就断开连接的问题。 You can test this by unplugging the network for 5 or 10 seconds, then plugging it back in - the connection should be fine. 您可以通过拔下网络5或10秒钟,然后再将其重新插入进行测试-连接应该正常。

However, this feature also means that the TCP connection won't be recognized as having been lost until after a few retries. 但是,此功能还意味着直到重试几次,TCP连接才不会被视为丢失。

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

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