I am trying to replace the Netcat command that I run on my terminal, which will reset some data on the server. The netcat command looks like this:
echo '{"id":1, "method":"object.deleteAll", "params":["subscriber"]} ' | nc xxxx 3994
I am trying to implement it in Java, since I would like to be able to call this command from the application that I am developing. I have problems with it, although the command is never executed on the server.
This is my Java code:
try { Socket socket = new Socket("xxxx", 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(); }
The code also hangs in a while loop that InputStream should read, I have no idea why. I use Wireshark to capture packets, and the data that comes out looks the same:
{"id":1,"method":"object.deleteAll","params":["subscriber"]}
Perhaps the rest of the packages are not formed the same way, but I really cannot understand why this would be. Perhaps I am writing an error string in an OutputStream ? I have no idea: (
Please note that I posted a question like this yesterday when I misunderstood the problem: Can't host JSON on a server with an HTTP client in Java
EDIT: These are the possible results that I get from running the nc command, I would expect to get the same messages in InputStream if OutputStream sends the correct data correctly:
Invalid arguments:
{"id":1,"error":{"code":-32602,"message":"Invalid entity type: subscribe"}}
Good, successful:
{"id":1,"result":100}
Do not delete anything:
{"id":1,"result":0}
Wow, I really had no idea. I experimented with some different writers, such as a "buffered writer" and a "writer", and it seems that the PrintWriter solution was a solution. Although I could not use the PrintWriter.write() and PrintWriter.print() methods. I had to use PrintWriter.println() .
If someone has an answer to the question why other authors do not work and explain how they will affect the data sent to the server, I will gladly agree with this as a solution.
try { Socket socket = new Socket(InetAddress.getByName("xxxx"), 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(); }