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. 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.
精彩评论