Multithreaded Realtime Console Chat App ๐ฌ
- Project Description
- Usage
- Project Structure
- Future Improvements
- To be Fixed
- Screenshots
- Code Documentation
Multi-threaded console
chat application implemented in C, consisting of both server and client components. The server manages user contacts and messages, while the client provides a user-friendly interface to interact with the server. The communication between the server and client is facilitated through socket programming.
Programming Languages:
- C
Frameworks and Technologies:
- No frameworks used (only standard libraries)
- Only C standard libraries for Unix-like systems
- Csv file system for storing messages and contacts
- Standart Libraries --> Listed Only Important Ones:
- arpa/inet.h (for socket programming)
- unistd.h (for read and write functions)
- pthread.h (for multithreading)
- time.h (for date and time functions)
- sys/types.h (for socket programming)
- sys/stat.h (for file permissions)
- External libraries:
- ansiTerminalColors.h (written by me)
- Dependencies:
- gcc (GNU Compiler Collection)
+ Linux
+ MacOS
- Windows (needs to include winsock.h instead of arpa/inet.h)
It is well described with screenshots in Screenshots section.
.
โโโ Contacts
โ โโโ userID.csv // example
โ โโโ ...
โโโ Messages
โ โโโ userID.csv // example
โ โโโ ...
โโโ Public // this folder is not included in the project
โ โโโ Project ScreenShots
โ โโโ ...
โโโ Readme.md
โโโ ansiTerminalColors.h // for colored output
โโโ client // executable of client component
โโโ client.c // source code of client component
โโโ server // executable of server component
โโโ server.c // source code of server component
5 directories, 24 files
+ Add a proper database instead of CSV files (maybe SQLite or PostgreSQL)
+ Add a decent GUI (maybe with Qt)
+ Listing from a specified user (in messages)
- Bug: server crash when a client disconnects
- Bug: deleting a message from a user deletes all messages from that user
- Bug: returning to the main menu after deleting a message from a user returns to the same menu
-
Handler function for each client thread
void *handleClient(void *arg)
This function runs in a separate thread for each connected client. It handles incoming messages from the client, interprets user input, and performs corresponding actions.
Input void *arg (client socket) Output None
-
Getter function for the current date and time
Date getCurrentDateAndTime()
This function retrieves the current date and time and returns it as a
Date
structure.Input None Output Date (structure)
-
Response function for listing contacts
void listContacts(char *userID, int client_socket)
This function reads the user's contacts from a CSV file and sends them to the client.
Input char *userID, int client_socket Output Sends a list of contacts to the client
-
Response function for listing messages from a user
void listMessagesFromUser(char *userID, Message message, int client_socket)
This function reads messages from a CSV file, filters them based on the specified user, and sends the result to the client.
Input char *userID, Message message, int client_socket Output Sends messages from a specified user to the client
-
Response function for deleting a message
void deleteMessage(char *userID, Message message, int client_socket)
This function deletes a specified message from the user's message history and sends a confirmation or error message to the client.
Input char *userID, Message message, int client_socket Output Sends a confirmation or error message to the client
-
Response function for adding a user
void addUser(char *userID, Message message, int client_socket)
This function adds a user to the contact list and sends a confirmation message to the client.
Input char *userID, Message message, int client_socket Output Sends a confirmation message to the client
-
Response function for deleting a user
void deleteUser(char *userID, Message message, int client_socket)
This function deletes a specified user from the contact list and sends a confirmation or error message to the client.
Input char *userID, Message message, int client_socket Output Sends a confirmation or error message to the client
-
Response function for sending a message
void sendMessage(char *userID, Message message, int client_socket)
This function sends a message from the user to another user, updating the recipient's message history.
Input char *userID, Message message, int client_socket Output Sends a confirmation message to the client
-
Response function for checking messages
void checkMessages(char *userID, int client_socket)
This function reads the user's messages from a CSV file and sends them to the client.
Input char *userID, int client_socket Output Sends a list of messages to the client
-
Function for sorting messages in a CSV file
void sortTheCSVFileAccordingToDate(char *messagesCSVPath)
This function sorts the messages in a CSV file based on their dates.
Input char *messagesCSVPath Output None
-
Function for comparing two dates
int compareDates(const Date *date1, const Date *date2)
This function compares two date structures and returns the result.
Input const Date *date1, const Date *date2 Output Returns an integer (comparison result)
-
Function for creating CSV files if they don't exist
void createCSVIfNotExists(char *userID)
This function creates contacts and messages CSV files if they don't exist for the given user.
Input char *userID Output None
-
Function for displaying the menu
void displayMenu()
This function displays the menu options for the user.
Input None Output None
-
Request function for sending a message to the server
void sendMessageToServer(int client_socket, char *userID)
This function takes user input for sending a message, constructs a
Message
structure, and sends it to the server.Input int client_socket, char *userID Output Sends a message to the server
-
Request function for receiving messages from the server
void receiveMessagesFromServer(int client_socket, char *userID)
This function receives and displays messages from the server.
Input int client_socket, char *userID Output Displays messages received from the server
-
Request function for adding a user to the contacts list
void addUserToContacts(int client_socket, char *userID)
This function takes user input for adding a contact, constructs a
Message
structure, and sends it to the server.Input int client_socket, char *userID Output Adds a user to the contacts list
-
Request function for deleting a user from the contacts list
void deleteUserFromContacts(int client_socket, char *userID)
This function takes user input for deleting a contact, constructs a
Message
structure, and sends it to the server.Input int client_socket, char *userID Output Deletes a user from the contacts list
-
Request function for displaying the list of contacts
void displayContacts(int client_socket, char *userID)
This function requests and displays the list of contacts from the server.
Input int client_socket, char *userID Output Displays the list of contacts
-
Request function for displaying messages from a specified user
void displayMessagesFromUser(int client_socket, char *userID)
This function takes user input for a specified user, constructs a
Message
structure, sends it to the server, and displays the response.Input int client_socket, char *userID Output Displays messages from a specified user
-
Request function for deleting a message from a specified user
void deleteMessageFromUser(int client_socket, char *userID)
This function takes user input for a specified user and message, constructs a
Message
structure, sends it to the server, and displays the response.Input int client_socket, char *userID Output Deletes a message from a specified user
-
Function for clearing the console screen
void clearScreen()
This function clears the console screen.
Input None Output None
-
Macro function for printing colored text to the console
// XXX is the color name #define LogXXX(x) printf(ANSI_COLOR_XXX x ANSI_COLOR_RESET)
This function prints colored text to the console.
Input char *text, char *color Output None