2012-07-05 47 views
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()); 

這是怎麼發生的?

回答

3

這不是一個奇怪的符號,它只是0x00
你正在寫整個緩衝區,檢查數據長度。

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

哦,哇,我搞砸了。謝謝。 – 2012-07-05 15:57:11

+0

沒問題。我也遇到過:) – pawelzieba 2012-07-05 15:59:08

相關問題