Skip to content

alienXXVI/chat-TCP-UDP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

Chat TCP/UDP

Description

This project consists of a distributed chat system implemented in Java. It provides two separate versions of the application: one utilizing TCP for reliable, connection-oriented communication, and another utilizing UDP for fast, packet-based messaging. The system allows multiple users to join a common room or exchange private messages.

Technologies

  • Java SE
  • TCP/IP (ServerSocket & Socket API)
  • UDP (DatagramSocket & DatagramPacket API)
  • Multi-threading

Features

  • Dual Protocol Support: Independent implementations for both TCP and UDP communication styles.
  • Multi-Client Handling: Servers can manage multiple simultaneous connections using threads.
  • Messaging Modes: Supports global broadcast messages to all users and targeted private messages using the @username syntax.
  • User Management: Includes commands to list active users (!list) and safely exit the chat (!exit).

How to Run

1. Compile the Source Code

Open a terminal in the project root and compile the Java files:

javac src/*.java

2. Run the TCP Version

  • Start the Server: java -cp src ChatServerTCP
  • Start the Client: java -cp src ChatClientTCP

3. Run the UDP Version

  • Start the Server: java -cp src ChatServerUDP
  • Start the Client: java -cp src ChatClientUDP

Project Structure

  • ChatServerTCP.java: Uses ServerSocket to listen for persistent connections and spawns ClientHandler threads for each user.
  • ChatClientTCP.java: Establishes a permanent stream with the server to send and receive text data.
  • ChatServerUDP.java: Acts as a listener for independent datagram packets, maintaining a manual map of client addresses and ports.
  • ChatClientUDP.java: Sends "fire-and-forget" packets to the server and handles incoming messages asynchronously.
  • .gitignore: Excludes compiled .class files and IDE metadata from the repository.
  • README.md: Project documentation.

What I Learned

  • Socket Programming: Gained hands-on experience with Java's networking APIs for both stream-based and packet-based communication.
  • Thread Synchronization: Learned to use synchronized blocks to safely manage shared resources, such as the list of active clients, across multiple threads.
  • Protocol Differences: Understood why UDP requires manual tracking of client endpoints (IP/Port) since it lacks the persistent connection state inherent to TCP.
  • Asynchronous I/O: Implemented background threads in clients to ensure the user interface remains responsive while waiting for incoming network messages.

Future Improvements

  • GUI Development: Create a JavaFX or Swing interface to replace the command-line interaction.
  • Message Persistence: Implement a database to log chat history for users to view previous messages.
  • Encryption: Integrate SSL/TLS for TCP or manual RSA/AES encryption for UDP packets to secure communication.

About

A Java-based chat application demonstrating networked communication via TCP and UDP protocols. Features multi-client support, private messaging, and user registration, highlighting the core differences between connection-oriented and stateless data transfer.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages