0% found this document useful (0 votes)
11 views7 pages

Programming Assignment Unit 7

The document describes a Java-based Online Chat Application that utilizes socket programming to create a multi-user communication system with a centralized server managing client connections. It highlights key components such as the ChatServer, ClientHandler, and ChatClient, demonstrating concepts of multi-threading, real-time communication, and error handling. The application serves as a scalable foundation for peer-to-peer messaging, featuring unique user identification and efficient message broadcasting.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Programming Assignment Unit 7

The document describes a Java-based Online Chat Application that utilizes socket programming to create a multi-user communication system with a centralized server managing client connections. It highlights key components such as the ChatServer, ClientHandler, and ChatClient, demonstrating concepts of multi-threading, real-time communication, and error handling. The application serves as a scalable foundation for peer-to-peer messaging, featuring unique user identification and efficient message broadcasting.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Title: A Multi-Threaded Java Socket Application for Real-Time Client-Server Chat

Communication

week 7

Benjamin Adusei

Institution: University of the People

CS 1103 -01 Programming 2

Instructor:

Dr. Amer Ahmad Al-Rahayfeh (Instructor)

Date of Submission:

December 31st, 2025


1. Introduction
This Java-based Online Chat Application implements a robust multi-user communication system
using socket programming. The application features a centralized server that manages multiple
client connections, assigns unique user IDs, and facilitates real-time message broadcasting. The
system demonstrates core networking concepts, multi-threading, and concurrent client
management through a clean text-based interface, providing a scalable foundation for peer-to-
peer communication while maintaining type safety and error resilience.
Note: Please not that I used port 12347 instead of the common 12345 because I had an error
message the Port 12345 is used by other applications.
2. Program Code
i. Program Structure in Eclipse

Screenshot 1: Eclipse Package Explorer showing chat application project structure


ii. ChatServer Implementation
Screenshot 2: ChatServer class using socket programming to manage multiple client connections
iii. ClientHandler Implementation
Screenshot 3: ClientHandler class managing individual client connections
iv. ChatClient Implementation
Screenshot 4: ChatClient class with socket connection and messaging capabilitie

v. Program Execution Output


Screenshot 5: Complete Program Execution Showing Server and Client (Ben) Interactions
3. Code Explanation
Program Structure and Design:
The Online Chat Application implements a client-server architecture using Java Socket
Programming. The system follows a multi-threaded design pattern to handle concurrent client
connections efficiently while maintaining real-time communication capabilities.
Key Components:
a) ChatServer Architecture:
 ServerSocket Implementation: Listens on port 12347 for incoming client connections
 Multi-threaded Client Management: Each client connection handled by separate
ClientHandler thread
 Synchronized Client List: Thread-safe management of connected clients
using [Link]()
 Message Broadcasting: Efficient message distribution to all connected clients using
synchronized iteration
 Connection Tracking: Real-time monitoring of active connections and user status with
timestamped logging
b) ClientHandler Implementation:
 Individual Connection Management: Dedicated thread per client for concurrent
processing using Runnable interface
 Stream-based Communication: BufferedReader and PrintWriter for input/output streams
with auto-flush
 User Identification: Unique client ID assignment and name customization with default
"Guest" prefix
 Command Processing: Built-in commands (/help, /list, /exit) with formatted response
display
 Resource Management: Proper cleanup of streams and sockets on disconnection with
notification broadcasting
c) ChatClient Architecture:
 Socket Connection: Establishes TCP connection to server at localhost:12347
 Dual-thread Design: Main thread for sending messages, separate thread for receiving
messages via MessageReceiver inner class
 User Interface: Text-based console interface with clear prompts and formatted output
 Command Support: Integration with server commands through standard input/output
streams
 Error Handling: Graceful handling of connection failures, timeouts, and disconnections
d) Communication Protocol:
 TCP Socket Communication: Reliable, connection-oriented data transfer using
Java Socket class
 Line-based Messaging: Simple text protocol using readLine() and println() methods
 Message Formatting: Clear distinction between user messages and system notifications
with prefix identification
 Command Structure: Prefixed commands (/) for system operations with server-side
validation
Programming Concepts Demonstrated:
1. Socket Programming:
 ServerSocket: Creation and management of server-side socket with port binding and
connection acceptance
 Socket: Client-side socket establishment and communication with address/port
configuration
 Network I/O Streams: BufferedReader and PrintWriter for efficient network
communication with buffering
 Port Management: Fixed port assignment (12347) with listening capabilities and conflict
handling
2. Multi-threading & Concurrency:
 Thread-per-Client Model: Each client handled in separate thread for parallel processing
 Synchronized Collections: Thread-safe management of shared client list
using synchronizedSet
 Concurrent Message Processing: Simultaneous message handling for multiple clients
without blocking
 Thread Safety: Proper synchronization for shared resources to prevent race conditions
3. Real-time Communication:
 Immediate Message Broadcasting: Near-instant message delivery to all connected clients
 Connection State Management: Dynamic tracking of user presence with join/leave
notifications
 User Notification System: Automatic system announcements for user state changes
 Live User List: Real-time updates of connected users with name and ID tracking
4. Error Handling & Resilience:
 Connection Recovery: Graceful handling of network interruptions with informative
messages
 Resource Cleanup: Proper closure of sockets and streams in finally blocks
 Input Validation: Sanitization of user input and commands with null and empty checks
 Exception Management: Comprehensive try-catch blocks for robust operation with
specific exception types
5. User Interface Design:
 Console-based Interface: Text-only interface with clear section separators and formatting
 Command Prompt: Clear user input indication with username context
 Message Formatting: Distinct formatting for system vs user messages with visual
separation
 Help System: Built-in command documentation with formatted output display
5. Conclusion
The Online Chat Application successfully implements a robust, multi-user communication
system using Java Socket Programming. The application demonstrates mastery of core
networking concepts including server-client architecture, multi-threading, real-time
communication, and concurrent connection management. The system provides a scalable
foundation for peer-to-peer messaging with features including unique user identification,
message broadcasting, command processing, and comprehensive error handling.

References
Oracle Corporation. (2023). Java Platform, Standard Edition Documentation. Retrieved
from [Link]
Oracle Corporation. (2023). Java Socket Programming Tutorial. Retrieved
from [Link]
Horstmann, C. S., & Cornell, G. (2013). Core Java Volume II: Advanced Features (9th ed.).
Prentice Hall.
Bloch, J. (2018). Effective Java (3rd ed.). Addison-Wesley Professional.
Sierra, K., & Bates, B. (2005). Head First Java (2nd ed.). O'Reilly Media.
TCP/IP Guide. (2023). Transmission Control Protocol/Internet Protocol. Retrieved
from [Link]
Eclipse Foundation. (2023). Eclipse IDE User Guide. Retrieved
from [Link]
Java Documentation. (2023). Java Multithreading and Concurrency. Retrieved
from [Link]
Stevens, W. R. (1994). *TCP/IP Illustrated, Volume 1: The Protocols*. Addison-Wesley
Professional.
Gamma, E., Helm, R., Johnson, R., & Vlissides, J. (1994). Design Patterns: Elements of
Reusable Object-Oriented Software. Addison-Wesley.

You might also like