简体   繁体   English

Java DataOutputStream在断开连接时不会引发IOException

[英]Java DataOutputStream doesn't throw IOException on disconnect

DataOutputStream doesn't throw an IOException when I plug my cable out, what's wrong with my code? 当我插入电缆时, DataOutputStream不会引发IOException ,我的代码怎么了?

Here is my code: 这是我的代码:

private void sendMessage(String message) {
    try {
        mDataOutputStream.writeUTF(message);
        mDataOutputStream.flush();
    } catch(IOException ex) {
        close();
    }
}

As soon as I plug the cable back in, the data is still sent to the server. 一旦重新插入电缆,数据仍将发送到服务器。

TCP connection are designed to cope with short term failures gracefully, and transparently where possible. TCP连接被设计为在可能的情况下优雅,透明地应对短期故障。 This means when the connection is lost for short period eg less than 3 minutes, the connection should re-establish itself transparently. 这意味着当连接丢失短时间(例如少于3分钟)时,连接应透明地重新建立。 Even if the connection has timed out you will not know asynchronously, it may be the next read or write which fails. 即使连接超时,您也不会异步知道,这可能是下一次读取或写入失败。

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

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