Send a JSON object to server over TCP connection in Java using Socket

So the other day I was struggling with sending a JSON object to a server. The JSON service would not accept this special object over HTTP so I had do dig into some Socket handling to send it. Of course, we could use Netcat to send the object like this:
echo '{"id":1, "method":"object.deleteAll", "params":["myParam"]}' | nc x.x.x.x 3994

But since the application I was working on needed to run some tests on creating and deleting stuff from the server we really needed a Java method to do this for us.

Here’s the code:

Creating the Socket is straight forward, the most noteworthy thing here the Writer that is being used. I tried with so many different Writers that I forgot what they all where called. Also note that the PrintWriter.println() method is used rather then the PrintWrite.print() method.