https://github.com/sungj921028/rt-chat-room
A real time chat room with server and client design, supplies a lot of implementations.
https://github.com/sungj921028/rt-chat-room
c chatroom client-server communication cpp network streaming
Last synced: 3 months ago
JSON representation
A real time chat room with server and client design, supplies a lot of implementations.
- Host: GitHub
- URL: https://github.com/sungj921028/rt-chat-room
- Owner: SunGj921028
- Created: 2025-01-01T15:58:45.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-11-20T07:31:03.000Z (8 months ago)
- Last Synced: 2025-11-20T09:25:04.510Z (8 months ago)
- Topics: c, chatroom, client-server, communication, cpp, network, streaming
- Language: C++
- Homepage:
- Size: 2.08 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# RT-chat-room
以 C++ 實作的即時通訊系統,聚焦在「**多用戶併發 + 安全通訊 + 多媒體傳輸**」三件事:
- TLS 加密的 client-server 通訊
- thread pool 架構下的多 client 連線處理
- 訊息、檔案、音訊三種資料型態的 relay
> Demo Video: [YouTube](https://www.youtube.com/watch?v=BcTFOp-aB6Y)(建議 0.75x)
---
## Project Highlights
- **Secure by default**: 所有資料透過 OpenSSL TLS 通道傳輸
- **Concurrent server design**: `accept()` + thread pool,最多同時處理 10 位 client
- **Real-time relay model**: client-client 溝通由 server 轉發,降低點對點連線複雜度
- **Multimodal transport**: 文字訊息、檔案(`txt`/`md`)、音訊(`wav`)
- **Integrity check for relayed files**: 使用 SHA-256 驗證轉傳檔案完整性
---
## Tech Stack
- **Language**: C++
- **Network**: POSIX Socket (`AF_INET`, `SOCK_STREAM`)
- **Security**: OpenSSL (`SSL/TLS`, cert verification)
- **Concurrency**: POSIX Threads (`pthread`), worker pool
- **Audio Playback**: `aplay` (`alsa-utils`)
- **Build**: `Makefile`
---
## System Architecture
```text
┌───────────────┐ TLS ┌────────────────────────────┐ TLS ┌───────────────┐
│ Client A │ <----------------> │ Server │ <----------------> │ Client B │
└───────────────┘ │ - Connection manager │ └───────────────┘
┌───────────────┐ TLS │ - ThreadPool (workers) │ TLS ┌───────────────┐
│ Client C │ <----------------> │ - UserManager │ <----------------> │ Client D │
└───────────────┘ │ - File/Audio relay pipeline│ └───────────────┘
└────────────────────────────┘
```
### Core Modules
- `server.cpp`, `server.hpp`
- 啟動 socket lifecycle(`socket -> bind -> listen -> accept`)
- 建立 TLS server context(載入 `server.crt`、`server.key`)
- 控制連線上限 `MAX_CLIENTS`
- `ThreadPool.cpp`, `ThreadPool.hpp`
- worker queue 處理 client 任務
- 指令解析與 6 種 send mode 的處理/轉發邏輯
- `UserManager.cpp`, `UserManager.hpp`
- 註冊、登入、登出與在線狀態管理
- socket 與 username 的映射
- `myfile.cpp`, `myfile.hpp`
- chunk/frame 收送
- client->client 檔案轉傳與 hash 驗證
- 音訊串流與播放
- `defAndFuc.cpp`, `defAndFuc.hpp`
- 共用常數、輸出訊息、CLI 選單、工具函式
---
## Feature Set
### 1) Authentication
- `register `
- `login `
- `logout`
- `exit`(已登入時需先 logout)
### 2) Messaging
- Client -> Server 訊息送達
- Client -> Client 私訊(經 server relay)
### 3) File Transfer
- 支援 `txt`、`md`
- Client -> Server 與 Client -> Client
- `EOF` 作為傳輸結束訊號
- 轉傳檔案會附上 hash metadata 並由接收端驗證完整性
### 4) Audio Streaming
- 支援 `wav`
- frame-based 傳輸(`FRAME_SIZE = 8192`)
- 接收端透過 `aplay -` 即時播放
---
## Command Flow
當輸入 `send` 後,可選擇 6 種模式:
1. `send `
傳訊息給 server
2. `send `
傳訊息給指定 client
3. `send `
傳檔案給 server
4. `send `
傳檔案給指定 client
5. `send `
傳音訊給 server
6. `send `
傳音訊給指定 client
---
## Run Locally
### Requirements
- Linux (Ubuntu recommended)
- `g++`
- `libssl-dev`
- `alsa-utils`
```bash
sudo apt update
sudo apt install -y build-essential libssl-dev alsa-utils
```
### Build
```bash
make
```
### Start Server
```bash
./server
```
### Start Clients
```bash
./client
```
> 先開 server,再開多個 client 進行互測。
---
## Quick Demo Script
Client 1:
```bash
register alice 1234
login alice 1234
```
Client 2:
```bash
register bob 1234
login bob 1234
```
Client 1 傳私訊給 Client 2:
```text
send
# choose: 2
send bob hello
```
Client 1 傳檔給 Client 2:
```text
send
# choose: 4
send bob test.txt
```
---
## Project Structure
```text
RT-chat-room/
├── server.cpp
├── server.hpp
├── client.cpp
├── ThreadPool.cpp
├── ThreadPool.hpp
├── UserManager.cpp
├── UserManager.hpp
├── myfile.cpp
├── myfile.hpp
├── defAndFuc.cpp
├── defAndFuc.hpp
├── users.txt # 帳號資料(明文)
├── server.crt # TLS certificate
├── server.key # TLS private key
└── serverFile/ # server 接收檔案目錄(執行時可能自動建立)
```
Runtime 可能額外產生:
- `clientFile_/`(client 端接收檔案)
---
## Design Notes and Trade-offs
- **Relay over peer-to-peer**: 實作更單純,集中控管安全與權限。代價是 server 負載較高
- **Thread pool over one-thread-per-client**: 降低 thread 建立成本。代價是 queue latency 需平衡
- **Simple local persistence** (`users.txt`): 開發速度快。代價是安全性不足(目前為明文)
---
## Current Limitations
- 允許檔案格式:`txt`、`md`、`wav`
- 帳號密碼為明文儲存(`users.txt`)
- CLI 依賴 ANSI color 與 `system("clear")`
---
## Future Improvements
- 導入密碼雜湊(bcrypt/Argon2)取代明文儲存
- 支援群組聊天室與聊天室歷史訊息
- 改善傳輸協議(長度前綴封包,取代字串關鍵字判斷)
- 加入 retry/backoff 與斷線恢復機制
- 增加自動化測試(單元測試 + 整合測試)
---
## Cleanup
```bash
make clean
```