Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Concurrent Multi-Client Chat Server

A TCP-based multi-client chat server built with Core Java — demonstrating Multithreading, Java Sockets, ExecutorService, ConcurrentHashMap, and the Producer-Consumer pattern using BlockingQueue.

Features

Feature Implementation
50+ concurrent clients Fixed-size ExecutorService (thread pool)
Thread-safe session store ConcurrentHashMap<clientId, ClientHandler>
Zero-loss messaging BlockingQueue (Producer-Consumer pattern)
Broadcast messaging Send to all connected clients
Private messaging Direct message to a specific user
Graceful shutdown ShutdownHook + awaitTermination
Custom exceptions ChatServerException hierarchy
Resource management try-with-resources on all I/O streams

Java Concepts Demonstrated

  • MultithreadingExecutorService, Thread, Runnable
  • ConcurrencyConcurrentHashMap, BlockingQueue, volatile
  • Java SocketsServerSocket, Socket, TCP communication
  • Java I/OBufferedReader, PrintWriter, InputStreamReader
  • OOP — Encapsulation, Single Responsibility Principle
  • Collections FrameworkConcurrentHashMap, LinkedBlockingQueue
  • Design Patterns — Producer-Consumer, Thread-per-client

Project Structure

src/main/java/com/chatserver/
├── ChatServer.java          # Entry point; manages ServerSocket + thread pool
├── ClientHandler.java       # Runnable; one per connected client
├── MessageBroker.java       # Consumer thread; delivers from BlockingQueue
├── Message.java             # Immutable message model
├── MessageType.java         # Enum: BROADCAST | PRIVATE
├── ChatServerException.java # Custom exception hierarchy
└── ChatClient.java          # Terminal client for testing

Architecture

Client 1 ──┐
Client 2 ──┤──► ClientHandler (Thread Pool) ──► BlockingQueue ──► MessageBroker ──► Deliver
Client N ──┘                                    (Producer)           (Consumer)
                    ConcurrentHashMap
                  (active session store)

How to Run

Prerequisites

  • Java 8 or above
  • No external dependencies

Compile

javac -d out src/main/java/com/chatserver/*.java

Start Server

java -cp out com.chatserver.ChatServer

Start Client (open multiple terminals)

java -cp out com.chatserver.ChatClient

Commands

JOIN:<username>              → Set your display name
MSG:<text>                   → Broadcast to all users
PRIVATE:<username>:<text>    → Send private message
QUIT                         → Disconnect

Example Session

[SERVER] Welcome! Your ID: a3f2b1c4. Use JOIN:<name> to set your username.
JOIN:Shambhavi
[SERVER] [User-a3f2b1c4 is now known as Shambhavi]
MSG:Hello everyone!
PRIVATE:Alice:Hey, only for you!
QUIT

Author: Shambhavi Trivedi
Tech Stack: Java 8 · TCP Sockets · ExecutorService · ConcurrentHashMap · BlockingQueue

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages