为什么Android在套接字的末尾发送奇怪的符号? ()

时间:2012-07-05 15:47:44

标签: java android sockets udp multicast

为什么Android会发送此符号?( )

我正在做的是,获取一些变量,完全放入它们,然后使用Multicast发送。符号仅出现在String的末尾,并重复一遍。

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);

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

byte[] buffer = new byte[256];

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

multicastSocket.receive(packet);

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

为什么会这样?

1 个答案:

答案 0 :(得分:3)

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

String msg = new String(packet.getData(), packet.getOffset(), packet.getLength());