I'm currently experimenting with UDP-Communication between a server written in Python using the SocketServer class and a client written in Java using the DatagramSocket and DatagramPacket classes. The server accepts python method calls as an input and routes the stdout and stderr back to the client, transmitted in a 1024byte sized packet.
The communication is working, the client can receive packets from the server and send packets to it, however I'm running into problems when comparing data.
For example, when receiving a packet containing the string __DONE__\n
in the client, it prints fine using System.out.print(packet.getData())
. I'm only running into problems when I am trying to compare it to a String done = "__DONE__\n"
as follows:
while (String(packet.getData()).equals(done) != true) {
doStuff();
}
Here the loop runs forever, as the evaluated statement always returns false
.
My guess is that it has something to do with different encodings. I tried to compare the byte-arrays of both the string from the packet and the native Java string and got these results:
String done: 5f5f444f4e455f5f0a
String(packet.getData()): 5f5f444f4e455f5fa0000000[...]
// The 0s are repeated for the whole 1024bytes of the packet
It seems that the String from the datapacket contains the bytes I'm trying to compare as well as the other bytes from the 1024byte packet, which is why the String.equals() method always returns false.
Is there a way to force Java to omit the trailing zeros when converting from a byte array to a String?