A robust, multi-threaded chat server implementation in Java that allows multiple clients to connect and communicate in real-time. The server supports both broadcast and private messaging capabilities, with a thread pool architecture for efficient client handling.
- Multi-client support using thread pool architecture
- Real-time message broadcasting
- Private messaging between users
- User management (username changes, listing connected users)
- Command-based interface
- Graceful shutdown handling
The application consists of three main components:
- Main: Entry point of the application that initializes and starts the server
- Server: Manages incoming connections and maintains the thread pool
- ServerWorker: Handles individual client connections and message processing
- Default Port: 8080
- Thread Pool Size: 10 concurrent connections
- Connection handling: Non-blocking using Java NIO
Users can interact with the server using the following commands:
/w <username> <message>
- Send a private message to a specific user/h
- Display help menu with available commands/c
- Change username/l
- List all connected users/q
- Quit the chat server
- Java Development Kit (JDK) 8 or higher
- Java IDE (recommended: IntelliJ IDEA or Eclipse)
- Compile the Java files:
javac io/codeforall/fanstatics/*.java
- Run the server:
java io.codeforall.fanstatics.Main
The server will start and listen for connections on port 8080.
Clients can connect to the server using any TCP client (such as Telnet or a custom client application):
telnet localhost 8080
The server implements several thread-safety measures:
- Synchronized access to the shared client list
- Thread pool management for controlled resource usage
- Proper connection cleanup on client disconnect
The implementation includes:
- Graceful shutdown procedures
- Connection error handling
- Runtime exception management
- Resource cleanup in case of failures
Feel free to submit issues and enhancement requests!
Potential improvements that could be added:
- SSL/TLS support for secure communications
- Persistent chat history
- User authentication
- Custom room creation
- File sharing capabilities