Chat Client/Server Part 2: Packet Extension Class
If 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 tutorial.
This is the second installment of a multi-post tutorial on writing network software using the dot net framework and the C# programming language. In this installment we will be going over the ChatPacket class which implements the Packet abstract class.
The ChatPacket Class
If you recall, our Packet class had three important elements.
- Type Code: This is an integer value which identifies the type of packet. It is transmitted across the connection for every packet, so that the receiving end knows how to interpret the packet it is receiving.
- Converts to Byte Array: Every packet which inherits from our base packet class should be able to convert itself into a byte array.
- Parses a Byte Array: The packet classes which inherit from the base class should implement a parse function which will take a byte array and read it into the proper packet structure.
These capabilities should be implemented by any class which inherits from Packet, so that the library stays consistent.
Let’s begin by creating a new class. Right-click on your Chat class library and add a new class called ChatPacket.cs. This file will be our main class for sending chat data back and forth.
Step 1: Inherit from Packet base class. Every packet class we create should inherit from the base Packet class. This is because the server and client code we will be using will use the base class in all of its operations.
1 | public class ChatPacket : Packet |
Step 2: Create the chat data property. Chat data is going to be just a string in our chat packet class. Let’s add a new property to our ChatPacket class.
1 | public string ChatData { get; set; } |
Chatdata is going to basically be the text of each chat packet that gets sent around. When you type in text to send out to the server (and the server sends it to the other clients) then this is the data that gets sent out.
Step 3: Constructor should set the Type of Packet. The next thing that must be done is that each packet must identify its type. This is so that the end point which receives every packet needs to know how to decode it into the correct class. In our constructor for each packet, we are going to tell the packet which type it is.
1 2 3 4 | public ChatPacket() { _packetTypeCode = 1; } |
Keep in mind that the type code should be unique to this type of packet. If we created another packet type in this application, its typecode should be set to 2 for each instance. ChatPacket is going to be packet type 1.
We are only going to send around ChatPackets, but it would be a good idea in a more complex program to use constants to make sure your packet types are consistent integer values.
Step 4: Implement ToByteArray() method. This is the method that will be called by our server to convert a packet into a stream of bytes. The network code will then send the bytes of the network, and the code at the other end will reconstruct the bytes into a packet.
1 2 3 4 | public override byte[] ToByteArray() { return Encoding.ASCII.GetBytes(ChatData); } |
The above code uses the built-in ASCII encoding capabilities of dot net to convert our string of packet data into an array of bytes which can be transmitted.
Step 5: Implement the Parse operation which reconstructs packets. The Parse function takes as input a series of bytes and should return the reconstructed packet object.
1 2 3 4 5 6 7 8 9 10 | public static new ChatPacket Parse(byte[] packetData) { ChatPacket p = new ChatPacket(); if(packetData.Length != 0) { p.ChatData = Encoding.ASCII.GetString(packetData); } return p; } |
As you can see, the above function is fairly simple. It just does the opposite of the ToByteArray function, in that it uses the built-in ASCII Encoding classes to construct a string from bytes rather than deconstruct a string into bytes.
It returns a new packet object.
Completing the ChatPacket Class
So what we have done here is to create a class which will our packet class. ChatPacket is a very simple packet class, because the only data that is sent in the packet is the text of the chat message. It is a good idea to keep your packets very simple, so they can be easily and quickly packaged and processed.
In the next portion of this tutorial series, we are going to move on to implement the Server class and program, which is where this all gets interesting!
Follow Dave on Twitter

how to send a packet from server to client using Qt programming?
Unfortunately I don’t know much about Qt.. sorry!