Skip to content
/ TCPChat Public

Terminal-based TCP chat with Client-Server Architecture and extensive error handling. Works on single machine or with multiple machines on same home network. ๐ŸŒ๏ฝก โ‚ŠยฐโŠน เฃช ห–๐Ÿ’ฟเน‹เฃญ โญ‘ เซฎ หถแต” แต• แต”หถ แƒ

License

Notifications You must be signed in to change notification settings

Jenspi/TCPChat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

14 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

TCPChat: Multi-Machine Chatroom

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.


๐Ÿ“‹ Table of Contents

โœจ Features

๐ŸŽฏ 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)

(back to table of contents)

๐Ÿ—๏ธ Architecture: Client-Server Model

 CLIENT 1                    SERVER                    CLIENT 2
    |                          |                          |
    |-------- connect -------->|<------- connect ---------|
    |                          |                          |
    |------ send message ----->|                          |
    |                          |------ broadcast -------->|
    |<----- broadcast ---------|<----- send message ------|
    |                          |                          |

(back to table of contents)

๐Ÿ—๏ธ File Architecture

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

(back to table of contents)

๐Ÿš€ Local development setup & usage

ใ€ 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

๐Ÿ“ฆ Requirements

  • Java JDK 8 or higher
  • Network connectivity (same home network and/or accessible IP addresses)
  • Available ports (default: 5000)

๐ŸŽฎ Commands

Command Description
bye Disconnect from the chat server gracefully
allusers Display list of all currently connected users
help Show available commands

(back to table of contents)

๐ŸŽฒ Chat Session Examples

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).

(back to table of contents)

๐Ÿ“„ License

CC BY-NC-ND 4.0
Updated December 7, 2025.

This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 4.0 International License.

๐Ÿ‘ค Author

Jenny Spicer / Jennifer Spicer

Portfolio LinkedIn GitHub

(back to table of contents)

๐Ÿ”ง Technical Details

๐Ÿงต 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
โš ๏ธ Thread Safety Note: Should upgrade to 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.

(back to table of contents)


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.

About

Terminal-based TCP chat with Client-Server Architecture and extensive error handling. Works on single machine or with multiple machines on same home network. ๐ŸŒ๏ฝก โ‚ŠยฐโŠน เฃช ห–๐Ÿ’ฟเน‹เฃญ โญ‘ เซฎ หถแต” แต• แต”หถ แƒ

Topics

Resources

License

Stars

Watchers

Forks

Languages