开发者

Writing to SocketChannel in CLOSE-WAIT

开发者 https://www.devze.com 2023-03-16 19:49 出处:网络
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, h开发者_如何学Cow can I figure out that the con

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, h开发者_如何学Cow can I figure out that the connection is not 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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号