Skip to content

frank06n/tcp-app-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TCP Chat Application

A multi-client TCP chat server and client application written in Java. This project demonstrates a simple chat system where multiple clients can connect to a server, send messages, and receive broadcasts from other clients.

Features

  • Multi-client support: Server can handle multiple concurrent client connections
  • Message broadcasting: Messages from one client are broadcasted to all connected clients
  • Virtual threads: Uses Java 21 virtual threads for efficient client handling
  • Graceful shutdown: Server stops accepting new clients after 30 seconds of inactivity
  • Simple protocol: Custom message protocol with support for names, messages, and quit commands

Requirements

  • Java 21 or higher
  • Gradle 9.1.0 or higher (included via wrapper)

Building the Project

To build the project, run:

./gradlew build

On Windows:

gradlew.bat build

Running the Application

The application can run as either a server or a client. When you run it, you'll be prompted to choose:

Running as Server

  1. Start the application:

    ./gradlew run
  2. Enter 0 when prompted to start the server

  3. The server will start on port 5000 and wait for client connections

  4. Server will stop accepting new clients after 30 seconds of inactivity

Running as Client

  1. Start the application:

    ./gradlew run
  2. Enter 1 when prompted to start the client

  3. Enter your name when prompted

  4. Once connected, you can:

    • Type messages to send to all connected clients
    • Type /quit to disconnect from the server

Running Multiple Clients

To test multiple clients, open multiple terminal windows and run the client mode (1) in each window. All clients will see messages from each other.

Architecture

Server Components

  • Server: Main server class that accepts client connections
  • Dispatcher: Handles message routing and broadcasting to all connected clients
  • ClientHandler: Manages individual client connections and message processing

Client Components

  • Client: Handles connection to server, sending messages, and receiving broadcasts

Message Protocol

The application uses a simple text-based protocol:

  • n| <name> - Set client name
  • m| <message> - Send a message to all clients
  • q| - Quit/Disconnect from server
  • c| - Command (reserved for future use)

Project Structure

tcp-app-java/
├── app/
│   ├── src/
│   │   ├── main/
│   │   │   └── java/
│   │   │       └── org/
│   │   │           └── example/
│   │   │               ├── App.java          # Main entry point
│   │   │               ├── Server.java       # Server implementation
│   │   │               ├── Client.java       # Client implementation
│   │   │               └── SendType.java     # Message type constants
│   │   └── test/
│   └── build.gradle.kts
├── gradle/
├── build.gradle.kts
└── README.md

Development

Building the JAR

To create an executable JAR:

./gradlew jar

The JAR will be created at app/build/libs/app.jar

Running Tests

./gradlew test

Notes

  • The server uses Java 21 virtual threads for efficient handling of multiple clients
  • Server automatically shuts down after 30 seconds without new client connections (if no clients are connected)
  • Messages are formatted as [ClientName] message content when broadcasted
  • The client polls for incoming messages while waiting for user input

License

This project is provided as-is for educational purposes.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages