Skip to content

SashaKYotoz/Multiclient-Chat-JavaFX

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multiclient Chat Application Architecture

A real-time, multi-client desktop chat application built with a decoupled architecture. It utilizes multithreading to manage simultaneous user connections, real-time message broadcasting, and non-blocking desktop client UI interactions.

sample of application work

Technologies Used

  • Language: Java 21
  • UI Framework: JavaFX (FXML layout configuration)
  • Networking: Java Socket API (TCP/IP)
  • Concurrency: Java Threads (java.lang.Thread)

Project Structure

net.sashakyotoz.multiclientchat/
│
├── server/
│   ├── Main.java              # Entry point for the server application; initializes network processes.
│   ├── Server.java            # Manages ServerSocket, connection loops, and client broadcasting.
│   ├── User.java              # Client-handler thread managing an individual socket connection and its input stream.
│   └── Message.java           # Data model representing username metadata and message payloads.
│
├── HelloApplication.java      # Application entry point; initializes the JavaFX window stage and loads FXML.
└── HelloController.java       # Handles UI logic and encapsulates the background Client network threads.
  1. Server Initialization
    • The entry point Main.java starts the Server instance on port 5555
    • The server initializes its ServerSocket and spawns two asynchronous worker threads: one for accepting incoming connections (waitForConnection) and one for broadcasting messages (sendMessage)
  2. Client Connection
    • The user runs the JavaFX application through HelloApplication.java
    • When the user fills in the connection details and clicks the "Connect" button, HelloController.java instantiates a Client object
    • This actions opens a network Socket to the specified host and port. Immediately upon connection, the client transmits a join handshake payload formatted as "[username] -> joined the chat"
  3. Handshake & User Registration
    • The server's background connection thread intercepts the socket via serverSocket.accept()
    • The socket is wrapped in a unique User thread object and appended to the active clients collection
    • The server reads the initial incoming stream. When it detects the "-> joined the chat" substring, it extracts the custom username and dynamically updates the default placeholder name assigned to that User thread
  4. The Message Propagation Loop
    • When a client types a message and hits "Send", the UI switches the sendMessageFlag to true. The client's background network writer thread extracts the string from the input field, pushes it out to the socket stream, and clears the input field
    • The server-side User thread blocks on reader.readLine(). Once it receives the message string, it creates a new Message object, appends it to the global Server.messages list, and flags Server.update = true
    • The asynchronous Server.sendMessage() thread catches the update flag, iterates through the entire list of active connected clients, and writes the timestamped message to everyone's output stream before resetting the flag
    • The client's dedicated network reader thread picks up the server's broadcast. Because modifying UI elements from a background thread causes runtime errors, it invokes Platform.runLater() to safely append the new message string to the JavaFX ListView
  5. Connection Termination
    • If a client types and sends "exit!", the server-side User run loop catches the instruction and breaks out of its listening cycle
    • The server logs the disconnection to the console, transmits a structural "kick" signal back to the client to safely break its reading loop, and closes the data streams and socket resources
    • If a client application closes abruptly, the server catches the resulting network drop inside an IOException block, logging a "Connection is lost" warning without crashing the backend application

About

Muliticlient techonology sample using JavaFX

Resources

Stars

0 stars

Watchers

1 watching

Forks

Contributors

Languages