A robust command-line chat application implemented in C using Windows Sockets 2 (Winsock2) API. This application demonstrates basic client-server network programming concepts through a TCP/IP-based chat system that supports multiple concurrent client connections.
- ✅ Basic server functionality working
- ✅ Client connection successful
- ✅ Multiple client support confirmed
- ✅ Message sending operational
- ⏳ Username system (planned)
- ⏳ Message broadcasting (planned)
-
Compile the Programs
gcc chat_server.c -o server.exe -lws2_32 -lssl -lcrypto gcc chat_client.c -o client.exe -lws2_32 -lssl -lcrypto
-
Start the Server
- Open Command Prompt
- Navigate to your project directory
- Run:
server.exe
- Wait for "Server listening on port 8080..." message
-
Connect Clients
- Open new Command Prompt window(s)
- Navigate to project directory
- Run:
client.exe
- Start chatting!
- Open additional Command Prompt windows
- Run
client.exein each window - Each client can send messages independently
- Concurrent handling of up to 10 client connections
- Non-blocking I/O using select() for efficient message handling
- Real-time message broadcasting
- Automatic client disconnect detection
- Dynamic client socket management
- Detailed connection status logging
- Simple command-line interface for message input
- Persistent connection to server
- Real-time message transmission
- Local echo of sent messages
- Network Protocol: TCP/IP
- Socket Type: SOCK_STREAM (connection-oriented)
- Port: 8080 (configurable via PORT macro)
- Buffer Size: 4KB (4096 bytes)
- Client Management: Array-based client socket tracking
- I/O Multiplexing: fd_set based select() implementation
- Address Binding: INADDR_ANY (accepts connections on all network interfaces)
- Network Protocol: TCP/IP
- Socket Type: SOCK_STREAM
- Buffer Size: 1KB (1024 bytes)
- Connection Type: Persistent TCP connection
- Target Address: localhost (127.0.0.1)
- Windows operating system (Windows 7 or later)
- C compiler with Windows SDK support
- Minimum 64MB RAM
- Network connectivity for client-server communication
- C compiler (GCC, MSVC, etc.)
- Windows SDK or MinGW
- Winsock2 library (ws2_32.lib)
- Basic understanding of network programming concepts
# Compile the server with debugging symbols and OpenSSL
gcc -g chat_server.c -o chat_server -lws2_32 -lssl -lcrypto -Wall
# Compile the client with debugging symbols and OpenSSL
gcc -g chat_client.c -o chat_client -lws2_32 -lssl -lcrypto -Wall# Compile the server (assuming OpenSSL libraries/headers are installed)
cl chat_server.c /I C:\OpenSSL-Win64\include /link ws2_32.lib libssl.lib libcrypto.lib /LIBPATH:C:\OpenSSL-Win64\lib
# Compile the client
cl chat_client.c /I C:\OpenSSL-Win64\include /link ws2_32.lib libssl.lib libcrypto.lib /LIBPATH:C:\OpenSSL-Win64\libThis project now uses TLS for encrypting traffic between the client and server using OpenSSL.
-
Install OpenSSL for Windows (e.g., from the official binaries or vcpkg).
-
Generate a self-signed certificate and private key (for development):
openssl req -x509 -newkey rsa:2048 -keyout server.key -out server.crt -days 365 -nodes -subj "/CN=localhost" -
Place
server.crtandserver.keyin the same directory as the server executable. -
Start the server and client as before; the connection is now protected with TLS.
- Open Command Prompt with administrator privileges
- Navigate to the executable directory
- Execute the server:
chat_server.exe- Server initialization sequence:
- Winsock initialization
- Socket creation
- Address binding
- Listen state activation
- Ready for client connections
- Open a new Command Prompt window
- Navigate to the executable directory
- Launch the client:
chat_client.exe- Client connection sequence:
- Winsock initialization
- Socket creation
- Server connection establishment
- Message input prompt
- Type messages and press Enter to send
- Client Management: Uses a fixed array of MAX_CLIENTS (10) socket descriptors
- Message Handling: Asynchronous message processing using select()
- Buffer Management: Zero-initialized 4KB buffer for message handling
- Error Handling: Comprehensive error checking for socket operations
- Resource Management: Proper socket cleanup on client disconnect
- Connection Management: Single persistent connection to server
- Input Handling: Blocking fgets() for user input
- Buffer Management: 1KB message buffer
- Error Handling: Basic error checking for connection and send operations
- Fixed maximum client limit (10 concurrent connections)
- No message persistence
- No user authentication
- No encryption or security features
- Single-threaded implementation
- No private messaging capability
- Fixed buffer sizes
- No connection recovery mechanism
- Server performance may degrade with increasing client connections
- Large messages may require multiple send/receive operations
- No built-in flow control mechanisms
- Memory usage scales linearly with client connections
Potential enhancements that could be implemented:
- User authentication system
- Message encryption using SSL/TLS
- Database integration for message persistence
- Private messaging functionality
- File transfer capabilities
- Dynamic buffer sizing
- Connection recovery mechanism
- Multi-threading for improved performance
- GUI implementation
- Configuration file support
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch
- Implement your changes
- Add appropriate tests
- Submit a pull request
Please ensure your code follows the existing style and includes appropriate comments.
For issues, questions, or contributions, please:
- Create an issue in the repository
- Document the problem or suggestion clearly
- Include relevant code snippets or error messages
- Specify your environment details
- v1.0.0 - Initial release
- Basic client-server functionality
- Multiple client support
- Real-time message handling