简体   繁体   中英

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, how 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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