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 Block of data.  Sent by the Receiver to the Sender after the INFO.
  • SEND - A response to a Block request, 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:

 

UDP File Transfer Diagram

 

Packet Data (Payloads)

Here's the data that some exchanges should contain.

  • REQF - Payload should contain a UTF8 encoded string that is the desired file.
    • An ACK must always be sent by the Sender.  If the file is present, it should contain the same Payload that was in the REQF, if not, then it should send an empty Payload.
  • INFO - First 16 bytes of the Payload must be a MD5 checksum of the original file (uncompressed).  The next 4 bytes are a UInt32 of the file size (in bytes).  The 4 bytes after that is the maximum Block size (in bytes),  The last 4 bytes are the total number of Blocks that will be transferred.
    • An ACK must be sent by the Receiver.  It should have an empty Payload.
  • REQB - Payload is an UInt32 number that is the Block.Number that is being requested by the Receiver.
  • SEND - Payload is a Block who's Number is that of one requested in a previous REQB.
  • BYE  - No Payload is needed.

 If you are confused, take a look at the diagram as it has some examples:

 

UDP File Transfer - Packet Examples
I'm sorry that the lines on the gridded paper didn't come out any clearer.  If you look really, really hard you should be able to see them.
© 16BPP.net – Made using & love.
Back to Top of Page
This site uses cookies.