This project is a classic client-server chat application built entirely in Java. It demonstrates the fundamentals of network programming, concurrency, and basic GUI development. The server is capable of handling multiple simultaneous client connections, broadcasting messages in real-time, and managing the state of the chat room. The client is a lightweight graphical applet that connects to the server to send and receive messages.
- Multi-Client Support: The server can accept and manage connections from up to 50 clients simultaneously.
- Real-Time Broadcasting: Messages sent by any client are instantly broadcast to all other connected clients.
- Concurrent Architecture: The server is multi-threaded, spawning a dedicated thread to handle each client connection, ensuring the main server loop remains non-blocking and responsive.
- State Management: The server gracefully handles client connections and disconnections, updating the list of active users.
- Simple GUI Client: The client application uses a Java Applet with AWT components for a simple and functional user interface.
This project serves as a practical implementation of several core computer science concepts:
- Client-Server Architecture: Utilizes the classic model where a central server listens for and manages connections from multiple clients.
- Multithreading and Concurrency: The server creates a new thread (
ChatServerThread) for each client. This allows it to handle I/O operations for many users in parallel without blocking. - Synchronization and Thread Safety: The
handle()andremove()methods in theChatServerare declared assynchronized. This is a critical design choice to prevent race conditions when multiple threads attempt to modify the sharedclientsarray simultaneously. - Java Networking (Sockets): Implements low-level networking using
java.net.Socketfor client connections andjava.net.ServerSocketfor the server's listening port. - Input/Output Streams: Uses
DataInputStreamandDataOutputStreamto handle the flow of text data between the server and clients. - GUI Development: The client leverages the Abstract Window Toolkit (AWT) within a Java Applet to provide a user interface with a text display area, an input field, and action buttons.
The application was developed incrementally through five distinct stages, building in complexity at each step:
- Stage 1: Simple, One-Time Server: A basic server that could accept only a single client connection and would terminate after the client disconnected.
- Stage 2: Persistent Server: The server was improved to remain active and accept new connections after a client quit.
- Stage 3: Multi-Client Server: The server was re-architected to handle multiple simultaneous client connections, displaying all their messages on the server's console.
- Stage 4: Functional Broadcast Server: The server logic was enhanced to broadcast messages received from any client to all other connected clients, creating a true chat room.
- Stage 5: GUI Client: The command-line client was wrapped in a simple AWT Applet to provide a graphical user interface, completing the application.
- Java Development Kit (JDK) 8 or higher (to ensure
appletvieweris available). - A terminal or command prompt.
Navigate to the project's source directory and compile all the Java files using the Java compiler.
javac *.javaStart the server from the command line, specifying a port number to listen on. For this example, we'll use port 4444.
java ChatServer 4444You should see output indicating the server has started and is waiting for clients.
Binding to port 4444, please wait ...
Server started: ServerSocket[addr=0.0.0.0/0.0.0.0,localport=4444]
Waiting for a client ...```
### 3. Run the Chat Client
The client is a Java Applet and is best run using the `appletviewer` tool included with the JDK.
**First, create a simple HTML file** (e.g., `run.html`) to host the applet and pass it the server parameters.
#### `run.html`
```html
<!DOCTYPE html>
<html>
<head>
<title>Chat Client</title>
</head>
<body>
<h1>Chat Client Applet</h1>
<applet code="ChatClient.class" width="400" height="300">
<param name="host" value="localhost">
<param name="port" value="4444">
</applet>
</body>
</html>
Second, launch the applet using appletviewer from your terminal.
appletviewer run.htmlYou can run this command in multiple separate terminals to launch several clients and test the multi-user functionality.
ChatServer.java: The main server application. It listens for incoming connections and manages the pool of client threads.ChatServerThread.java: A dedicated thread created by the server for each connected client. It handles reading input from and writing output to a single client.ChatClient.java: The main client application, built as a Java Applet. It contains the GUI and the primary logic for connecting to the server and handling user input.ChatClientThread.java: A helper thread for the client. Its sole responsibility is to continuously listen for incoming messages from the server and pass them to theChatClientfor display.