英文:
Sending String from Java client to NodeJS server via socket also sends unwanted characters
问题
以下是您要翻译的内容:
我想从我的Java客户端通过套接字将一个String数组发送到我的NodeJS服务器。我的意图更复杂,但我认为一切都可以简化为以下几行代码:
Java
Socket s = new Socket("localhost", 6666);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF("test"); // 也尝试过writeChars和writeBytes
NodeJS
require('net').createServer(function (socket) {
console.log("connected");
socket.on('data', function (data) {
console.log(data.toString())
});
}).listen(6666);
只需运行服务器,然后运行客户端,我得到以下结果:
我意识到发送数据的方式存在问题。也许它需要某种序列化,但我找不到一些清晰的步骤来做到这一点。谢谢。
英文:
I want to send an array of String from my Java client to my NodeJS server using sockets. My intentions are more complicated, but I think everything can be reduce to the next lines of code:
Java
Socket s = new Socket("localhost", 6666);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF("test"); // also tried writeChars and writeBytes
NodeJS
require('net').createServer(function (socket) {
console.log("connected");
socket.on('data', function (data) {
console.log(data.toString())
});
}).listen(6666);
Simply running the server, then the client, I get:
I am aware there's a problem with the way I send the data. Maybe it needs some kind of serialization, but I couldn't find some clear steps to do this. Thank you.
答案1
得分: 2
使用OutputStreamWriter替代DataOutputStream:
Socket s = new Socket("localhost", 6666);
OutputStreamWriter out = new OutputStreamWriter(s.getOutputStream(), StandardCharsets.UTF_8);
out.write("test");
out.flush();
DataOutputStream
应该与Java中的DataInputStream
类一起使用,它们使用的数据交换格式有点非标准。特别是writeUTF
方法:它首先将字符串的长度以2字节二进制数的形式写入。然后,它使用非标准字符编码写入字符串的字符。
如果您正在与非Java编写的软件交换信息,最好不要使用DataOutput/InputStream类。
英文:
Use OutputStreamWriter instead of DataOutputStream:
Socket s = new Socket("localhost", 6666);
OutputStreamWriter out = new OutputStreamWriter(s.getOutputStream(), StandardCharsets.UTF_8);
out.write("test");
out.flush();
DataOutputStream
is meant to be used with the DataInputStream
Java class, and the data exchange formats that they use are somewhat non-standard. This goes especially for the writeUTF
method: It starts by writing the length of the string as a 2-byte binary number. Then it writes the characters of the string, using a non-standard character encoding.
If you are exchanging information with software not written in Java, it's easier to just not use the DataOutput/InputStream classes.
答案2
得分: 0
根据我的经验,通过套接字发送字符串的最安全方式是像这样发送它的字节数组:
public static void main(String[] args) {
try (Socket s = new Socket("localhost", 6666)) {
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF("test utf");
dout.writeUTF("hello from ☕");
dout.writeUTF("\n");
dout.write("test byte[]".getBytes());
dout.write("hello from ☕".getBytes());
dout.write("I support unicode ✁ ✂ ✃ ✄ ✆".getBytes());
} catch (IOException ioe) {
// do nothing
}
}
但即使我复制您的代码,我也无法重现未知字符:
~/Projects/scratch via ⬢ v12.18.3
➜ node server.js
connected
test utfhello from ☕
test byte[]hello from ☕I support unicode ✁ ✂ ✃ ✄ ✆
英文:
On my experience the safest way to send a string through a Socket is by sending it's byte array like this:
public static void main(String[] args) {
try (Socket s = new Socket("localhost", 6666)) {
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF("test utf");
dout.writeUTF("hello from ☕");
dout.writeUTF("\n");
dout.write("test byte[]".getBytes());
dout.write("hello from ☕".getBytes());
dout.write("I support unicode ✁ ✂ ✃ ✄ ✆".getBytes());
} catch (IOException ioe) {
// do nothing
}
}
But even by replicating your code I wasn't able to reproduce the unknown character:
~/Projects/scratch via ⬢ v12.18.3
➜ node server.js
connected
test utfhello from ☕
test byte[]hello from ☕I support unicode ✁ ✂ ✃ ✄ ✆
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论