0
votes

Preface: I am NOT experienced at all and have likely made silly mistakes. Please correct these mistakes and teach me how to be better instead of berating me for being ignorant, if I wasn't already humbled by my lack of knowledge, I wouldn't need to ask for help. I have searched the internet for a solution but perhaps I'm choosing all the wrong keywords.

I have to use UDP and not TCP (too long to explain) and I need to send a large byte[] from client to server that will not fit into a single DatagramPacket. Therefore, I have decided to split the byte[] into chunks no longer than 1024 before creating my DatagramPackets

The Problem: Once the Server receives the packets, they are of course out of order. I need a way to re-assemble the variable number of DatagramPackets back into a single byte[]

My plan: Create a new byte[] with metadata about each byte[] in the above DatagramPacket (e.g. total packets after split, position of particular packet, packetgroup UUID) and pre-pend this to each datagramPacket.

byte[] (UUID idOfPacket + int positionOfPacket + int numberOfPackets) + byte[] data(not actual code just so you get an idea of how I want to arrange the byte[] incase my description was bad)

Because I have no idea how to determine when the metadata ends and the original packet data begins I thought I could use metadata that was a set length of bytes so I can always split at a certain point on the server and recombine based on info from metadata.

The Question: This seems like a bad idea because once deployed, I will never be able to adjust the format of the metadata. Is there a better way of approaching this?

1

1 Answers

0
votes

Your idea is not bad. Many protocols use it. You can either give enough bytes to allow for worst case scenarios.

For example:

idOfPacket:  16 bytes (128 bits)
positionOfPacket:  4 bytes (4294967296)
numberOfPackets:  4 bytes (4294967296)

Or what you can do is have a byte (or bytes) (lets call it the size byte) to tell you how long (in bytes) are those fields. But that's a lot harder.

sizeOfId:  1 byte (value would be 16 for this example)
sizeOfNumberOfPackets: 1 byte (value would be 4 for this example)
idOfPacket:  16 bytes (128 bits)
positionOfPacket:  4 bytes (4294967296)
numberOfPackets:  4 bytes (4294967296)