views:

615

answers:

4

hi all, i'm working on a network programming assignment about writing a simple IM system (pretty much like the simplest version of windows messenger).

the spec specifies that i must send over 4 fields of data in a single datagram packet, those are: To From Type Message where type refers to message type, implemented as a user defined enum class.

I would like to be taught how to pack all these datas into a single packet.

Thx.

UPDATE: thx for the help so far, but say i have String sentence and String From the normal way to patch the packet individually would be byte[] sendData = new byte [256]

sendData = sentence.getBytes(); but how exactly can i append the "from" string to sendData along with the sentence string?

+1  A: 

You simply append them before passing them to the network interface. Something along the lines of:

byte[] buff = new byte[256];
// Add al your fields here to buff.
DatagramPacket packet = new DatagramPacket(buff, buff.length, address, 1234);
socket.send(packet);

If they're not all strings, you'll need to encode them as such.

paxdiablo
simple though that might be, without delimiters it could be tricky to turn the single field back into the original four fields...
Alnitak
Or fixed-length fields. I assumed that would be covered by the spec mentioned. The actual insertion and extraction depends on that spec so, if more info is needed, it will need to be provided.
paxdiablo
that comment was written just before you changed to a byte array when all you had was four concatenated strings. It still holds true, though...
Alnitak
A: 

There are plenty of options for encoding the data, all of which come down to putting the four fields into one data structure, and then sending that all in one go.

The important part is that the encoding needs to show which of the four fields is at which point in the packet, otherwise the far end won't be able to decode it reliably.

Alnitak
For homework at this beginner level, I'd be using fairly simple data structures such as fixed length or a simple delimiter. You'll almost certainly be red-flagged as a potential plagiarist/cheat if you come up with a complicated solution like XML or serialization.
paxdiablo
yep - I did put DIY first, though :)
Alnitak
Oh, I see, at first I just assumed it was a new acronym I hadn't heard of yet (so many floating around). Just realized now it was the old favorite DIY :-)
paxdiablo
Java serialization is the exact opposite of a "complicated solution" - it's built into the language and requires less code than any of the others.
Michael Borgwardt
serialization is the way to go :) makes the solution really simple. And trust me, he/she is not going to get flagged as plagiarist for using serialization. That's the sort of stuff they'd expect him to know already at this level (if he/she's doing Java networking)
Peter Perháč
+2  A: 

You can send any Serializable object using something like this.

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buffer);
out.writeObject(objectYouWantToSend);
out.close();
buffer.close();
DatagramPacket packet = new 
    DatagramPacket(buffer.toByteArray(), 
                   buffer.size(), 
                   InetAddress.getByName(...),
                   portNumber);
socket.send(packet);
Peter Perháč
how would you deserialize this in the other end? I mean, as far as I know (I'm starting with Java) you have to pass a DatagramPacket (created with a fixed size buffer) to the receive method, how do you "guess" that size? Thanks.
Matías
are you sending huge amounts of data over UDP? note that some packets might not be delivered, or they may arrive in a different order. I believe the idea here is to agree on a fixed length of the datagram packets being sent and received, e.g. 4K chunks or even less, and then you don't have to guess. Just allocate 4K to the incoming message, receive() into this allocated packet, and go from there. If you are worried that the object sent might be more than 4KB in size, you should probably consider using a TCP socket.
Peter Perháč
thanks for your answer Peter
Matías
+1  A: 

Briefly, what you need to do is:

  1. create an object (class) which contains your 4 fields (from/to/enum/message)
  2. serialise this. It has to implement Serializable. See other solutions here for how to serialise
  3. convert to a byte array and send down the socket (see elsewhere for details)

At the other end you'll read this bytestream, deserialise and cast it to an instance of your class defined in 1. above.

By creating the one object containing the 4 fields and serialising this object, this allows you to send all four fields together (I get the impression that this is the stumbling block?).

Note that datagrams will have size limits imposed by the network transport layers, but for this application I suspect that's not a problem.

Brian Agnew