A desktop-based multi-user chat application built in Java using UDP datagrams, Java Swing, and multithreading.
The project follows a client-server model where multiple desktop clients connect to a central UDP server and exchange messages in real time.
The application includes:
- a Java Swing client for entering a username, server IP, and port
- a central UDP server for managing connected users and broadcasting messages
- online-user tracking
- client connection monitoring and timeout detection
- server-side commands for managing active users
This project was developed to explore core networking concepts, including UDP communication, datagram-based messaging, concurrent send/receive operations, and client-server coordination.
- Multi-user chat over UDP
- Central server for client registration and message broadcasting
- Java Swing desktop client
- Login screen for username, server IP address, and port
- Real-time message sending and receiving
- Online-user list maintained by the server
- Automatic client identification using unique IDs
- Connection-health monitoring using periodic server checks
- Automatic removal of unresponsive clients after repeated missed responses
- Graceful client disconnect handling
- Server-side administrative commands, including:
/clientsto list connected users/kickto remove a user/rawto toggle raw server output/helpto display available commands/quitto stop the server
- Server-originated broadcast messages to all connected clients
The application uses a centralized UDP-based client-server model.
┌───────────────────┐
│ Server │
│ │
│ Client Registry │
│ Message Broadcast │
│ Health Monitoring │
└─────────┬─────────┘
│
UDP Datagrams
│
┌──────────────┼──────────────┐
│ │ │
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│ Client A │ │ Client B │ │ Client C │
│ Swing │ │ Swing │ │ Swing │
└──────────┘ └──────────┘ └──────────┘
The server:
- listens for incoming UDP datagrams
- registers connected clients and assigns unique identifiers
- maintains the list of active users
- broadcasts chat messages to connected clients
- monitors client responsiveness
- removes disconnected or unresponsive clients
- provides administrative commands for server-side user management
The client:
- connects to the server using an IP address and port
- sends registration and chat messages through UDP datagrams
- receives broadcast messages from the server
- displays chat messages through a Java Swing interface
- displays the list of connected users
- handles disconnect operations
Language: Java 8
Desktop UI: Java Swing
Networking: UDP / DatagramSocket / DatagramPacket
Concurrency: Java Multithreading
Architecture: Client-Server
Build Tool / IDE: Ant, NetBeans
When a client starts:
- The client opens a
DatagramSocket. - It sends a connection message containing the username:
/c/<username>/e/
- The server generates a unique client ID.
- The server stores the client's:
- username
- IP address
- UDP port
- unique ID
- The generated ID is sent back to the client.
Client
│
│ /c/username/e/
▼
Server
│
│ Generate unique ID
│ Register client
▼
Client
│
└── /c/client-id/e/
When a user sends a message:
- The client prefixes the message with the username.
- The message is wrapped using the application's message protocol:
/m/<username>: <message>/e/
- The UDP datagram is sent to the server.
- The server receives the message and broadcasts it to every connected client.
- Each client extracts the message content and displays it in the chat window.
UDP Message
Client A ─────────────────────► Server
│
┌────────────┼────────────┐
│ │ │
▼ ▼ ▼
Client A Client B Client C
The server periodically sends the current connected-user list to all clients.
The user-list message uses the following format:
/u/user1/n/user2/n/user3/e/
Clients parse this message and update the Online Users window.
Because UDP does not maintain a persistent connection, the server periodically checks whether clients are still active.
- The server sends an identification request:
/i/server
- Each active client replies with its assigned ID:
/i/<client-id>/e/
- The server tracks missed responses.
- After repeated failures, the client is considered timed out and is removed from the active-client list.
When a client closes the application, it sends:
/d/<client-id>/e/
The server then removes that client from its active-user registry.
java-udp-chat-application/
└── java-chatApp-master/
└── java-chatApp-master/
├── src/
│ └── com/
│ └── Chat/
│ └── ChatApp/
│ ├── Client.java
│ ├── ClientWindow.java
│ ├── Login.java
│ ├── OnlineUsers.java
│ │
│ └── server/
│ ├── Server.java
│ ├── ServerClient.java
│ ├── ServerMain.java
│ └── UniqueIdentifier.java
│
├── build.xml
├── manifest.mf
└── nbproject/
Client.java handles the low-level UDP communication for each desktop client.
It is responsible for:
- creating the
DatagramSocket - connecting to the configured server address and port
- sending UDP datagrams
- receiving incoming datagrams
- storing the unique client ID assigned by the server
- closing the socket when the client disconnects
ClientWindow.java provides the main Java Swing chat interface and manages the client-side messaging workflow.
It handles:
- sending chat messages
- continuously listening for server messages
- parsing the application's message protocol
- displaying incoming chat messages
- updating the online-user list
- responding to server identification requests
- notifying the server when the client disconnects
Login.java provides the initial connection screen where the user enters:
- username
- server IP address
- server port
After validation, it launches the main chat window.
OnlineUsers.java displays the list of users currently connected to the server.
Server.java contains the core server-side networking logic.
It is responsible for:
- receiving UDP datagrams from clients
- registering new clients
- assigning unique client IDs
- maintaining the active-client registry
- broadcasting chat messages
- publishing online-user updates
- monitoring client responsiveness
- disconnecting timed-out clients
- processing server administration commands
ServerClient.java represents an individual connected client and stores information such as:
- username
- IP address
- UDP port
- unique client ID
- connection-monitoring attempts
ServerMain.java starts the UDP server on the configured port.
UniqueIdentifier.java generates unique IDs used by the server to identify connected clients.
The application contains both the UDP server and desktop client in the same Java project.
Make sure the following are installed:
- Java 8 or later
- Apache Ant, or NetBeans IDE with Ant support
git clone https://github.com/as271996/java-udp-chat-application.git
cd java-udp-chat-applicationThe NetBeans project is located inside:
java-chatApp-master/java-chatApp-master
Navigate to the NetBeans project directory:
cd java-chatApp-master/java-chatApp-masterBuild the application using Ant:
ant clean
ant jarThe generated JAR is created under:
dist/java-chatApp-master.jar
The server requires a port number as a command-line argument.
For example:
java -jar dist/java-chatApp-master.jar 8192This starts the UDP server on port 8192.
The server entry point is:
com.Chat.ChatApp.server.ServerMain
You can choose another available port instead of 8192.
Launch the Swing client using:
java -cp dist/java-chatApp-master.jar com.Chat.ChatApp.LoginThe login window asks for:
Username
Server IP Address
Server Port
For a server running on the same machine, use:
Server IP: 127.0.0.1
Server Port: 8192
Use the same port that was supplied when starting the server.
Launch the client command multiple times to simulate multiple users:
java -cp dist/java-chatApp-master.jar com.Chat.ChatApp.LoginEach client can connect with a different username.
UDP Server
Port 8192
│
┌───────────┼───────────┐
│ │ │
▼ ▼ ▼
Client A Client B Client C
Once connected, users can exchange broadcast messages and view the list of currently online users.
While the server is running, the following commands can be entered in the server console:
/clients
/kick <username-or-id>
/raw
/help
/quit
Text entered directly into the server console without a / prefix is broadcast to all connected clients as a server message.
This project was built as a networking-focused desktop application and can be extended further in several areas:
- Add user authentication and persistent user accounts
- Encrypt communication between clients and the server
- Add message delivery acknowledgements and retry handling
- Improve reliability for lost or out-of-order UDP packets
- Replace the custom string-based message protocol with a structured format such as JSON
- Add private messaging in addition to broadcast chat
- Add persistent server-side chat history
- Improve synchronization and thread safety around shared client state
- Support configurable message sizes instead of the current fixed UDP buffer
- Improve validation for usernames, IP addresses, and port numbers
- Add richer server administration and client-management controls
- Add automated unit and integration tests
- Replace the NetBeans/Ant setup with Maven or Gradle
- Modernize the Java Swing user interface
- Add better error handling and user-facing connection status
Amit Singh
Java networking project focused on UDP communication, client-server coordination, multithreading, and desktop application development.
- GitHub: github.com/as271996
- LinkedIn: linkedin.com/in/amit-singh-sp27
This project was built to explore network programming through a lightweight multi-user desktop chat application.
It demonstrates:
- UDP-based client-server communication using
DatagramSocketandDatagramPacket - registration and unique client identification
- broadcast messaging across multiple connected users
- online-user tracking
- client health monitoring and timeout detection
- multithreaded message handling
- server-side administrative commands
- Java Swing desktop UI development