19 Commits
Author SHA1 Message Date
mohammadreza 275a89a59a Delete images/a 2026-06-24 13:42:25 +00:00
mohammadreza 5e246a2968 Upload files to "images" 2026-06-24 13:42:15 +00:00
mohammadreza 267c01d463 Add images/a 2026-06-24 13:41:03 +00:00
mohammadreza 379efffb4e Delete images 2026-06-24 13:40:48 +00:00
mohammadreza ba426c6ca3 Delete login_success.png 2026-06-24 13:39:35 +00:00
mohammadreza 3bd9d46aa4 Delete login_screen.png 2026-06-24 13:39:28 +00:00
mohammadreza 06862f2376 Delete chat_interface.png 2026-06-24 13:39:16 +00:00
mohammadreza 176b52a2cb Upload files to "/" 2026-06-24 13:39:01 +00:00
mohammadreza bf7d8ee05a Add images 2026-06-24 13:38:00 +00:00
mohammadreza f75fd98a2f Delete images.png 2026-06-24 10:19:20 +00:00
mohammadreza 8461a9986a Update images.png 2026-06-24 10:19:07 +00:00
mohammadreza 17d597dd4c Add images 2026-06-24 10:18:16 +00:00
mohammadreza 523871e957 Delete images 2026-06-24 10:17:54 +00:00
mohammadreza 7c206ee192 Delete chat_interface.png 2026-06-24 10:17:43 +00:00
mohammadreza 22fbe41051 Delete login_screen.png 2026-06-24 10:17:36 +00:00
mohammadreza 5e7aa9cae0 Delete login_success.png 2026-06-24 10:17:28 +00:00
mohammadreza 0356d6f574 Upload files to "/" 2026-06-24 10:17:10 +00:00
mohammadreza 8f819673b8 Add images 2026-06-24 10:16:55 +00:00
mohammadreza db9e1ceb05 Update README.md 2026-06-24 10:12:50 +00:00
4 changed files with 27 additions and 123 deletions
+27 -123
View File
@@ -1,132 +1,36 @@
## Network File-Sharing chat System (Java)
# UniChat - Network File-Sharing chat System (Java)
### Overview
This project is a multi-client network chat system with file sharing, implemented using **Java sockets**, **MUltithreading** and **Object Serialization**.
Clients connect to a central server and can:
A modern, real-time chat application built using **JavaFX** and **Java Socket Programming**. UniChat allows users to connect to a central server, see who is online, and engage in both public and private conversations through a clean, dark-themed interface.
* send public chat message
* send private messages
* request the list of online users
* transfer files to other users
## 🚀 Key Features
communication between client and server is implemented using Java Object Streams(`ObjectInputStream` / `ObjectOutputStream`) so that structured objects can be transferred directly over the network.
* **Authentication System:** A dedicated login screen where users can authenticate with a username.
* **Real-time Public Chat:** A global communication channel to broadcast messages to all connected users.
* **Private Messaging:** Targeted, one-on-one communication capabilities between users.
* **Live User List:** A sidebar that displays all currently connected users in real-time.
* **Modern UI:** Built with JavaFX, featuring a responsive, dark-themed interface for a comfortable user experience.
## 📸 Project Interface
| Login Screen | Authentication Success | Main Chat Interface |
| :---: | :---: | :---: |
| ![Login](images/login_screen.png) | ![Success](images/login_success.png) | ![Chat](images/chat_interface.png) |
## 🛠 Tech Stack
* **Language:** Java 17+
* **GUI Framework:** JavaFX
* **Networking:** Java Sockets (TCP)
* **Concurrency:** Multi-threaded Client-Server architecture
## 💻 How to Run
1. **Start the Server:** Ensure the `Server` application is running to listen for incoming connections.
2. **Launch the Client:** Run the `Launcher` class to open the UI.
3. **Connect:** Enter your desired username on the login screen and click "Connect".
---
### Architecture
1. **ChatServer** opens a `ServerSocket` and waits for connections.
2. Each connecting client gets its own **ClientSession** thread.
3. A shared **UserManager** tracks who's online and routes messages.
4. Everything is exchanged as serialized objects (`ChatMessage` / `FileMessage`).
```
Client → Socket → Server → ClientSession (thread) → UserManager → Broadcast/Route → Clients
```
---
### Message Protocol
Two serializable classes carry everything over the wire:
* **ChatMessage** text-based: logins, public/private messages, user-list requests, server replies.
* **FileMessage** file transfers, carrying the file as a `byte[]`.
`MessageType` is an enum (`LOGIN`, `LOGIN_SUCCESS`, `LOGIN_FAILED`, `PUBLIC_MESSAGE`, `PRIVATE_MESSAGE`, `USER_LIST`, …) so both sides agree on what each message means.
---
### Package Structure
```
com.university.chat
├── common → shared message classes (ChatMessage, FileMessage, MessageType)
├── server → server-side logic
└── client → client application
```
#### `common`
Already complete — these define the protocol and don't need any changes.
#### `server`
* **ChatServer** entry point. Opens the server socket and creates a `ClientSession` per client.
* **ClientSession** one thread per client. Handles login, reads incoming messages, and dispatches them (broadcast, private message, user list, file transfer).
* **UserManager** thread-safe registry of online users (`ConcurrentHashMap<String, ClientSession>`). Already complete.
* **FileManager** manages per-user folders on disk (`server_data/<username>/sent` and `received`). Already complete.
#### `client`
* **chatClient** entry point. Connects to the server, logs in, starts a listener thread, and runs the input loop for user commands.
* **ServerListener** background thread that continuously reads and displays messages/files coming from the server.
* **TransferProgress** small helper that prints a progress bar during file uploads. Already complete.
---
### Client Commands
```
/msg <username> <message> → private message
/users → list online users
/sendfile <username> <filepath> → send a file
<anything else> → public message
```
---
### File Transfer Flow
1. Client reads the file and converts it to `byte[]`.
2. It's wrapped in a `FileMessage` and sent to the server.
3. The server saves a copy (in `sent/` for the sender, `received/` for the receiver) and forwards it to the recipient.
---
### Threading Model
* **Server**: one `ClientSession` thread per connected client.
* **Client**: main thread handles user input, a `ServerListener` thread handles incoming messages — so you can chat and receive **at the same time**.
---
## Your Task
Several core pieces are left as **TODOs** for you to implement. Follow the comments in each file alongside the structure mentioned above to complete them.
| File | What to implement |
|------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `Server/ChatServer.java` | Pick a port, create a shared `UserManager`, open the server socket, accept clients in a loop, and create a `ClientSession` thread per client. |
| `Server/ClientSession.java` | Set up object streams in the constructor; handle login (success/failure); receive messages in a loop and dispatch `ChatMessage`/`FileMessage`; broadcast, private message, and user-list logic; remove user on disconnect; forward files. |
| `Client/chatClient.java` | Connect to the server, set up streams, log in, start `ServerListener`, and implement the command loop (`/msg`, `/users`, `/sendfile`, plain messages). |
| `Client/ServerListener.java` | Continuously read objects from the server and print chat messages / file-received notifications. |
**Tips:**
* Always create the `ObjectOutputStream` *before* the `ObjectInputStream` on both ends — this avoids a stream-handshake deadlock.
* Don't forget `out.flush()` after `writeObject(...)`.
* Test with two or more client instances to verify broadcasting, private messages, and file transfer all work correctly.
#### Running multiple clients in IntelliJ
1. Click the **⋮** icon next to the run button → **Edit Configurations**
2. Open **Modify options** (Alt+M)
3. Enable **Allow multiple instances** (Alt+U)
---
## Optional Bonus: JavaFX UI
If you want to go further replace the console client with a simple **JavaFX** GUI (chat window, online-user list, file-send button, etc.).
1. **Add the JavaFX dependencies** to your `pom.xml` — for Maven:
```xml
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>21</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>21</version>
</dependency>
```
You'll also need the `javafx-maven-plugin` to run the app from Maven.
2. **Create a JavaFX entry point**, e.g. `Client/ChatClientApp.java`, extending `javafx.application.Application` and implementing `start(Stage stage)`. This becomes your new launch class instead of (or alongside) `chatClient`.
3. Reuse your existing socket/streams logic — just move the "send" actions to button handlers and update the UI from `ServerListener` using `Platform.runLater(...)` (**since UI updates must happen on the JavaFX Application Thread**).
Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB