Simple client server chat

CPP, Sockets

Small client server chat application using sockets and windows forms for the GUI.

This application is a small project I made for a coursework in Denmark related with computer communications. It features multithread management of the clients connections to the server throw sockets. The server creates a chat session and wait until the users ask for connection. In that moment the server accept the connection and introduce the user in the chat room along with the others users.

Flow diagram

In the following graphic we can see the flow diagram of this chat. When we run the server we create a new thread for manage the connections, it thread creates a socket and wait listening in the selected port for new users.

Once that the user send a request, the listening thread register the petition and create another thread sending the connection information to it. In that new thread we create a new socket to manage the connection with the user (Leaving the main socket free for new users requests). After that we can start to send all the messages that we want between the client and the server until one of them disconnect.

Class diagram

The system has three main classes. The first one is CChatServer to the implemented in the server application and allows the server to create a connection, send packets, get the status of the connection and register all the users requests.

The second of is CChatClient that must be implemented in the user application and allow it to send request for connection and, after identification normal messages. Both messages from server and from user implement the interface CChatMessage and the allow types of message are:

  • ID_MSGTYPE_STATUS: General status information about the connection, server, new users, disconnections, etc.
  • ID_MSGTYPE_USERMSG: Normal use plain text message.
  • ID_MSGTYPE_NUDGE: Nudge (Vibration of the user window).
  • ID_MSGTYPE_SUPERNUDGE: Big nudge, produced by admin. (Big vibration of the user window).
  • ID_MSGTYPE_NEWUSER: New user connect to the server.
  • ID_MSGTYPE_USEREXIT: User disconnect from the server.
  • ID_MSGTYPE_ADMINMSG: Plain text message from the administrator.

Packet description

As it’s explained before a message can be of different types. The basic structure of one message or packet is the following:

  • Username: The name of the user that send the message.
  • MsgType: The type of the message sent (See before description).
  • Message: The message itself in plain text (Or parameters if the msgtype required that).