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 Block
s. 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 aBlock
of data. Sent by the Receiver to the Sender after theINFO
.SEND
- A response to aBlock
request, 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
-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 samePayload
that was in theREQF
, if not, then it should send an emptyPayload
.
- An
INFO
- First 16 bytes of thePayload
must be a MD5 checksum of the original file (uncompressed). The next 4 bytes are aUInt32
of the file size (in bytes). The 4 bytes after that is the maximumBlock
size (in bytes), The last 4 bytes are the total number ofBlock
s that will be transferred.- An
ACK
must be sent by the Receiver. It should have an emptyPayload
.
- An
REQB
-Payload
is anUInt32
number that is theBlock.Number
that is being requested by the Receiver.SEND
-Payload
is aBlock
who's Number is that of one requested in a previousREQB
.BYE
- NoPayload
is needed.
If you are confused, take a look at the diagram as it has some examples: