In the last months I worked on a chat program built in C# using the .NET socket library. The result is what is described in the post you are reading.
Before moving on, let me say that this program is not perfect. There is ample room for improvement in many areas, like security, exception handling, robustness, and user interface, and probably it can’t handle billions of users like Whatsapp, but I can say that more or less… it works!
The goal was to create something like Skype or Whatsapp where the user can create a contact list, manage it and chat with the contacts in the list.
The architecture
The system is made up of a server part, which is a program listening on a configurable TCP/IP port, and at least two clients who use the server as a means of communicating with each other.
A client communicates with the server using a predefined set of messages, that resemble something like:
- “I’m Client1 give me the contact list”
- “I’m Client1 ask Client2 if I can add him in my contact list”
- “I’m Client1 send the message ‘Hello!’ to Client2”
Received a message from a client, the server responds with the outcome of the message and eventually some data, depending on the message type. If the message involves a second client, such as the one to send a chat message in which you have a sender and a recipient, the server sends a push notification to the recipient to inform him that there is something new. The recipient sends a message to get the updated chat message list.
For each type of message a connection is opened and closed immediately after use. The only connection that is always kept open is the one used for push messages.
A Sql Server database is used to store all the users, contacts and chat messages.
When you download the source code, you will find two Visual Studio solutions, one for the server and one for the client. You will see some project references to a base service library and a push notifications library. These libraries are also developed by the Dummy Programmer and you can download them .
I will not go to the bottom with too many explanations… you will have the possibility to deepen it examining the source code, which you can download here 😉
Quick start
Now I would like to give you some information to immediately see the chat at work. We will see how to configure the applications to run in a “production” environment. Note that the configuration files as they are, allow you to run both clients and server in your own development environment.
The server application
Let’s start by preparing the server application. Make sure you have a server with .NET Framework 4.7.2 and Sql Server installed (for the latter a 2016 Express or above it’s ok). The server must be reachable from any of the client you want to connect. If you want to make a “worlwide” chat, you server must have a public IP address.
Create an empty database in your server’s Sql Server instance and run the script “CreateDatabase.sql” you can find in the database project contained in the TDP.ChatServer solution.
Next build the TDP.ChatServer project an copy the outcome in a folder on the server. In the configuration file “TDP.ChatServer.exe.config” check the following parameters:
- PushLib:ListenIPAddress: it is the IP address used by the push notification library. Usually you have to enter an IP address which is reachable by the clients
- PushLib:ListenPort: it is the TCP/IP port used by the push notification library. Check the firewall configuration, as this port must be open and reachable by the clients
- Chat:ListenIPAddress: it is the IP address used by the chat service. Usually you have to enter an IP address which is reachable by the clients
- Chat:ListenPort: it is the TCP/IP port used by the chat service. Check the firewall configuration, as this port must be open and reachable by the clients
- DBConnectionString: this is the connection string that the server will use to connect to your Sql Server database, ensure it is correct
The client application
To prepare the client application, open the solution TDP.ChatClient and build the project TDP.ChatClient. Copy the outcome on each client you want to use. In the configuration file “TDP.ChatClient.exe.config” che the following parameters:
- PushLib:ConnectIPAddress: it is the IP address used by the push notification library. The IP address must match the one configured in the server parameter “PushLib:ListenIPAddress”
- PushLib:ConnectPort: it is the TCP/IP port used by the push notification library. The port number must match the one configured in the server parameter “PushLib:ListenPort”
- Chat:ConnectIPAddress: it is the IP address used by the chat service. The IP address must match the one configured in the server parameter “Chat:ListenIPAddress”
- Chat:ConnectPort: it is the TCP/IP port used by the chat service. The port number must match the one configured in the server parameter “Chat:ListenPort”
Now you can execute the client program… in the next post I will give you some instructions on how to use the client.
See you in the next post, and forgive me if you will find some bug! 😉