A simple multi-client chat application implemented in Java using sockets.
The project consists of a server that manages multiple client connections and clients that can send chat requests, confirm requests, and exchange messages.
Playerchat-360T-Comprossed.mov
- Overview
- Architecture
- Setup
- How Handshake Works
- Message Request Flow
- Messaging Flow
- Payloads
- Limitations
The application allows multiple clients to connect to a central server.
Clients can:
- Register with the server via a handshake.
- Send a chat request to other clients.
- Exchange messages with connected clients.
- Limit messages to a configurable maximum per session.
-
Server (
Server.java):- Listens on a specific port for incoming client connections.
- Creates a
ClientInstancefor each connected client. - Maintains a map of connected clients for routing messages and requests.
-
Client (
Client.java):- Connects to the server via a TCP socket.
- Performs handshake to register instance ID.
- Sends/receives chat requests and messages.
-
ClientInstance (
ClientInstance.java):- Represents a client on the server.
- Handles incoming messages, forwards requests, and sends confirmations.
-
MessageParser (
MessageParser.java):- Utility to serialize and parse message payloads between clients and server.
- Start the Server:
java -cp target/classescom.hashedalgorithm.playerchat.server.App
Server listens on port 12345.
- Start Clients:
java -cp target/classes com.hashedalgorithm.playerchat.client.App
- Enter your unique chat name when prompted.
Handshake ensures each client has a unique ID and is registered with the server.
- Client connects to server.
- Client sends a handshake request:
req:handshake|from:<instanceId>
- Server verifies that the instanceId is unique.
- Server responds:
- Success or Failed (duplicate or invalid)
req:handshake|id:<instanceId>|stat:success/failed
- Client proceeds only if handshake succeeds.
Message requests allow a client to initiate a chat with another client.
- Client A sends a message request to Client B:
from:<clientA>|to:<clientB>|req:msg
- Server forwards the request to Client B:
- If Client B exists, Client B can accept:
req:msg|from:<clientB>|to:<clientA>|stat:success
- If Client B does not exist, Client A is notified:
req:msg|from:<clientA>|to:<clientB>|stat:failed
- Upon receiving SUCCESS, Client A marks the connection as active.
Once a chat request is accepted, clients can exchange messages.
- Client sends a message:
from:<clientA>|to:<clientB>|msg:<message>
- Server receives the message and forwards it to the recipient.
- Recipient processes the message:
from:<clientA>|msg:{<counter>} - <message>
-
- counter tracks message sequence number per session.
- Each client limits the number of messages per session (configurable via MAX_MESSAGES).
| Field | Description | Example |
|---|---|---|
| req | Type of request | handshake, msg |
| from | Sender’s instance ID | hashed |
| to | Recipient’s instance ID | dee |
| msg | Actual chat message | Hello there! |
| stat | Status of a request/response | success, failed |
| id | Instance ID (used in handshake) | hashed |
- Maximum clients supported: 10
- Maximum messages per session: Configured using MAX_MESSAGES in Client and ClientInstance.
- No encryption; plain text communication.
- Simple sequential processing; does not scale for large numbers of clients.
Client 1:
Enter chat name: hashed
Connected to server.
Handshake completed.
Send chat request to: dee
Connected with dee!
[hashed]: {1} - Hello
Client 2:
Enter chat name: dee
Connected to server.
Handshake completed.
Received message request from hashed.
[hashed]: {1} - Hello
Server:
Handshake with client - hashed completed.
Handshake with client - dee completed.
Message request from hashed to dee
Message request confirmation from dee to hashed is success
- All communication follows a structured payload format for easier parsing.
- Clients handle reconnections, timeouts, and failed requests gracefully.
- The project can be extended to include message persistence, encryption, or GUI integration.
- ChatGPT - Contents of this Readme are written using AI.