Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] UDP DatagramPacket - data field corruption ?

Started by Skyy Dec 18, 2009 at 12:04 PM 4 replies 1.8k views
Original Post
Skyy
Skyy
Hello fellow devs, I'm facing a really silly problem. I'm just trying to implement a simple UDP feature for the client and server to talk to each other, everything works there. But when I actually came to parse the messages I found an interesting problem. The data field for some reason keeps corrupting/changing all the time. I just send a simple message to the client saying "Hi, I'm the server" and when the client reads it, it sees it as "Hi, I'm the server[random characters]". Naturally when I come to parse it, it just blows up. Here is a screenshot of the problem. At the top of the picture you can see the "Hi, I'm the server" String being created and at the bottom you can see the console of the Client and how the Client sees the message from the server. It worked just fine earlier but when I came back home with my work and slammed the project into my own Eclipse, this is what I got out of it. Any ideas what's going on? Or how to possibly solve this. Naturally I'd love to get rid of those weird characters completely since I'm not adding them there myself... Cheers. // Skyy
Erik Rufelt
Erik Rufelt
It seems you have a string-termination problem. A string needs to end with a 0, the value zero. If you send the string "Hi", and you only send two characters, you must make sure to append a string-terminating zero after the last received character, on the receiving side. If you're lucky there might already be a zero there, which is why it might work sometimes, or on some compilers.
Skyy
Skyy
so I have to add the terminating character. Alright, turns out to be a lot easier than I originally though, just a small silly thing. Is there a specific way to add it to the end of the String in Java, or?
rip-off
rip-off
You need to be able to define the length of your messages. Either prefix them with the length (you'll have to decide how to encode the length too), or use a delimiter. The character '\0' is commonly used for strings.

If the entire packet is to be interpreted as a string, just use the getLength() and getOffset() functions of the DatagramPacket to determine which areas of the buffer are valid. This does assume that the sender is sending only the exact string and not any additional data.
Erik Rufelt
Erik Rufelt
Quote:
From String (Java 2 Platform SE 5.0)
String(byte[] bytes, int offset, int length)
Constructs a new String by decoding the specified subarray of bytes using the platform's default charset.


So instead of just doing new String(p.getLength()), you do new String(p.getData(), 0, p.getLength()), to tell the String to stop after a certain number of bytes. (I assume you are receiving bytes into an array of some max-length, and that the String gets constructed from the entire array, even though fewer bytes are actually received).

In the long run it's better to use packet headers or similar, to tell the receiver what it is receiving.
Skyy
Skyy
Thank you guys. Solved.
I have to admit, interesting "problem" on it's own. Definitely had fun with this one all though it completely stopped me on my tracks for a while. Heh.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.