Java socket – half closed

Typically, the output stream is closed to indicate that the output has ended. However, this cannot be done during network communication. Because when we close the output stream, the socket corresponding to the output stream will also be closed, so the program will no longer be able to read data from the socket.

In order to cope with this situation, socket provides two and a half closing methods to close only the input stream or output stream of socket to indicate that the output data has been sent. Method details:

Shutdown input(): close the input stream of the socket, and the program can also output data through the output stream of the socket;

Shutdown output(): close the output stream of the socket, and the program can also read data through the input stream of the socket.

When the input stream or output stream is closed by calling the shutdowninput () or shutdownoutput () method, the socket is in a semi closed state. At this time, you can use isinputshutdown() or isoutputshutdown() to determine whether the socket is in a half read or half write state.

It should be noted that even if the same socket calls shutdowninput () and shutdowninput () methods, the socket instance is still not closed, but the socket can neither output data nor read data.

When the input stream or output stream is closed by calling the shutdowninput () or shutdownoutput () method, the socket cannot open the output stream or input stream again. Therefore, this method is not suitable for interactive applications that need to maintain a persistent communication state.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>