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 be- ACK'd by the Sender.
- INFO- Information regarding a transferable file. Sent by the Sender to the Receiver after the- REQF. Needs to be- ACK'd by the Receiver.
- REQB- A request for a- Blockof data. Sent by the Receiver to the Sender after the- INFO.
- SEND- A response to a- Blockrequest, containing Block data. Sent by the Sender to the Receiver of a corresponding- REQB.
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 the- Payloadmust be a MD5 checksum of the original file (uncompressed). The next 4 bytes are a- UInt32of the file size (in bytes). The 4 bytes after that is the maximum- Blocksize (in bytes), The last 4 bytes are the total number of- Blocks that will be transferred.- An ACKmust be sent by the Receiver. It should have an emptyPayload.
 
- An 
- REQB-- Payloadis an- UInt32number that is the- Block.Numberthat is being requested by the Receiver.
- SEND-- Payloadis a- Blockwho's Number is that of one requested in a previous- REQB.
- BYE- No- Payloadis needed.
If you are confused, take a look at the diagram as it has some examples:


