Linux Networking
For my networking class we had to create a client and server to pass a file back and forth successfully. Setting up the network sockets is pretty easy, everyone has read beej's guide, but the trick was in setting up the data to send and to ensure that I was getting data I expected. I could have just sent the data and tried to deal with it but I decided I would create a basic packet header to ensure I was getting my data and the length of the data. This header is super simple and contains 3 four byte integers. The first one is actually just HEAD spelled out in ascii (which if necessary would give the endianness of the sending system, a trick I learned from the Halo 2 map file layout), the second one is the length of the message being sent in case we have to read multiple times, and the third one is a type field. All of this together allowed me to determine if the data being received originated from my server process and allowed for easier handling of data. The type field was really important for this project because we were required to emulate the way that FTP opens two connections when transferring: a command connection, and a data connection. Setting up my packet header with a type field allowed me to use the same code for both command and data connections by simply changing the type. In the end this worked out really well and proved to be a simple file transfer solution.
Check out the full article for the code snippets!