java 关闭 Input/OutputStream 和直接关闭 Socket 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3428127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between closing Input/OutputStream and closing Socket directly?
提问by Alfred
I just wondering what java does when we call close on the inputStream and outStream associated with a socket. What is the difference from the close call on the socket, i.e Socket.close().
我只是想知道当我们在与套接字关联的 inputStream 和 outStream 上调用 close 时,java 做了什么。和socket上的close调用有什么区别,即Socket.close()。
if we just close the io stream on the socket, but not close the socket, can we reopen the io stream on the socket again?
如果我们只是关闭了socket上的io流,而没有关闭socket,我们可以重新打开socket上的io流吗?
Thanks in advance!
提前致谢!
采纳答案by user207421
You should close the outermost output stream you have created from the socket. That will flush it. Closing either the socket or the input stream doesn't do that so it isn't adequate. Having closed that output stream you don't need to do anything else.
您应该关闭从套接字创建的最外面的输出流。那会冲洗它。关闭套接字或输入流都没有这样做,所以它是不够的。关闭该输出流后,您无需执行任何其他操作。
回答by Jes
From the java api documentation for Socket:
来自Socket的 java api 文档:
public void close() throws IOException Closes this socket. Any thread currently blocked in an I/O operation upon this socket will throw a SocketException.
Once a socket has been closed, it is not available for further networking use (i.e. can't be reconnected or rebound). A new socket needs to be created.
Closing this socket will also close the socket's InputStream and OutputStream.
If this socket has an associated channel then the channel is closed as well.
public void close() throws IOException 关闭此套接字。当前在此套接字上的 I/O 操作中阻塞的任何线程都将抛出 SocketException。
一旦一个套接字被关闭,它就不能用于进一步的网络使用(即不能重新连接或重新连接)。需要创建一个新的套接字。
关闭此套接字也将关闭套接字的 InputStream 和 OutputStream。
如果此套接字具有关联的通道,则该通道也将关闭。
Closing the InputStream of the Socket will lead to the closing of the Socket. The same goes for closing the OutputStream of the Socket.
关闭 Socket 的 InputStream 会导致 Socket 关闭。关闭 Socket 的 OutputStream 也是如此。
From the java api documentation for Socket#getInputStream()
来自Socket#getInputStream()的 java api 文档
Closing the returned InputStream will close the associated socket.
关闭返回的 InputStream 将关闭关联的套接字。
Check the API documentation, it is there for a reason.
检查 API 文档,这是有原因的。
回答by PMorganCA
This is more like a comment than a good answer: (I would rather add a comment to one the answers above but I don't have the rep)
这更像是一个评论而不是一个好的答案:(我宁愿为上面的答案添加评论,但我没有代表)
The question, as I read it, is "can I close a stream on a socket and then open a stream on the same socket?"... but people seem to be answering this: "how should I cleanly close my socket?"... which is not the question being asked.
正如我所读到的,问题是“我可以关闭一个套接字上的流,然后在同一个套接字上打开一个流吗?”……但人们似乎在回答这个问题:“我应该如何干净利落地关闭我的套接字?” ......这不是被问到的问题。
The answer to the question being asked is "no". When you close the stream, you close the socket.
所问问题的答案是“否”。关闭流时,也就关闭了套接字。
(I do understand why, in at least one case, someone might ask this question. When you are streaming Java Properties over a socket the receiving end has to see EOF to recognize the end of the properties - for the receiver to see EOF the sender has to close the stream/socket. BUT, if you have a command/response protocol operating over that socket, you DON'T want to close it or you'll lose the channel that you want to send a response on. See Java streaming Properties over Socketfor one way to handle this)
(我确实理解为什么,至少在一种情况下,有人可能会问这个问题。当您通过套接字流式传输 Java 属性时,接收端必须看到 EOF 以识别属性的结尾 - 接收者才能看到发送者的 EOF必须关闭流/套接字。但是,如果您有一个通过该套接字运行的命令/响应协议,您不想关闭它,否则您将丢失要发送响应的通道。请参阅Java 流Socket 上的属性是处理此问题的一种方法)

