title: Client application socket closed after printing string
body: <p>I'm building a Java client application which needs to send a message to a server and receive a response afterwards. I can send the message successfully, the problem is that I can't get the response because I get an IO exception ("Socked is closed") when trying to read the 'BufferedReader'.</p>
  
  <p>This is my code, so far:</p>
  
  <pre><code>public class MyClass {
  
      /**
       * @param args the command line arguments
       */
      @SuppressWarnings("empty-statement")
      public static void main(String[] args) {
          JSONObject j = new JSONObject();
          try {
              j.put("comando", 1);
              j.put("versao", 1);
              j.put("senha", "c4ca4238a0b923820dcc509a6f75849b");
              j.put("usuario", "1");
              j.put("deviceId", "1");
  
          } catch (JSONException ex) {
              System.out.println("JSON Exception reached");
          }
  
          String LoginString = "{comando':1,'versao':1,'senha':'c4ca4238a0b923820dcc509a6f75849b','usuario':'1','deviceId':'1'}";
          try {
              BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  
              Socket clientSocket = new Socket("10.1.1.12", 3333);
              System.out.println("Connected to the server successfully");
              PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream(),true);
  
              outToServer.println(j.toString());
              outToServer.close();
              System.out.println("TO SERVER: " + j.toString());
  
              BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  
              String resposta = inFromServer.readLine();
              System.out.println("FROM SERVER: " + resposta);
  
              clientSocket.close();
          } catch (UnknownHostException ex) {
              System.out.println("Could not connect to the server [Unknown exception]");
          } catch (IOException ex) {
              System.out.println(ex.getMessage());
          } 
      }
  }
  </code></pre>
  
  <p>I know that the socket is being closed because of the <strong>OutToServer.close()</strong> but closing the stream is the only way to send the message. How should I approach this situation?</p>