title: Sending a JSON object over TCP with Java
body: <p>I'm trying to replace a Netcat command that I'm running in my terminal that will reset some data on a server. The netcat command looks like this:</p>
  
  <pre><code>echo '{"id":1, "method":"object.deleteAll", "params":["subscriber"]} ' | nc x.x.x.x 3994
  </code></pre>
  
  <p>I've been trying to implement it in Java since I would like to be able to call this command from an application I'm developing. I'm having issues with it though, the command is never executed on the server. </p>
  
  <p>This is my java code:</p>
  
  <pre><code>try {
      Socket socket = new Socket("x.x.x.x", 3994);
      String string = "{\"id\":1,\"method\":\"object.deleteAll\",\"params\":[\"subscriber\"]}";
      DataInputStream is = new DataInputStream(socket.getInputStream());
      DataOutputStream os = new DataOutputStream(socket.getOutputStream());
      os.write(string.getBytes());
      os.flush();
  
      BufferedReader in = new BufferedReader(new InputStreamReader(is));
      String inputLine;
      while ((inputLine = in.readLine()) != null)
          System.out.println(inputLine);
  
      is.close();
      os.close();
  
  } catch (IOException e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>The code also hangs on the while loop that should read the <code>InputStream</code>, I have no idea why. I've been using Wireshark to capture the packets and the data that is going out looks the same:</p>
  
  <pre><code>{"id":1,"method":"object.deleteAll","params":["subscriber"]}
  </code></pre>
  
  <p>Perhaps the rest of the packets are not shaped in the same way but I really can't understand why that would be. Perhaps I am writing the string in a faulty way to the <code>OutputStream</code>? I have no idea :(</p>
  
  <p>Note that I posted a question similar to this yesterday when I didn't properly understand the problem: 
  <a href="https://stackoverflow.com/questions/14774729/cant-post-json-to-server-with-http-client-in-java#comment20685628_14774729">Can&#39;t post JSON to server with HTTP Client in Java</a></p>
  
  <p>EDIT: 
  These are the possible results I get from running the <code>nc</code> command, I would expect to get the same messages to the InputStream if the OutputStream sends correct data in a correct way:</p>
  
  <p>Wrong arguments:</p>
  
  <pre><code>{"id":1,"error":{"code":-32602,"message":"Invalid entity type: subscribe"}}
  </code></pre>
  
  <p>Ok, successful:</p>
  
  <pre><code>{"id":1,"result":100}
  </code></pre>
  
  <p>Nothing to delete:</p>
  
  <pre><code>{"id":1,"result":0}
  </code></pre>
  
  <hr>
  
  <p>Wow, I really had no idea. I experimented with some different Writers like "buffered writer" and "print writer" and it seems the <code>PrintWriter</code> was the solution. Although I couldn't use the <code>PrintWriter.write()</code> nor the <code>PrintWriter.print()</code> methods. I had to use <code>PrintWriter.println()</code>. </p>
  
  <p>If someone has the answer to why other writers wouldn't work and explain how they would impact the data sent to the server I will gladly accept that as the solution.</p>
  
  <pre><code>    try {
          Socket socket = new Socket(InetAddress.getByName("x.x.x.x"), 3994);
          String string = "{\"id\":1,\"method\":\"object.deleteAll\",\"params\":[\"subscriber\"]}";
          DataInputStream is = new DataInputStream(socket.getInputStream());
          DataOutputStream os = new DataOutputStream(socket.getOutputStream());
          PrintWriter pw = new PrintWriter(os);
          pw.println(string);
          pw.flush();
  
          BufferedReader in = new BufferedReader(new InputStreamReader(is));
          String inputLine;
          while ((inputLine = in.readLine()) != null)
              System.out.println(inputLine);
  
          is.close();
          os.close();
  
      } catch (IOException e) {
          e.printStackTrace();
      }
  </code></pre>