java 关闭 BufferedReader/PrintWriter 是否会关闭套接字连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/484925/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:36:07  来源:igfitidea点击:

Does closing the BufferedReader/PrintWriter close the socket connection?

javasockets

提问by Alex

I have an application that uses simple sockets to pass some characters between two systems. I have my java application running as a server. I establish a connection fine, and even pass one message. However, after one message has been sent my connection closes.

我有一个应用程序,它使用简单的套接字在两个系统之间传递一些字符。我的 Java 应用程序作为服务器运行。我很好地建立了连接,甚至传递了一条消息。但是,在发送一条消息后,我的连接关闭。

From what I can tell it appears as if on closing the printWriterand bufferedReaderthe socket itself is being closed?! This is bad, as I have multiple messages to send on the same connection.

据我所知,似乎在关闭时printWriterbufferedReader套接字本身正在关闭?!这很糟糕,因为我有多个消息要在同一个连接上发送。

printWriter = new PrintWriter(theServer.getClientSocket().getOutputStream());
bufferedReader = new BufferedReader(new InputStreamReader(theServer.getClientSocket().getInputStream()));


printWriter.println("the line");

printWriter.close(); //Closing on these lines?
bufferedReader.close(); //Closing on these lines?

Am I full of it? How do I maintain this connection in Java?

我充满了吗?我如何在 Java 中维护这种连接?

回答by Eddie

Yes, closing any Writer/Reader will close all other Writers and Readers that they wrap. Don't close it until you are ready to close the underlying socket.

是的,关闭任何写入器/读取器将关闭它们包装的所有其他写入器和读取器。在您准备好关闭底层套接字之前不要关闭它。

回答by Hosam Aly

As @Eddie said(seconds before me! :) ), closing the writer and/or the reader will close the underlying socket streams and the socket itself: However, I believe the socket itself will not be closed.

正如@Eddie所说(比我早几秒!:)),关闭写入器和/或读取器将关闭底层套接字流和套接字本身但是,我相信套接字本身不会被关闭.

Closing the returned InputStream will close the associated socket.

关闭返回的 InputStream 将关闭关联的套接字。

You shouldn't close the writer nor the reader. Just flush the writer to make sure your messages will arrive in time. Closing the socket later on will close the respective streams, so you don't need to close them yourself. Just leave your reader/writer objects to the GC.

你不应该关闭作者或读者。只需刷新写入器即可确保您的消息及时到达。稍后关闭套接字将关闭相应的流,因此您无需自己关闭它们。只需将您的读取器/写入器对象留给 GC。

回答by Lawrence Dol

Another alternative is to create yourself a NoCloseInputStream and NoCloseOutputStream filters which simply do nothing on close; then use them to wrap your application socket's streams (before any application wrappering like a buffer).

另一种选择是为自己创建一个 NoCloseInputStream 和 NoCloseOutputStream 过滤器,它们在关闭时什么都不做;然后使用它们来包装应用程序套接字的流(在任何应用程序包装如缓冲区之前)。

Note that if you were to do this, you would need to keep a reference to the socket (or the wrapped streams) so that you can close the socket when you are actually done with it.

请注意,如果要执行此操作,则需要保留对套接字(或包装的流)的引用,以便在实际使用完毕后可以关闭套接字。

To answer the comment that this is "too advanced a concept for the OP": The OP's problem is that in closing the top level stream, he is also closing the underlying socket, but that's no good since he wants to create further top-level streams over the socket to send further messages. Depending on his architecture, the only way to achieve this may be to wrap the streams in NoClose wrappers - for example he may be passing the streams to an XML serializer or deserializer which closes the stream when it's done, which close is outside of his control.

回答“对于 OP 来说这是一个太高级的概念”的评论:OP 的问题是,在关闭顶级流的同时,他也在关闭底层套接字,但这不好,因为他想创建进一步的顶级流通过套接字发送更多消息。根据他的架构,实现这一点的唯一方法可能是将流包装在 NoClose 包装器中 - 例如,他可能将流传递给 XML 序列化器或反序列化器,该序列化器或反序列化器在完成时关闭流,而哪个关闭在他的控制之外.

回答by Peter Lawrey

You need to close the socket after closing the streams.

您需要在关闭流后关闭套接字。