UDP File Transfer - Common Files

This UDP example is split into two projects, but they share some common code.  Feel free to just paste these in (I do recommend typing them in, but it can get lengthy), though make sure you read the summaries at the bottom of each.

 

Block

 

 

This is a little data container that's used to chop up a larger file into smaller chunks.  We have two constructors.  One for creating a new one with a set Number, and the other for reconstructing it from a byte array.  The GetBytes() method is used to well, get the Block as a byte array.

 

 

Packet

 

 

Packet is what we're bouncing around the inter-networks.  It has two only data fields, PacketType and Payload.  All Packets should have one of the types listed at the top under the region "Message Types."  Like with Block, there is also a second constructor to create a Packet from a byte array, and a GetBytes() method to get a Packet as a byte array.

At the bottom there are "specific packets," which are there to make working with Payload data easier with some message types.  You'll notice there isn't one for BYE as it doesn't need any Payload.

 

 

NetworkMessage

 

 

NetworkMessage is a tiny data structure that used to pair a Packet with a Sender.   These are only used when reading data we've received.

© 16BPP.net – Made using & love.
Back to Top of Page
This site uses cookies.