Java Multi-Client Chat Application
Overview
This is a command-line chat application built in Java, demonstrating fundamental concepts of network programming, including TCP Sockets and multithreading. It allows multiple users (clients) to connect concurrently to a single central server and exchange real-time messages.
This project is excellent for understanding how a simple client-server architecture manages multiple connections using Java's standard concurrency tools.
Key Features
Multithreaded Server: Uses Java's ExecutorService and a separate ClientHandler thread for each connected user, ensuring the server can handle multiple clients simultaneously without blocking.
Real-time Broadcast: Messages sent by any client are immediately broadcasted to all other active clients.
User Identification: Prompts each connecting client for a unique username.
Simple Protocol: Uses a basic, text-based protocol for communication between the Server and Client.
Exit Command: Supports the /quit command for clients to cleanly disconnect from the server.
Technologies Used
Language: Java (JDK 8 or later)
Networking: java.net.Socket, java.net.ServerSocket
Concurrency: java.util.concurrent.ExecutorService, Thread, Runnable
I/O: BufferedReader, PrintWriter
How to Run the Application
Prerequisites
Java Development Kit (JDK): Ensure you have Java installed on your system.
IntelliJ IDEA (Recommended): The project is structured to run easily within the IntelliJ IDE.
Step 1: Start the Server
Open the ChatServer.java file in IntelliJ.
Run the main method (click the green "Play" icon next to public static void main).
The console will show: Starting Chat Server on port 9001... Keep this terminal window running.
Step 2: Start the Clients
Open the ChatClient.java file in IntelliJ.
Run the main method. This will open the first client's console.
The client will prompt you: Enter your username:. Type a name (e.g., Alice) and press Enter.
To add more users, run the ChatClient.java file again, and enter a new username (e.g., Bob).
Repeat this for as many clients as you wish.
Chatting
Switch between the active client console tabs in IntelliJ.
Type your message and press Enter to send.
The message will be broadcast to all other active client windows.
To exit, type /quit and press Enter in the client window.
Code Structure
File
Description
ChatServer.java
Contains the main server logic (main method) and the ClientHandler inner class. Manages connections, maintains the list of active clients, and handles message broadcasting.
ChatClient.java
The client program. Connects to the server and uses two threads: one for receiving messages from the server, and one for reading user input from the console.