简体   繁体   中英

How to close java client socket correctly?

How to close java client socket correctly?

  • is it necessary to close the socket.getOutputStream?
  • is it necessary to close the socket.getInputStream?
  • is it necessary to call socket.shutdownInput () ?
  • is it necessary to call socket.shutdownOutput () ?

What should be the sequence of calls (before|after) socket.close()?

The Socket documentation states:

Closing this socket will also close the socket's InputStream and OutputStream.

You don't have to shutdown the input/output. However that does allow you to "half" close the socket. Say if you wanted to continue to send data, but want to indicate you will no longer receive it.

So in short; It's completely fine to do the following:

...
finally {
    if (socket != null)
        socket.close();
}

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