Check Out Dave's New game for iPhone and iPod Touch: Smiled Out!

Chat Client/Server Part 1: Network Programming Primer and Packet Base Class

Pages: 1 2 3 4 5 6 7

Step 5: Add The Method to Serialize A Packet to a Byte Array. Now, this may only be one line of code, but it is a very important one. Our packet classes are going to serve two purposes. They will take their data and convert it all into a byte array that can be sent over the network, and they will take a byte array and convert it back into a packet object on the other side.

Every class which inherits from Packet must implement these two capabilities. This is the first one. Add the following abstract function to our Packet class definition.

1
        public abstract byte[] ToByteArray();

This function, when implemented by child classes, should return a byte array which can be sent over the network.

Step 6: Add a class (static) function which will take an array of bytes and return a Packet object. This is the reverse of the function we just added. This one will take a given array of bytes and reconstruct it into a meaningful object our server can work with.

Again, each class which inherits from Packet will need to override this function, but since it’s static we have to provide the implementation for even the Packet class. It just won’t do anything. Note that a class can get away with no implementing this function, but a protocol would not really be able to do anything if it can’t interpret a stream of bytes.

Add these lines of code to our packet class.

1
2
3
        public static void Parse(byte[] packetData)
        {
        }

Pages: 1 2 3 4 5 6 7


Follow Dave on Twitter
  1. [...] See the original post here: Program With Dave » Blog Archive » Chat Client/Server Part 1 … [...]

  2. [...] the original post:  Program With Dave » Blog Archive » Chat Client/Server Part 1 … By admin | category: chat software | tags: dot-net, manifest-itself, p2p-video, result, [...]

  3. [...] Address Already In Use: JVM_Bind:8080″. Now, if you’ve read my sockets tutorial network primer, you know that a port can only be bound for listening once. So apparently I had another process on [...]

  4. [...] you have not read Part 1 of this Multi-Post tutorial, check it out now. This tutorial will rely on the code written in that [...]

  5. [...] you have not read Part 1 and Part 2 of this network tutorial series, please do so now. The code in this post will build upon [...]

Leave a Reply