2

为什么 Android 会发送这个符号?(�)

我正在做的是,获取一些变量,将它们放在一起,然后使用多播发送。这些符号只出现在字符串的末尾,并且重复很多。

byte[] buffer;

String temp1 = "temp1=" + "37";
String weight1 = "weight1=" + "68";
String bpm = "bpm=" + "128";
String angBack = "angback=" + "90";
String angLeg = "angleg=" + "90";
String angBed = "angbed=" + "91";

String msg = "status:teste," + temp1 + "," + weight1 + "," + bpm + "," + angBack + 
              "," + angLeg + "," + angBed;

buffer = msg.getBytes();

InetAddress group = InetAddress.getByName("230.0.0.1");

DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, 5556);
socket.send(packet);

然后在另一边,我得到了字符串,这些符号出现在字符串的末尾。

byte[] buffer = new byte[256];

DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

multicastSocket.receive(packet);

String msg = new String(packet.getData());

为什么会这样?

4

1 回答 1

3

这不是一个奇怪的符号,它只是0x00.
您正在写入整个缓冲区,请检查数据长度。

String msg = new String(packet.getData(), packet.getOffset(), packet.getLength());
于 2012-07-05T15:52:44.143 回答