https://github.com/justin-marian/tcp-udp-server
Client-server messaging platform using TCP/UDP sockets, publish-subscribe, message forwarding, client subscriptions, and store-and-forward functionality, and multiplexed connections.
https://github.com/justin-marian/tcp-udp-server
c multiplexing server-client subscription tcp-udp
Last synced: 3 months ago
JSON representation
Client-server messaging platform using TCP/UDP sockets, publish-subscribe, message forwarding, client subscriptions, and store-and-forward functionality, and multiplexed connections.
- Host: GitHub
- URL: https://github.com/justin-marian/tcp-udp-server
- Owner: justin-marian
- License: mit
- Created: 2024-03-13T06:06:45.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-15T12:45:16.000Z (3 months ago)
- Last Synced: 2025-02-15T13:24:06.076Z (3 months ago)
- Topics: c, multiplexing, server-client, subscription, tcp-udp
- Language: C
- Homepage:
- Size: 41 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# π Subscription Client Management Platform
This project is a **client-server application** that facilitates **message management** using **sockets**. It follows the **client-server model**, where the **server acts as a broker**, enabling **clients to publish and subscribe to messages**.
The implementation includes:
- π **TCP and UDP communication** for data transmission.
- β‘ **Multiplexing TCP and UDP connections** to handle multiple clients simultaneously.
- π‘ **A predefined protocol over UDP** to ensure structured message exchange.
- π§ **A client-server architecture** using sockets for network communication.---
## βοΈ 1. System Architecture
The project consists of three main components: **a server, TCP clients, and a UDP client**. These components work together to implement a **publish-subscribe messaging system**.
### π₯οΈ Server
- Opens **two sockets**: **one TCP and one UDP**.
- Listens for incoming connections and messages on all available **IP addresses**.
- Manages **client subscriptions**, **message forwarding**, and **store-and-forward (SF) functionality**.### π§ TCP Clients
- Connect to the server via **TCP sockets**.
- Can **subscribe** or **unsubscribe** from topics.
- Receive and display **only messages related to their subscribed topics**.### π‘ UDP Client
- Publishes messages to the server using a **predefined protocol**.
- Messages are **distributed only** to TCP clients that are subscribed to the **relevant topics**.
- The UDP client does **not require persistent connections**.### π₯ Store-and-Forward (SF) Functionality
- When a **TCP client disconnects**, the server **stores messages** for its subscribed topics.
- Upon **reconnection**, the stored messages are **delivered to the client** to ensure **no data loss**.
- Ensures **message reliability** even in case of temporary disconnections.---
## ποΈ 2. Buffer Structure
The buffer data structure provides **dynamic storage** for elements with a fixed initial capacity, which can **grow as needed**. It is used for **queueing messages** and **managing client subscriptions** in a **publish-subscribe system**.
### π Store/Forward Subscription Structure
In addition to standard buffer functionality, this structure is used for **store/forward client subscriptions**, ensuring that disconnected clients receive messages upon reconnection.
- Maintains **two buffers**:
- **π Subscribed topics buffer** β Tracks topics each client is subscribed to.
- **ποΈ Store/forward message buffer** β Stores messages for clients with active store-forward subscriptions.### ποΈ Buffer Functions
- **`buff init(int cap, size_t size)`** β Initializes a buffer with a specified **capacity** and **element size**.
- **`int len(buff buffer)`** β Returns the **current number of elements** stored in the buffer.
- **`void *get_pos(buff buffer, int pos)`** β Retrieves the **element at a given position** in the buffer.
- **`void add_pos(buff buffer, void *data, int pos)`** β Inserts a **new element** at the specified position.
- **`void del_pos(buff buffer, int pos)`** β Removes the **element at a specific position** and shifts remaining elements.This buffer implementation is **optimized for real-time applications**, ensuring data storage and retrieval for message queues and client subscriptions.
---
## π 3. Header Files
### π¨ `message.h`
- Defines **maximum lengths** for message content, topic names, client IDs, and command strings.
- Contains **command length constants** for `"subscribe"`, `"unsubscribe"`, and `"exit"` commands.
- Includes standard **C libraries** and **networking headers** for handling sockets, message transmission, and address conversions.### β `helper.h`
- Provides **error handling utilities**.
- Defines the **`DIE()`** macro, which acts as a **generic error handler**.
- If an error condition occurs, `DIE()` prints an error message and terminates execution.### π οΈ `list.h` / `queue.h`
#### π `list.h`
- Implements a **linked list** for dynamically storing elements.
- Provides **list manipulation functions**:
- `cons()` β Creates a new list node with a given element.
- `cdr_and_free()` β Removes the **head of the list** and deallocates memory.#### π `queue.h`
- Implements a **queue** based on the linked list structure.
- Provides **queue management functions**:
- `queue_create()` β Initializes a new queue.
- `queue_enq()` β Adds an element to the queue.
- `queue_deq()` β Removes and returns the front element.
- `queue_destroy()` β Frees memory for the entire queue.These data structures are used in **message buffering, client subscription tracking, and store-and-forward functionalities**.
---
## π§βπ» 4. Subscriber
The client connects to the server over **TCP sockets**, subscribes to topics, receives messages, and processes commands from the user.
**π οΈ Key Functionalities:**
- **π Establishing a Connection:**
- Opens a TCP socket to connect to the server.
- Sends a unique **client ID** to the server for identification.
- Disables the **Nagle algorithm** to reduce latency.- **π₯ Handling User Input and Server Messages:**
- Uses `select()` to wait for activity on **stdin** or the **TCP socket**.
- Processes **user commands** and **incoming messages** from the server.### π Commands
- **`subscribe `** β Client subscribes to a topic. If **SF (store-and-forward)** is enabled, the server saves messages while the client is offline.
- **`unsubscribe `** β Client unsubscribes from a topic.
- **`exit`** β The client closes the TCP connection and releases allocated resources.### π‘ Message Handling
- Processes **different message types**, including:
- **`processIntMessage()`** β Handles integer-based messages.
- **`processShortRealMessage()`** β Handles floating-point messages.
- Extracts metadata such as **server IP, port, and topic**.- **π Error Handling:**
- Errors are handled using the **`DIE()` macro**.
- The client terminates the connection and releases memory when shutting down.---
## π₯οΈ 5. Server
The server **manages client connections, subscriptions, and messages**. It listens on **both TCP and UDP sockets**, forwarding messages to subscribed clients.
### π Key Functions
- **π `accept_new_connection()`** β Accepts new TCP clients, disables **Nagle's algorithm**, and registers them.
- **π₯ `receive_udp_message()`** β Extracts topic from **UDP messages** and forwards them to **subscribed clients**.
- **π‘ `subscribe()`** β Adds a client to a topicβs subscription list.
- **π« `unsubscribe()`** β Removes a client from a topicβs subscription list.
- **π¨ Message Processing:**
- **Forwards messages** to subscribed clients if they are **online**.
- If a client is **offline with SF enabled**, messages are **stored** for later delivery.The server ensures **real-time message delivery**, while **disconnected clients receive stored messages upon reconnection**.
---