1

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?

3 Answers 3

2

I now managed to resolve the issue by specifyng an offset of 0 and the length of the packet when converting my packet to a String:

String(packet.getData(), 0, packet.getLength(), "UTF-8");

The resulting String is stripped of the trailing 0s.

Sign up to request clarification or add additional context in comments.

Comments

0

It would seem to me that you can use the setLength before packet.getData to specify how many bytes you want to get from the buffer.

http://download.oracle.com/javase/1.4.2/docs/api/java/net/DatagramPacket.html#setLength%28%29

1 Comment

Please consider these possibilities: (0) the OP doesn't know in generality how many bytes he wants to get (1) setLength is something the SENDER might want to do (2) the RECEIVER (the Java code with the problem) might want to use getLength (3) using getLength is quite likely to return 1024, according to the OP
0

5f5f444f4e455f5fa is an ODD number of hex characters. Looks like it should be 5f5f444f4e455f5fa0 i.e. "__DONE__\xA0" rather than the "__DONE__" that you wrote. If not, why is that 'a0' in the incoming packet?

Isn't sending a 1024-byte packet padded out with NULs a bit wasteful? Perhaps you should be talking to the source of the packets.

8 Comments

Oh my, yes, thank you for the correction, the messages do indeed end with a newline. I edited my original post. Sorry!
@JeanMarieStaub: Your post is still not consistent. Your hex for your done string still has an ODD number of hex characters. Your hex output for the packet shows a0 (a NO-BREAK SPACE) not 0a (a newline). PLEASE copy/paste from actual output, don't type from memory.
The two hex-representations are actually taken from my terminal output. I generated them by parsing through the strings' byte arrays and adding together the format("%x", byte) string for each byte, maybe that's where the inconsistency is coming from. I removed nothing manually from my output, except for the last couple of hundred 0s from the second string. As for your second suggestion, I am probably going to look into that, however the server code was not written by me and at the moment I'm just trying to get a very rudimentary experimental version working to experiment with.
@JeanMarieStaub: aarrgghh (1) try "%02x " [leading zero, space separator for legibility] (2) Doesn't Java have an equivalent of Python's repr()? [reinventing the wheel as a polygon of few sides is not a good idea]
Sorry for the teethgrinding-inducing mistake, I'm only taking my very first steps with Java and this was a quick and dirty hack to see the byte representation of the strings as I didn't find an equivalent of repr() in Java (and according to stackoverflow.com/questions/1350397/… it doesn't seem to exist in the standard library). Although that's no excuse for that particular mistake, the format syntax is almost the same as in Python and C :-) After correcting it, the output is indeed 5f5f444f4e455f5f0a, not [..]fa as previously.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.