【发布时间】:2013-04-05 21:07:07
【问题描述】:
我想通过套接字将字符串从我的 Android 设备发送到我的 node.js 服务器。连接已经工作了,但是每当我发送字符串时,服务器都会收到额外的字符。
这是我将字符串“hans”发送到 node.js 服务器时收到的内容:
Buffer ac ed
Buffer 00 05 77 04 68 61 6e 73
作为 utf8 字符串:
??
♣w♦hans
这是发送字符串的 Java 部分:
clientSocket = new Socket("xxx.xxx.xxx.xxx",9988);
ObjectOutputStream clientOut = new ObjectOutputStream(clientSocket.getOutputStream());
String sendString = "hans";
clientOut.write(sendString.getBytes());
clientOut.flush();
那么为什么会这样呢?
【问题讨论】:
-
您的接收代码是什么样的?你怎么希望接收者知道字符串有多长?
-
只使用
OutputStream而不是ObjectOutputStream。
标签: java android node.js sockets