关闭BufferedReader / PrintWriter会关闭套接字连接吗? [英] Does closing the BufferedReader/PrintWriter close the socket connection?
问题描述
我有一个应用程序,它使用简单的套接字在两个系统之间传递一些字符。我把我的java应用程序作为服务器运行。我建立一个连接很好,甚至传递一条消息。但是,在发送一条消息后,我的连接关闭。
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.
从我可以看出来看,好像在关闭 printWriter
和 bufferedReader
套接字本身是否正在关闭?!这很糟糕,因为我有多条消息要在同一个连接上发送。
From what I can tell it appears as if on closing the printWriter
and bufferedReader
the socket itself is being closed?! This is bad, as I have multiple messages to send on the same connection.
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?
我吃饱了吗?如何在Java中维护此连接?
Am I full of it? How do I maintain this connection in Java?
推荐答案
是的,关闭任何Writer / Reader将关闭它们包装的所有其他作家和读者。在准备好关闭底层套接字之前,请不要关闭它。
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.
这篇关于关闭BufferedReader / PrintWriter会关闭套接字连接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!