简体   繁体   English

在 CLOSE-WAIT 中写入 SocketChannel

[英]Writing to SocketChannel in CLOSE-WAIT

I noticed that SocketChannel.write does not throw any exception, when the underlying connection is in CLOSE-WAIT state.我注意到当底层连接处于CLOSE-WAIT state 时, SocketChannel.write不会抛出任何异常。 Is it expected behaviour?这是预期的行为吗? If so, how can I figure out that the connection is not ESTABLISHED ?如果是这样,我怎样才能确定连接不是ESTABLISHED

It shouldn't throw an exception the first time.它不应该第一次抛出异常。 The connection may still be writable.连接可能仍然是可写的。 CLOSE_WAIT means that an incoming FIN has been received. CLOSE_WAIT 表示已收到传入的 FIN。 All that means is that the other end has finished writing.这意味着另一端已经完成了写入。 It may still be reading: it may only have shutdown the connection for output.它可能仍在阅读:它可能只关闭了 output 的连接。 So TCP has to write the data.所以 TCP 必须写入数据。 If the peer has closed the entire connection, it will issue an RST on receipt of the write, which you will experience as a connection reset on a subsequent write.如果对等方已关闭整个连接,它将在收到写入时发出 RST,您将在后续写入时体验到连接重置。

Your application protocol should be such that either writing to a connection closed by the peer is impossible, or else that multiple writes may be done before an error shows up.您的应用程序协议应该是不可能写入由对等方关闭的连接,否则可能会在出现错误之前完成多次写入。 TCP cannot possibly give you an error on the first such write. TCP 不可能在第一次这样的写入时给你一个错误。

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

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