Terminal-based TCP chat application with Client-Server Architecture and extensive error handling. Real-time communication between multiple clients works on single machine or with multiple machines on same home network.
๐ฏ Real-time messaging between numerous connected clients in the terminal
๐ฏ Broadcast messaging to all connected clients
๐ฏ Connection notifications when users join or leave
๐ฏ Client commands
๐ฏ Extensive error handling so it's nearly foolproof
๐ฏ Cross-platform (Windows, macOS, and Linux)
CLIENT 1 SERVER CLIENT 2
| | |
|-------- connect -------->|<------- connect ---------|
| | |
|------ send message ----->| |
| |------ broadcast -------->|
|<----- broadcast ---------|<----- send message ------|
| | |
TCPChat/
โโโ tcpchat/ # Java package containing source code
โ โโโ Client.java # Client-side implementation
โ โโโ Server.java # Server-side implementation
โโโ docs/ # Auto-generated Javadoc documentation
โ โโโ (Javadoc generated files)
โโโ images/ # README assets (screenshots, diagrams)
โโโ LICENSE.txt # CC BY-NC-ND 4.0 license file
โโโ CITATION.cff # Citation metadata with ORCID reference
โโโ README.md
ใ Clone or download the project ใ
$ git clone https://github.com/Jenspi/TCPChat
$ cd tcpchat
ใ Compilation ใ
$ javac tcpchat/Server.java
$ javac tcpchat/Client.java
ใ Server ใ
# 3a. Start the Server on a custom port:
$ java tcpchat.Server 3000 # 3000 = desired port
# 3b. Start the Server on the default port (5000):
$ java tcpchat.Server
# Note: This port number may already be taken by other processes on the machine if the user already has something running with *Vite*, *Expo*, etc.
ใ Client ใ
# 4a. Connecting Clients on the same machine (localhost):
$ java tcpchat.Client localhost 3000 # 3000 = desired port
# 4b. Connecting Clients on different machines (use Server's IP address; see below):
$ java tcpchat.Client 192.168.1.100 3000 # 3000 = desired port
# 4c. Connecting Clients using defaults (localhost, port 5000):
$ java tcpchat.Client
ใ Finding your IP address for connecting to a custom server address ใ
# Finding Your SERVER IP Address onmacOS/Linux:
$ ifconfig | grep "inet " | grep -v 127.0.0.1
# Finding Your SERVER IP Address on Windows:
$ ipconfig
# Look for "IPv4 Address" under your active network adapter.
ใ Javadoc Generation ใ
$ javadoc -d docs -author -version Client.java Server.java- Java JDK 8 or higher
- Network connectivity (same home network and/or accessible IP addresses)
- Available ports (default: 5000)
| Command | Description |
|---|---|
bye |
Disconnect from the chat server gracefully |
allusers |
Display list of all currently connected users |
help |
Show available commands |
TCPChat 1.0 Example A: Server with 2 Clients connected, both on same machine.
TCPChat 1.0 Example B: Server with 2 Clients connected, one on different machine (hard to read, sorry).
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 4.0 International License.
Jenny Spicer / Jennifer Spicer
๐งต Concurrency & Multithreading
โ
Multithreaded Server: Thread-per-client model - dedicated ServerHandler thread spawned for each connection
โ
Concurrent Message Handling: Multiple clients send/receive messages simultaneously without blocking
โ
Asynchronous I/O: Separate threads for reading and writing enable full-duplex communication
โ
Thread Lifecycle Management: Threads created on connection, gracefully terminated on disconnect
โ
Thread-Safe Collections: Uses HashMap for client storage (consider ConcurrentHashMap for production)
๐ Networking
โ
TCP/IP Socket Programming: Connection-oriented protocol with automatic 3-way handshake (handled by Java Socket API)
โ
Client-Server Architecture: Centralized server coordinates all client communication
โ
Full-Duplex Communication: Simultaneous bidirectional data flow between server and clients
โ
Persistent Connections: Sockets remain open until client sends "bye" command
๐๏ธ Data Structures
โ
HashMap Storage: HashMap<String, Socket> maps username โ client socket for O(1) lookup
โ
Output Stream Cache: HashMap<String, DataOutputStream> maps username โ output stream for efficient broadcasting and prevents the need to recreate threads
ConcurrentHashMap instead of HashMap for thread-safe client storage
๐ฃ I/O Streams
๐ฃ๏ธ Client โ Server: DataOutputStream โ DataInputStream
๐ข Server โ Client: DataOutputStream (broadcast to all)
๐ฃ๏ธ User Input: BufferedReader reading from System.in
๐ฌ Message Format
YYYY/MM/DD HH:MM:SS [Username]: Message content
๐ฌ Example:
2025/12/08 15:30:45 [Batman27]: Hello.
Caution
This is a learning project demonstrating TCP socket programming, multithreading, and client-server architecture in Java. It is not production-ready and lacks security features required for real-world applications.



