lundi 20 avril 2015

Java: Why does my String get corrupted over UDP?

I'm sending an UDP-Datagram with only one string as content and I'm creating the packet like this:

String content = ALIVE + "," + clusterName + "," + nodeName + "," + location;
byte[] data = content.getBytes();
packet = new DatagramPacket(data, data.length);

The problem is, when it arrives it has some weird binary data at the end, which can't be displayed as characters (in Sublime Text it just shows several a NUL-symbol).

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

I extract the different information parts with a string tokenizer(,) and solved this problem now by just adding another , at the end before sending it. Still I would like to know, where does this data come from?

Aucun commentaire:

Enregistrer un commentaire