UDP File Transfer - Protocol Design
This protocol is going to be a bit complex. Since UDP doesn't have ACKs built in, we're going to have to add them manually. Not every type of message with have an ACK, just some control ones. The Packet class (code in next part), contains two fields, PacketType (control) and Payload (data). For each type, the data in Payload will be structured differently.
Block
File data will be transferred in Blocks. They have simply two fields, an unsigned 32 bit integer that is it's ID Number, and a byte array that is the contained Data. Typically the Data in the Block is compressed. The code for this class is in the next part.
Packet Types
These are used to tell our app what each datagram is supposed to mean, they are:
ACK- An acknowledgement. Mostly for control messages, sent by both sides.BYE- An "end transfer," message. Can be sent by either the Sender or Receiver at any time.REQF- A request for a file. Sent by the Receiver to the Sender. Needs to beACK'd by the Sender.INFO- Information regarding a transferable file. Sent by the Sender to the Receiver after theREQF. Needs to beACK'd by the Receiver.REQB- A request for aBlockof data. Sent by the Receiver to the Sender after theINFO.SEND- A response to aBlockrequest, containing Block data. Sent by the Sender to the Receiver of a correspondingREQB.
Here is a helpful diagram that explains how the exchanges work:
Packet Data (Payloads)
Here's the data that some exchanges should contain.
REQF-Payloadshould contain a UTF8 encoded string that is the desired file.- An
ACKmust always be sent by the Sender. If the file is present, it should contain the samePayloadthat was in theREQF, if not, then it should send an emptyPayload.
- An
INFO- First 16 bytes of thePayloadmust be a MD5 checksum of the original file (uncompressed). The next 4 bytes are aUInt32of the file size (in bytes). The 4 bytes after that is the maximumBlocksize (in bytes), The last 4 bytes are the total number ofBlocks that will be transferred.- An
ACKmust be sent by the Receiver. It should have an emptyPayload.
- An
REQB-Payloadis anUInt32number that is theBlock.Numberthat is being requested by the Receiver.SEND-Payloadis aBlockwho's Number is that of one requested in a previousREQB.BYE- NoPayloadis needed.
If you are confused, take a look at the diagram as it has some examples:


