Chat-Application

A chat application allows users to interact or communicate by texting, audio, and video. It can be useful for social or commercial activities.

In this assignment, students are required to write a Chat Program based on Windows Socket Programming C++.
The program should allow users to chat concurrently on the Internet (i.e., User A can send encrypted messages to User B, and vice versa).
You can download the sample programs (namely, “server.cpp” and “client.cpp”) from the Web site. The program should consist of the following modules:

  1. User management: provide functions to add, delete, or group users;
  2. Group chat: allow users to chat as a group;
  3. Management of connections: support at least three concurrent connections.

User Management: Implemented By Server

  • Server: Store the User Information

Group chat: Group started by Client

  • Server Provide all users for Client User to select.

Management of connections: Give every User a Threads


I'm going to talk some details about how we implemented this chat room application.

Overview Of the Data structure

DataStructure

  • Sock: Client socket
  • messageQueue: Queue to store messages to be sent
  • queueMutex: Mutex to protect the message queue
  • queueCond: Condition variable to notify the send thread

Multithreading on both client and server side

ServerThreads

  1. Initially, our client was single-threaded, handling message receiving and sending alternately.
  2. This caused a problem: the client or server couldn't receive and send messages concurrently, leading to blocking and a poor user experience.

To solve this, we introduced multithreading.
Separate threads for receiving and sending to improve responsiveness and user experience.


Message queuing on the server side

Send

  1. On the server side, we introduced a mutex to ensure data transmission accuracy and thread safety.
  2. When multiple users send messages to the same user, the server pushes these messages into the receiver's message queue using a mutex to lock the process.
  3. This ensures orderly and complete message transmission.

These changes enabled the server to efficiently manage message queues in a multi-user environment, ensuring each user correctly receives their messages.