A multithreaded console chat room implemented in Java using Socket programming. This project is submitted as part of CSC-1004: Computational Laboratory Using Java, The Chinese University of Hong Kong, Shenzhen (CUHK-SZ).
Course context: the assignment requires a Java chat room with multithreading, chat-room functions, chat-record storage, and proper message display. 3
- TCP client–server architecture on
localhost:6666 - Automatic 5-digit client ID assignment (e.g.
01234) - Custom username input during handshake
- Real-time broadcasting to all connected clients
- Separate sender/receiver threads on the client side
- Server-side multithreading: one
ClientHandlerper connection - Online user list sent to newly joined clients
- Join/leave system notifications for all users
- Timestamped messages (
HH:mm:ss) - Server-side chat history persisted to
chat_history.txt - Recent history delivered to new clients (last 50 messages)
- Client
/savecommand exports chat history to desktop - Commands:
/help,/exit,/save(client);/users,/history(server-side)
- Java SE (Sockets, DataInputStream/DataOutputStream, Threads)
Vectorfor thread-safe active client storageAtomicIntegerfor unique ID generation- File I/O for history persistence
src/main/java/org/example/ ├── Main.java # Launcher: choose 1=Server, 2=Client ├── Server.java # ServerSocket, broadcast, history, ClientHandler ├── Client.java # Console client, sender/receiver threads, /save pom.xml # Maven project file chat_history.txt # Auto-created server-side history file
- JDK 11+ (JDK 17/21 recommended)
- Maven (optional) or any Java IDE
mvn exec:java -Dexec.mainClass=org.example.Main
mvn exec:java -Dexec.mainClass=org.example.Main
javac src/main/java/org/example/*.java -d out
java -cp out org.example.Main
java -cp out org.example.Main
- Start the server first.
- Start one or more clients.
- Enter a username when prompted.
- Chat normally; messages are broadcast with timestamps.
- Client commands:
/savesave chat history to desktop asmy_chat_history.txt/helpshow command list/exitleave the chat room
- Server-side client commands:
/userslist online users/historyshow recent server history
- Multithreading: server spawns one thread per client; client uses one receiver thread + main sender thread.
- Socket I/O:
ServerSocket/SocketwithDataInputStream/DataOutputStream. - Storage: in-memory history plus append-only
chat_history.txt. - Suitable for local multi-client demonstration and video recording.
Repository: https://github.com/berserkross/Java_Chat_Room