简体   繁体   中英

Sending String from Java client to NodeJS server via socket also sends unwanted characters

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.

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.

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 ✁ ✂ ✃ ✄ ✆

terminal output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM