Skip to content

richtxteditor/Client-Server-Chat-System-with-Enhanced-TCP-Protocol

Repository files navigation

Java Client/Server Chat Application

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.

Features

  • 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.

Architectural Concepts Demonstrated

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() and remove() methods in the ChatServer are declared as synchronized. This is a critical design choice to prevent race conditions when multiple threads attempt to modify the shared clients array simultaneously.
  • Java Networking (Sockets): Implements low-level networking using java.net.Socket for client connections and java.net.ServerSocket for the server's listening port.
  • Input/Output Streams: Uses DataInputStream and DataOutputStream to 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.

Project Evolution

The application was developed incrementally through five distinct stages, building in complexity at each step:

  1. Stage 1: Simple, One-Time Server: A basic server that could accept only a single client connection and would terminate after the client disconnected.
  2. Stage 2: Persistent Server: The server was improved to remain active and accept new connections after a client quit.
  3. 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.
  4. 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.
  5. Stage 5: GUI Client: The command-line client was wrapped in a simple AWT Applet to provide a graphical user interface, completing the application.

How to Compile and Run

Requirements

  • Java Development Kit (JDK) 8 or higher (to ensure appletviewer is available).
  • A terminal or command prompt.

1. Compile the Source Code

Navigate to the project's source directory and compile all the Java files using the Java compiler.

javac *.java

2. Run the Chat Server

Start the server from the command line, specifying a port number to listen on. For this example, we'll use port 4444.

java ChatServer 4444

You 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.html

You can run this command in multiple separate terminals to launch several clients and test the multi-user functionality.

Source Code Overview

  • 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 the ChatClient for display.

About

Networking class project chat app operating over the internet via the OSI model

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages