Does closing the BufferedReader/PrintWriter close the socket connection?

java, sockets

Solution

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.

Problem

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. 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? ``` Am I full of it? How do I maintain this connection in Java?

Original source