将数据从 Java 客户端发送到 NodeJS 服务器

send data from java client to nodejs server

本文关键字:NodeJS 服务器 客户端 数据 Java      更新时间:2023-09-26

所以我将一个普通字符串从java客户端发送到服务器,服务器成功接收它。问题是接收到的数据不仅仅是我最初发送的字符串。

我用 DataOutputStream 在 java 中发送数据 -> writeUTF("欢迎...");

查看控制台输出

不使用 JSON.stringify()

节点JS客户端

connection --> 127.0.0.1
Data arrived type = string
Data = Welcome...
end
Disconnect --> 127.0.0.1

爪哇客户端

connection --> 127.0.0.1
Data arrived type = string
Data = 
Welcome...
end
Disconnect --> 127.0.0.1


with json.stringify()

节点JS客户端

connection --> 127.0.0.1
Data arrived type = string
Data = "Welcome..."
end
Disconnect --> 127.0.0.1

爪哇客户端

connection --> 127.0.0.1
Data arrived type = string
Data = "'u0000'nWelcome..."
end
Disconnect --> 127.0.0.1

所以我想知道当我从 Java 客户端收到消息时,我如何才能摆脱消息前面的内容?正常的 replace() 是没有意义的,因为它会有所不同。

问题可能出在 java 端。为什么要使用 DataOutputStream?尝试使用更适合发送文本的 OutputStreamWriter 类。