A comprehensive chat application consisting of a C++ TCP server, C++ console client, and Python Tkinter GUI client that all interoperate seamlessly.
/server.cpp - C++ TCP server with multithreading support
/client.cpp - C++ console client with full-duplex I/O
/client_gui.py - Python Tkinter GUI client
/README.md - This file
/chat.log - Created at runtime for message logging
- Object-oriented design with Connection base class, Server derived class, and ClientHandler composition
- TCP socket communication with IPv4 support
- Multithreaded architecture handling up to 100 concurrent clients using std::thread
- Thread-safe operations with std::mutex protection for shared state
- Message broadcasting - forwards messages from one client to all connected clients
- Private messaging support with
/msg <username> <text>
command - User management with unique username validation
- Message logging - timestamped messages appended to chat.log using fstream
- Graceful shutdown handling SIGINT/SIGTERM with proper socket cleanup
- Robust error handling for all socket operations
- Full-duplex I/O with separate threads for sending and receiving
- Command support for
/quit
,/msg <username> <text>
, and/who
- Real-time message display with server-provided timestamps
- Graceful disconnection handling
- Signal handling for clean shutdown on Ctrl+C
- Tkinter-based graphical interface with scrollable chat history
- Background threading for non-blocking message reception
- Thread-safe message queue for GUI updates
- Real-time chat with automatic scrolling
- Command support matching console client functionality
- Connection status display and error handling
- Entry field bound to Enter key for quick messaging
- Username transmission: First message from client is the chosen username (newline-terminated)
- Regular messages: Newline-terminated strings
- Server formatting: Messages prefixed with
[HH:MM:SS] <username>: <text>
- Private messages: Prefixed with
[HH:MM:SS] [PM] <sender>: <text>
- System messages: Server announcements for join/leave events
/quit
- Disconnect from server gracefully/msg <username> <text>
- Send private message to specific user/who
- List all connected users
- C++ compiler with C++17 support (g++ recommended)
- Python 3 with Tkinter (usually included in standard installations)
- POSIX-compliant system (Linux, macOS, or WSL on Windows)
# Build server
g++ -std=c++17 -pthread server.cpp -o server
# Build console client
g++ -std=c++17 -pthread client.cpp -o client
- Start the server:
./server 5555
- Connect console clients:
./client 127.0.0.1 5555
- Connect GUI client:
python3 client_gui.py 127.0.0.1 5555
- Start server:
./server 5555
- server should start and listen on port 5555 - Connect multiple console clients:
./client 127.0.0.1 5555
with different usernames - Message broadcasting: Send messages from one client, verify they appear on all others
- Message logging: Check that messages are logged to
chat.log
with timestamps - Username uniqueness: Try connecting with duplicate username, should be rejected
- Private messaging: From one client use
/msg <username> <message>
, verify target receives it - User listing: Use
/who
command, verify it returns comma-separated list of users - Graceful disconnect: Use
/quit
command, other clients should see disconnect message - Server persistence: Disconnect one client, verify server continues running for others
- GUI connection:
python3 client_gui.py 127.0.0.1 5555
should show GUI window - Cross-platform messaging: Send messages between GUI and console clients
- Real-time updates: Messages should appear immediately in GUI chat window
- Command support: GUI client should support all commands (private messages, user list)
- Connection handling: Close GUI window should disconnect gracefully
- Invalid server: Try connecting to non-existent server, should show error
- Server shutdown: Stop server while clients connected, clients should detect disconnect
- Network interruption: Simulate network issues, clients should handle gracefully
- Invalid commands: Send malformed commands, server should handle without crashing
- Multiple clients: Connect 10+ clients simultaneously
- Message flooding: Send rapid messages, server should handle without blocking
- Long-running: Leave server running for extended period, should remain stable
- Default port: 5555
- Binding: Server binds to
0.0.0.0
(all interfaces) - Client connection: Clients connect to specified IP and port
- Ensure server port (5555) is properly exposed in Replit environment
- If inbound connections are limited, run all components within same Replit shell sessions
- Use
0.0.0.0
for server binding to accept connections from all interfaces
- "Address already in use": Wait a few seconds after stopping server, or use different port
- "Connection refused": Ensure server is running before starting clients
- "Permission denied": Check firewall settings and port permissions
- GUI not starting: Verify Python3 and Tkinter are properly installed
Add -g -DDEBUG
flags when compiling for additional debug output:
g++ -std=c++17 -pthread -g -DDEBUG server.cpp -o server
g++ -std=c++17 -pthread -g -DDEBUG client.cpp -o client
- Server uses
std::mutex
for protecting shared client list and logging operations - All socket operations are handled with proper error checking
- Client disconnections are handled gracefully with thread cleanup
- Uses
std::shared_ptr
for automatic client handler cleanup - RAII principles applied for socket and file resource management
- No memory leaks in normal operation
- Server supports up to 100 concurrent connections (configurable)
- Message broadcasting is O(n) where n is number of connected clients
- Log file grows indefinitely (consider rotation for production use)
This project is provided as educational material. Feel free to modify and distribute.