简体   繁体   English

如何正确关闭Java客户端套接字?

[英]How to close java client socket correctly?

How to close java client socket correctly? 如何正确关闭Java客户端套接字?

  • is it necessary to close the socket.getOutputStream? 是否需要关闭socket.getOutputStream?
  • is it necessary to close the socket.getInputStream? 是否需要关闭socket.getInputStream?
  • is it necessary to call socket.shutdownInput () ? 有必要调用socket.shutdownInput()吗?
  • is it necessary to call socket.shutdownOutput () ? 有必要调用socket.shutdownOutput()吗?

What should be the sequence of calls (before|after) socket.close()? socket.close()之前(之后)的调用顺序应该是什么?

The Socket documentation states: 套接字文档指出:

Closing this socket will also close the socket's InputStream and OutputStream. 关闭此套接字还将关闭套接字的InputStream和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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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