使用Java通过TCP发送JSON对象 [英] Sending a JSON object over TCP with Java
问题描述
我正在尝试替换我在终端中运行的Netcat命令,该命令将重置服务器上的某些数据。 netcat命令如下所示:
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
我一直在尝试用Java实现它,因为我希望能够从我正在开发的应用程序中调用此命令。我遇到了问题,该命令永远不会在服务器上执行。
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.
这是我的java代码:
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();
}
代码也挂起应该读取<$ c $的while循环c> InputStream ,我不知道为什么。我一直在使用Wireshark来捕获数据包,并且出去的数据看起来是一样的:
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"]}
也许其余的数据包没有以相同的方式塑造,但我真的不明白为什么会这样。也许我正在以错误的方式将字符串写入 OutputStream
?我不知道:(
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 :(
请注意,当我没有正确理解问题时,我昨天发布了一个与此类似的问题:
不能使用Java中的HTTP客户端将JSON发布到服务器
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
编辑:
这些是我从运行 nc
命令得到的可能结果,我会如果OutputStream以正确的方式发送正确的数据,则期望获得与InputStream相同的消息:
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:
错误的参数:
{"id":1,"error":{"code":-32602,"message":"Invalid entity type: subscribe"}}
好的,成功的:
{"id":1,"result":100}
无需删除:
{"id":1,"result":0}
哇,我真的不知道。我试验了一些差异像缓冲的作家和打印作家这样的作家似乎是 PrintWriter
的解决方案。虽然我不能使用 PrintWriter.write()
,也不能使用 PrintWriter.print()
方法。我不得不使用 PrintWriter.println()
。
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();
}
推荐答案
我认为服务器正在等待消息末尾的换行符。尝试使用原始代码与 write()
并在最后添加 \ n
以确认这一点。
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.
这篇关于使用Java通过TCP发送JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!