Sending a JSON object over TCP with Java

2019-04-14 07:11发布

问题:

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:

echo '{"id":1, "method":"object.deleteAll", "params":["subscriber"]} ' | nc x.x.x.x 3994

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.

This is my java 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();
}

The code also hangs on the while loop that should read the InputStream, I have no idea why. I've been using Wireshark to capture the packets and the data that is going out looks the same:

{"id":1,"method":"object.deleteAll","params":["subscriber"]}

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 OutputStream? I have no idea :(

Note that I posted a question similar to this yesterday when I didn't properly understand the problem: Can't post JSON to server with HTTP Client in Java

EDIT: These are the possible results I get from running the nc command, I would expect to get the same messages to the InputStream if the OutputStream sends correct data in a correct way:

Wrong arguments:

{"id":1,"error":{"code":-32602,"message":"Invalid entity type: subscribe"}}

Ok, successful:

{"id":1,"result":100}

Nothing to delete:

{"id":1,"result":0}

Wow, I really had no idea. I experimented with some different Writers like "buffered writer" and "print writer" and it seems the PrintWriter was the solution. Although I couldn't use the PrintWriter.write() nor the PrintWriter.print() methods. I had to use PrintWriter.println().

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.

    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();
    }

回答1:

I think the server is expecting newline at the end of the message. Try to use your original code with write() and add \n at the end to confirm this.