简体   繁体   English

网络断开连接不会在Java中引发IOException

[英]Network disconnect does not throw IOException in Java

I have a thread that writes to a TCP socket every 5 sec., connected to a remote TCP port (using Java .net socket, not NIO). 我有一个线程,每5秒写入一个TCP套接字,并连接到远程TCP端口(使用Java .net套接字,而不是NIO)。 For your information I have not configured any timeout (S0,..) on to the socket layer, it's having the default values. 供您参考,我尚未在套接字层上配置任何超时(S0,..),它具有默认值。 If the network cable connected to remote TCP port is removed my java thread is not getting any IO exception, rather it keeps writing to the socket. 如果删除了连接到远程TCP端口的网络电缆,我的Java线程将不会出现任何IO异常,而是会继续写入套接字。

Can somebody guide me why am I not getting those exceptions? 有人可以指导我为什么我没有得到那些例外吗? I guess write method should through IOException while trying to write to the socket every 5 sec. 我想在尝试每5秒写入一次套接字时,write方法应该通过IOException。

Regards, Rajib 问候,拉吉卜

This depends entirely an how much you write to the socket, in case you don't flush regurarly. 如果您不定期刷新,这完全取决于您向套接字写入的数量。 If you write only a couple of bytes every 5 seconds, it may indeed take quite a bit for the buffer to fill and flush automatically. 如果每5秒仅写入几个字节,则缓冲区可能需要花费很多时间才能自动填充和刷新。 The exception should arise eventually. 最终应该出现例外。 If you do flush, then there could be another network pipe that you are overlooking, such as WiFi, and the transport just fails over to that one. 如果刷新,则可能有另一个网络管道正在忽略,例如WiFi,而传输只是故障转移到该网络管道。

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

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