A simple yet efficient real-time chat application implemented in C++ using socket programming. This application consists of a server and client component that enables multiple users to communicate in real-time over a network.
- Real-time Communication: Direct TCP socket communication between server and clients
- Multi-client Support: Server can handle multiple simultaneous client connections
- Cross-platform Compatibility: Works on Linux, Windows, and macOS
- Lightweight: Minimal resource usage with native C++ implementation
- Console-based Interface: Simple terminal-based chat interface
- Thread-safe Operations: Proper handling of concurrent client connections
- Language: C++
- Networking: TCP Sockets (Berkeley sockets / Winsock)
- Threading: POSIX threads (pthread) / Windows threads
- Build System: Make / CMake (depending on your setup)
- GCC compiler (g++) version 7.0 or higher
- POSIX-compliant system
- Make utility
- MinGW-w64 or Visual Studio with C++ support
- Windows SDK (for Winsock)
git clone https://github.com/Dipukumar1997/SocketChatApp.git
cd SocketChatApp# Compile server
g++ -o server server.cpp -lpthread
# Compile client
g++ -o client client.cpp -lpthread# Compile server
g++ -o server.exe server.cpp -lws2_32
# Compile client
g++ -o client.exe client.cpp -lws2_32./server
# or on Windows: server.exe./client
# or on Windows: client.exeSocketChatApp/
โโโ server.cpp # Server implementation
โโโ client.cpp # Client implementation
โโโ README.md # Project documentation
โโโ LICENSE # License file
-
Start the Server First:
./server
The server will start listening on a default port (usually 8080 or as configured).
-
Connect Multiple Clients:
./client
Each client will prompt for connection details and username.
-
Start Chatting:
- Type messages in any client terminal
- Messages will be broadcasted to all connected clients
- Use special commands (if implemented) like
/quitto exit
The server typically uses these default settings:
- Port: 8080 (see the console for {PORT})
- Max Connections: 10 (or as configured)
- Buffer Size: 1024 bytes (or as defined)
- Server IP: localhost/127.0.0.1 (default)
- Server Port: Must match server port
- Username: Prompted during connection
#define PORT 8080 // Change server port
#define MAX_CLIENTS 10 // Maximum concurrent clients
#define BUFFER_SIZE 1024 // Message buffer size#define SERVER_IP "127.0.0.1" // Server IP address
#define PORT 8080 // Server port (must match server)
#define BUFFER_SIZE 1024 // Message buffer size-
Single Client Test:
# Terminal 1 ./server # Terminal 2 ./client
-
Multiple Clients Test:
# Terminal 1: Server ./server # Terminal 2: Client 1 ./client # Terminal 3: Client 2 ./client # Terminal 4: Client 3 ./client
-
Network Test:
- Run server on one machine
- Connect clients from different machines on the same network
- Update client.cpp with server's actual IP address
Connection Refused:
- Ensure server is running before starting clients
- Check if the port is already in use
- Verify firewall settings
Compilation Errors:
# Linux: Install build essentials
sudo apt-get install build-essential
# Missing pthread library
sudo apt-get install libc6-devWindows Socket Errors:
- Ensure Winsock2 is properly linked (-lws2_32)
- Check Windows Defender firewall settings
Compile with debug flags for troubleshooting:
g++ -g -DDEBUG -o server server.cpp -lpthread
g++ -g -DDEBUG -o client client.cpp -lpthread-
Configure Server IP:
- Find server machine's IP address:
ifconfig(Linux/Mac) oripconfig(Windows) - Update client.cpp with the server's IP address
- Find server machine's IP address:
-
Firewall Configuration:
- Open the port used by the server (default 8080)
- Allow incoming connections for the server application
-
Run Across Network:
# On server machine ./server # On client machines ./client
- Main Thread: Accepts incoming connections
- Client Threads: Handle individual client communication
- Broadcast Function: Sends messages to all connected clients
- Connection Management: Maintains list of active clients
- Main Thread: Handles user input
- Receive Thread: Listens for incoming messages from server
- Send Function: Sends user messages to server
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch:
git checkout -b feature/new-feature
- Make your changes:
- Follow C++ best practices
- Add comments for complex logic
- Test thoroughly on different platforms
- Commit your changes:
git commit -m "Add new feature: description" - Push and create a Pull Request
- Use consistent indentation (4 spaces or tabs)
- Follow C++ naming conventions
- Add error handling for socket operations
- Include comments for complex algorithms
- Test on both Linux and Windows if possible
- GUI interface using Qt or GTK
- Private messaging between specific users
- File transfer capabilities
- Message encryption (SSL/TLS)
- User authentication system
- Chat rooms/channels
- Message history logging
- Configuration file support
- IPv6 support
- Reconnection handling
This project is licensed under the MIT License - see the LICENSE file for details.
Dipu Kumar
- GitHub: @Dipukumar1997
- Email: [[email protected]]
- C++ Socket Programming tutorials and documentation
- POSIX threading documentation
- Winsock API documentation
- Open source community for inspiration and best practices
If you encounter issues:
- Check the Issues section
- Create a new issue with:
- Operating system and version
- Compiler version
- Error messages or unexpected behavior
- Steps to reproduce the problem
โญ Star this repository if you found it helpful!