3

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();
    }
Community
  • 1
  • 1
span
  • 5,405
  • 9
  • 57
  • 115
  • Is the target x.x.x.x:3994 responding with any data? If not, your program will hang. – harpun Feb 09 '13 at 12:22
  • I'm getting packets back that I can see in wireshark but they contain no data. I only get an ACK for the packets sent and then the FIN/FIN ACK sequence. When I run the nc command in my terminal I either get an "error" if the syntax is wrong, or a "result = 0" if not successful or "result = 1" if successful execution. I'm updating the questions with the error messages that should appear in the inputstream. – span Feb 09 '13 at 12:25
  • So if you do not expect any output from the target confirming the JSON data transmission you could skip the while loop. Try commenting it out and see if this suites your needs. – harpun Feb 09 '13 at 12:26
  • Yes, I've tried without the inputstream and it didn't work. It seems the error is in the sent message. – span Feb 09 '13 at 12:31
  • Why don't you use Jersey and create a proper web service instead of handling sockets? – Srinivas Feb 09 '13 at 12:50
  • I'm not in control of the web service and I cannot change it in anyway. – span Feb 09 '13 at 12:51

1 Answers1

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.

iTech
  • 18,192
  • 4
  • 57
  • 80
  • Yeah, I agree completely on that. I wonder why though... It must be the server implementation using some code waiting for the new line to decide that the command is complete. Which is strange, since I do not add a new line in the nc command except to finish the bash command. There is no \n after the JSON object. Perhaps nc adds a new line by itself? – span Feb 09 '13 at 22:57
  • 1
    Wooops, it seems it does! http://stackoverflow.com/questions/11273999/new-line-issue-with-netcat – span Feb 09 '13 at 22:58