Skip to content

Dinethzz/enhanced-chat

Repository files navigation

Simple Multi-Threaded Chat Server (Pure Java)

//LAST UPDATE

cd "/Users/harshanaamuwatte/Projects/Academic/Enhanced chat" export CHAT_ADMIN_TOKEN="admin123" ./run-server.sh 7200

cd "/Users/harshanaamuwatte/Projects/Academic/Enhanced chat" ./run-ws-bridge.sh --chat-port 7200 --ws-port 8081

cd "/Users/harshanaamuwatte/Projects/Academic/Enhanced chat/web" python3 -m http.server 8000

if python3 is missing, try: python -m http.server 8000


1) Chat server on 7210

./run-server.sh 7210

2) WebSocket bridge to that port (ws://localhost:8081)

./run-ws-bridge.sh --ws-port 8081 --chat-host 127.0.0.1 --chat-port 7210

3) Serve the web UI

python3 -m http.server 5500 -d web

Overview

A lightweight multi-threaded TCP chat server implemented in pure Java (no external libraries). Each connecting client is handled by a dedicated thread (ClientHandler). Messages from one client are broadcast to all other connected clients.

Run backend (server):

./run-server.sh 7210

Run web frontend:

./run-ws-bridge.sh --ws-port 8081 --chat-host 127.0.0.1 --chat-port 7210
python3 -m http.server 5500 -d web

Open http://localhost:5500 and set WS URL to ws://localhost:8081. Use the file picker to upload files to Everyone or a selected user.

Features

  • Concurrent client handling using threads
  • Line-based text messaging
  • Join/leave notifications
  • Graceful client disconnect with /quit
  • Private messaging with /private <user> <message>
  • File transfer with /sendfile (<user>) <path>

Project Structure

src/chatserver/Server.java
src/chatserver/ClientHandler.java
src/chatserver/FileReceiver.java
FileSender.java
ChatServer.java
ChatClient.java

Build

From project root (produces classes runnable directly):

javac -d . src/chatserver/*.java ChatServer.java ChatClient.java

Run (Simplified Commands)

Start server on default port 5000 (after compiling with -d .):

java ChatServer

Specify port:

java ChatServer 6000

Or use the helper script (builds then runs):

./run-server.sh            # uses 5000
./run-server.sh 7205       # choose custom port

The server also opens a file listener on port+1. Run a client (host, port optional):

java ChatClient

Connect to custom host/port:

java ChatClient 127.0.0.1 6000

GUI Client

Launch the Swing GUI (LoginUI → Chat window):

java GuiChatClient
# or
./run-gui.sh

In the login dialog, set host to your server (e.g., 127.0.0.1) and the chat port (e.g., 7205).

Important: connect clients to the chat port (e.g., 7205). The file transfer listener uses port+1 (e.g., 7206) and is not for chat connections.

Web (HTML/CSS/JS) Client

The browser can’t open raw TCP sockets, so use the WebSocket bridge to connect the web UI to the Java chat server.

  1. Start the chat server (example 7210):
./run-server.sh 7210
  1. In a new terminal, start the WebSocket bridge (example bridge on 8081):
./run-ws-bridge.sh --ws-port 8081 --chat-host 127.0.0.1 --chat-port 7210
  1. Open web/index.html in your browser (no server required). Set WS URL to:
ws://localhost:8081

Click Connect, then chat. Use the Private form or /private <user> <msg>.

Notes:

  • The bridge is a minimal RFC6455 implementation (text frames only).
  • File transfer from the browser is not implemented; use console client for /sendfile.

Send a file

From inside a running client session:

/sendfile sample.txt

Sends sample.txt to all users (broadcast receipt notice).

Target a specific user:

/sendfile User2 sample.txt

Files are uploaded to the server uploads/ directory. Only the target user receives a receipt message (or all if *).

Alternative Connection (Netcat / Telnet)

nc 127.0.0.1 5000
# or
telnet 127.0.0.1 5000

Type messages and press Enter to send. Use /quit to disconnect.

Protocol Notes

  • Server sends a welcome line on connect.
  • Plain text lines are broadcast prefixed with the user's auto-assigned name (UserN).
  • /quit triggers a clean disconnect.
  • /private <user> <message> sends a direct message only to the target user.
  • /sendfile (<user>) <path> opens a secondary connection on port+1 and sends header: FILE <filename> <size> <target> followed by raw bytes.
    • <target> is * for broadcast notice or a specific username.
    • Server saves file under uploads/<filename>; no automatic download channel yet.

Extensibility Ideas

  • Add username negotiation.
  • Implement private messaging (e.g., /msg User2 hi).
  • Add server-side command to list users.
  • Persist chat history.

License

(No license specified; internal academic use.)

About

Real-time chat app with WebSocket bridge, Java TCP server, file sharing & admin dashboard

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors