在 CLOSE-WAIT 中写入 SocketChannel

发布于 2024-11-18 09:33:16 字数 113 浏览 1 评论 0原文

我注意到,当底层连接处于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎言笑 2024-11-25 09:33:16

它不应该第一次抛出异常。连接可能仍然是可写的。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文