I'd like to ask you one more thing i don't get. I understand what your code for the sending packets is doing, but i don't get this: why for the same string, your array returns a different result from the packet_send = data.getBytes(); , where the "data" is the string. Shouldn't the printing result of these two arrays be the same?
No, they shouldn't be the same. When you retrieve the data from the packet, using the getData() method, you get an array of bytes. A String is an object, and has some extra data associated with it. Also, you're dealing with bytes, rather than characters. In Java, a character is stored in memory as two bytes, rather than just one.
And something last. I think that i understand what your code for the receiving packets is doing but i have a few questions. First of all, you store the data from the packet in a buffer "bis". Then you create an object from the class ObjectInputStream named "in". As parametres you pass the object "bis". Finally, you read the data with a method. Am i right till here?
All the data in a UDP packet is transferred in a special format, called UTF. Or it should, anyway, but you can certainly do it a bit differently if you want. The packets you receive from the server are being transferred in this format, using the same technique as we used to write data to a packet earlier, using a ByteArrayOutputStream and an ObjectOutputStream. When reading the data, you need to do the reverse. You need to use the wrapper classes to automatically handle the extra header data that comes in the packet. The stream classes provide a very easy mechanism of doint that.
The first thing we do is create a ByteArrayInputStream, which wraps around an array of bytes. You don't have any real reason to use it, other than the fact that it provides an InputStream object, which you can use as a parameter to another InputStream. The InputStream that we use is an ObjectInputStream. What it does is allow you to read objects from any kind of stream, you don't have to worry about what kind of stream you're reading from, be it a file, socket, or byte array. You just create the stream and say you want to read an integer, and it figures everything out and gives you an integer. So that's why we use an ObjectInputStream - it provides easy access to the underlying stream, so you don't have to bother with dirty details.
I don't get what you are saying about the order the data is stored in the packet. I tried to run your code in my code but i get an Exception
"IOException!java.io.StreamCorruptedException: invalid stream header".
No matter the order i write the methods i get this exception.
Can you explain this to me?
That's exactly the exception I was talking about. You can't just plug the code I wrote into your own code - you'll need to modify it some.
Here's the basic format of the data in a UDP packet:
typeCode:length:value:typeCode:length:value
It's not exactly like that, but it gives you an idea. So here's an example:
int:2:67:string:4:wolf
The data in the packet is an integer and a string, in that order. So what happens when I call readInt(), is it checks out the stream, sees that the type code matches the one expected (int), and that it has a length of 2. Then it picks off the next two characters of a string, converts them to an int, and returns it to you. Then it does it for the string.
Now say you call readUTF() first. It looks at the stream, and sees that the first type code is for an int. It's expecting a string, so it throws an exception, and you don't get anything.
So what you need to know is the order of the data in the packet. You need to know that the first field is an int, and the second is a string. Keep in mind that's not how it is, that's just an example. You need to take a look at the assignment, and it should tell you what order to expect the data.