套接字连接 – Java添加字节

我想通过套接字从我的Android设备发送一个string到我的node.js服务器。 连接已经工作,但是每当我发送string,服务器接收额外的字符。

这是我发送string“hans”到node.js服务器时收到的内容:

Buffer ac ed Buffer 00 05 77 04 68 61 6e 73 

作为utf8string:

 ?? ♣w♦hans 

这是发送string的Java部分:

 clientSocket = new Socket("xxx.xxx.xxx.xxx",9988); ObjectOutputStream clientOut = new ObjectOutputStream(clientSocket.getOutputStream()); String sendString = "hans"; clientOut.write(sendString.getBytes()); clientOut.flush(); 

那么为什么会这样呢?

您正在使用用于发送序列化对象的ObjectOutputStream ,并且必须由另一端的ObjectInputStream解码。 如果你只是在另一端发送字节和读取字节,那么你应该只是使用一个OutputStream和一个InputStream