在 CLOSE-WAIT 中写入 SocketChannel
我注意到,当底层连接处于 CLOSE-WAIT 状态时,SocketChannel.write 不会引发任何异常。这是预期的行为吗?如果是这样,我如何确定连接尚未ESTABLISHED
?
I noticed that SocketChannel.write
does not throw any exception, when the underlying connection is in CLOSE-WAIT
state. Is it expected behaviour? If so, how can I figure out that the connection is not ESTABLISHED
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不应该第一次抛出异常。连接可能仍然是可写的。 CLOSE_WAIT 表示已收到传入的 FIN。这意味着另一端已经完成写入。它可能仍在读取:它可能只是关闭了输出连接。所以TCP必须写入数据。如果对等方已关闭整个连接,它将在收到写入后发出 RST,您将在后续写入时体验到连接重置。
您的应用程序协议应该使得要么不可能写入由对等方关闭的连接,要么在出现错误之前可以完成多次写入。 TCP 不可能在第一次这样的写入时给你一个错误。
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. 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. So TCP has to write the data. 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.
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.