Note: Please run these codes using Visual Studio and not Visual Studio Code.
This project is a simple chat application that demonstrates a server-client model using C++ with sockets and threads. The server can handle multiple clients, allowing them to connect and communicate with each other. The server manages client connections, broadcasts messages, and handles disconnections gracefully. The client connects to the server and allows users to send and receive messages.
- Server Code: Manages client connections, receives and broadcasts messages, and handles disconnections.
- Client Code: Connects to the server, sends messages, and receives broadcasts from the server.
- Multiple clients can connect to the server simultaneously.
- Messages sent by a client are broadcasted to all other connected clients.
- Pending connection requests are managed using a queue.
- Disconnections are handled gracefully, and pending connections are processed.
- C++: The primary programming language used for implementing the server and client.
- Winsock API: Used for socket programming to manage network communications.
- Multithreading: Used in the client code to handle message sending and receiving concurrently.
- Standard Library: Utilized various standard library components such as queues and strings.
- Environment: This project is designed to run on Windows, using the Winsock API for socket programming. Ensure you have the Windows SDK installed.
- Development Tools: Use Visual Studio for compiling and running the code. Visual Studio provides the necessary environment for compiling Winsock applications.
- Pending Connections: The server maintains a queue of pending connections when the maximum client limit is reached. Once a client disconnects, a pending connection is processed.
- Graceful Disconnection: Both server and client handle disconnections gracefully, ensuring resources are cleaned up properly.
- Linker Settings: Make sure to add wsock32.lib in the Project Properties -> Linker -> Input section. This is necessary for the code to work properly.
The server code initializes Winsock, creates a socket, binds it to a port, and listens for incoming connections. It uses a select call to handle multiple client connections and broadcasts messages received from any client to all other connected clients. Disconnections and pending connections are managed appropriately.
The client code connects to the server, sends messages typed by the user, and receives broadcasts from the server. It uses a separate thread for receiving messages to allow simultaneous sending and receiving of messages.
- Server:
- Open the server code in Visual Studio.
- Compile and run the server code.
- The server will start listening on port 9909.
- Client:
- Open the client code in Visual Studio.
- Compile and run the client code.
- Connect to the server by running multiple instances of the client code.
- Start the server.
- Run multiple instances of the client code.
- Type messages in any client instance and see the messages broadcasted to all other connected clients.
This project demonstrates a basic chat application using sockets and threads in C++. It serves as a foundational example for understanding server-client communication, handling multiple connections, and managing message broadcasting.
For further enhancements, consider adding features such as user authentication, encryption for secure communication, and a graphical user interface.
Feel free to explore the code and modify it to suit your needs!
My name is Vikas Prajapati, and I am currently pursuing BTech in Computer Science and Engineering from IIT Jammu. This project is part of my ongoing efforts to explore and understand network programming and concurrent processing using C++.