{"id":25450143,"url":"https://github.com/rajnandinithopte/socket-programming--computer-networks","last_synced_at":"2025-07-13T08:34:25.541Z","repository":{"id":275403947,"uuid":"925964267","full_name":"rajnandinithopte/Socket-Programming--Computer-Networks","owner":"rajnandinithopte","description":"A UNIX socket programming project that implements a meeting scheduler using TCP and UDP communication. The system includes a client, main server, and two backend servers to process meeting requests and determine common available time slots efficiently. Built using C/C++ on Ubuntu, following POSIX socket programming principles.","archived":false,"fork":false,"pushed_at":"2025-02-03T05:19:42.000Z","size":203,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-16T09:08:53.640Z","etag":null,"topics":["computernetworks","cpp","posix","socket-programming","tcp","ubuntu","unix"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rajnandinithopte.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-02-02T07:29:56.000Z","updated_at":"2025-02-03T05:19:45.000Z","dependencies_parsed_at":"2025-02-02T09:27:37.797Z","dependency_job_id":"fc08fd1a-bef9-44da-b9c2-62396b714981","html_url":"https://github.com/rajnandinithopte/Socket-Programming--Computer-Networks","commit_stats":null,"previous_names":["rajnandinithopte/socket-programming--computer-networks"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/rajnandinithopte/Socket-Programming--Computer-Networks","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rajnandinithopte%2FSocket-Programming--Computer-Networks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rajnandinithopte%2FSocket-Programming--Computer-Networks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rajnandinithopte%2FSocket-Programming--Computer-Networks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rajnandinithopte%2FSocket-Programming--Computer-Networks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rajnandinithopte","download_url":"https://codeload.github.com/rajnandinithopte/Socket-Programming--Computer-Networks/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rajnandinithopte%2FSocket-Programming--Computer-Networks/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265109932,"owners_count":23712776,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["computernetworks","cpp","posix","socket-programming","tcp","ubuntu","unix"],"created_at":"2025-02-17T21:22:19.552Z","updated_at":"2025-07-13T08:34:25.520Z","avatar_url":"https://github.com/rajnandinithopte.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Socket-Programming-Computer-Networks\n\n## Developed a **meeting scheduling system** using **UNIX socket programming** with TCP and UDP communication.\n\n---\n\n## 🔹 Tech Stack\n\n**Programming Language:** C++  \n**Networking:** UNIX Sockets, TCP, UDP  \n**Development Environment:** Ubuntu 22.04 \n\n**Libraries \u0026 Tools:**\n- **Beej’s Guide to Network Programming** - Socket programming reference\n- **POSIX Sockets** - Implementing client-server communication\n- **Makefile** - For compiling and running executables\n\n---\n\n## 🔹 System Overview\n\n![System Architecture](architecture.png)\n\nThis project implements a **meeting scheduler** that:\n- Enables users to **schedule a meeting with a group of people**.\n- Uses **a client, main server, and two backend servers**.\n- Communicates between **client \u0026 main server using TCP** and **main server \u0026 backend servers using UDP**.\n- Finds **common available meeting slots** for multiple users.\n\nThe system consists of **four main components**:\n1. **Client (client.cpp)** - Takes user input (names) and requests available time slots.\n2. **Main Server (serverM.cpp)** - Manages user requests and forwards them to backend servers.\n3. **Backend Server A (serverA.cpp)** - Stores availability data for a subset of users.\n4. **Backend Server B (serverB.cpp)** - Stores availability data for another subset of users.\n\n---\n\n## 🔹 Workflow \u0026 Communication\n\n1. **Boot-up Phase**\n   - The **Main Server (ServerM)** starts and listens for incoming connections.\n   - **Backend Servers (ServerA \u0026 ServerB)** load user availability data from input files (`a.txt`, `b.txt`).\n   - **Backend Servers** send the list of stored users to **Main Server** via **UDP**.\n\n2. **Request \u0026 Forwarding Phase**\n   - The **Client** sends a list of participants to the **Main Server** via **TCP**.\n   - The **Main Server** determines which **Backend Server** stores the required users' availability.\n   - The **Main Server** forwards the request to the correct **Backend Server** via **UDP**.\n\n3. **Scheduling Phase**\n   - Each **Backend Server** finds the common available time slots for the requested users.\n   - The **Backend Server** returns the result to the **Main Server**.\n\n4. **Reply \u0026 Final Intersection Phase**\n   - The **Main Server** aggregates availability from both **Backend Servers**.\n   - The final list of available meeting times is sent back to the **Client** via **TCP**.\n\n---\n\n## 🔹 Code Files \u0026 Their Purpose\n\n### 1. **serverM.cpp** (Main Server)\n- Handles **client requests** and forwards them to the correct backend servers.\n- Creates a **TCP socket** to communicate with the **Client**.\n- Creates a **UDP socket** to communicate with **Backend Servers**.\n- Determines if the user belongs to `serverA` or `serverB` based on input files.\n- **Aggregates** the final meeting time slots from both backend servers.\n\n### 2. **serverA.cpp** (Backend Server A)\n- Creates a **UDP socket** to communicate with the **Main Server**.\n- Reads **user availability data** from `a.txt` and stores it in a **map data structure**.\n- Finds **common available time slots** for requested users.\n- Sends the result back to **Main Server**.\n\n### 3. **serverB.cpp** (Backend Server B)\n- Creates a **UDP socket** to communicate with the **Main Server**.\n- Reads **user availability data** from `b.txt` and stores it in a **map data structure**.\n- Finds **common available time slots** for requested users.\n- Sends the result back to **Main Server**.\n\n### 4. **client.cpp** (Client Program)\n- Creates a **TCP socket** to communicate with **Main Server**.\n- Accepts **usernames as input** (max 10 usernames, max 20 characters each).\n- Sends **user requests** to the **Main Server**.\n- Receives and **displays the available meeting slots**.\n\n---\n\n## 🔹 Constraints Considered While Developing the Algorithm\n\n### **🔸 Efficiency**\n- **User list processing (hashmap lookup):** `O(U)`\n- **Sorting user intervals before merging:** `O(N log N)`\n- **Merging time slots for intersection:** `O(N)`\n- **Sending \u0026 receiving data from backend servers:** `O(K)`\n- **Overall Time Complexity:** `O(U log U + N log N)`\n\n**Final Complexity:**  \n`O(U log U + N log N)`,  \nwhere **U = number of users in request** and **N = number of time intervals processed**.\n\n- **Pairwise intersection checking** ensures that only **valid overlapping intervals** are considered.\n- **Memory Efficiency:**  \n  - Uses **maps or hash tables** to store and retrieve user availability efficiently.\n  - Can handle **large input sizes (up to 200 lines per file) without excessive memory consumption**.\n\n---\n\n### **🔸 Input Validation \u0026 Formatting**\n- **Ensures Correct Time Intervals:**\n  - **Start time \u003c End time** for every time slot.\n  - Each interval must be **strictly increasing** (i.e., `t[i]_end \u003c t[i+1]_start`).\n- **Handles Formatting Issues:**\n  - **Removes unwanted spaces** before processing.\n  - Ensures that **usernames contain only lowercase letters** (as required).\n  - **Rejects invalid or malformed time intervals** (e.g., `[[1,1]]` or `[[10,5]]` are invalid).\n- **Handles Cases Where No Intersection Exists:**\n  - If **no overlapping time slots** exist, the algorithm correctly returns **an empty list (`[]`)**.\n\n---\n\n### **🔸 Scalability \u0026 Performance**\n- **Handles Large Datasets Efficiently:**\n  - The algorithm is optimized for processing **large input files (up to 200 lines each)**.\n  - Efficient use of **sorting and merging** prevents excessive computation time.\n- **Designed to Process Multiple Users in One Query:**\n  - Supports up to **10 usernames per request**.\n  - Uses **iterative intersection** to process multiple users efficiently.\n\n---\n\n### **🔸 Robust Handling of Edge Cases**\n- **Handles Single User Queries Efficiently:**\n  - If a **single user** is requested, the backend directly **returns their full availability** without unnecessary computations.\n- **Handles Very Short and Very Long Time Slots:**\n  - Works with **small intervals** like `[[1,2]]` and **large intervals** like `[[0,100]]`.\n- **Correctly Processes Scenarios Where Users Exist on Different Backend Servers:**\n  - Ensures that the **Main Server** correctly assigns **each user to the correct backend server**.\n  - Aggregates results from multiple backend servers correctly.\n\n---\n\n### **🔸 Consistency \u0026 Accuracy**\n- **Results Are Always Sorted and Maintain Valid Overlaps:**\n  - Ensures **final intersection lists remain sorted**.\n  - Guarantees that **all overlapping intervals are preserved**.\n- **Ensures All Time Slots Are Fully Processed:**\n  - Even if a user has **multiple separate availability slots**, the algorithm **correctly computes intersections across all of them**.\n- **Adheres to On-Screen Message Requirements:**\n  - **Backend servers print** results after computing intersections.\n  - **Main Server correctly forwards** results and prints appropriate messages.\n\n---\n\n# 🔹 Algorithm for Finding Common Meeting Slots\n\nThe **meeting scheduler algorithm** ensures that a meeting can be scheduled by finding **common available time slots** among multiple users stored across two backend servers.\n\n---\n\n## **🔸Step 1: Backend Servers Read and Store Data**\n- **Backend Server A (ServerA)** reads `a.txt` and stores availability data for **a subset of users**.\n- **Backend Server B (ServerB)** reads `b.txt` and stores availability data for **another subset of users**.\n- Each **backend server** sends a list of usernames it manages to the **Main Server** via **UDP**.\n\n### **Example Data in `a.txt` and `b.txt`**\n```\na.txt:\nalice;[[1,10],[11,12]]\nbob;[[5,9],[11,15]]\n\nb.txt:\namy;[[4,12]]\ncharlie;[[3,8],[9,15]]\n```\nAt this stage, **ServerA manages Alice and Bob**, while **ServerB manages Amy and Charlie**.\n\n---\n\n## **🔸 Step 2: Client Sends a Meeting Request**\n1. The **client** enters names of participants.\n2. The **client program** sends the list to the **Main Server** via **TCP**.\n3. The **Main Server** checks which **backend server** stores each user’s data.\n4. It **splits the list** and **forwards requests via UDP** to the respective backend servers.\n\n### **Example Client Request**\n```\nUser Input: alice bob amy\n```\nThe **Main Server** identifies:\n- `alice` and `bob` → Stored in **ServerA**\n- `amy` → Stored in **ServerB**\n\nThus, the **Main Server** sends:\n- **To ServerA**: Request for **alice, bob** availability.\n- **To ServerB**: Request for **amy** availability.\n\n---\n\n## **🔸 Step 3: Backend Servers Compute Individual Intersections**\nEach backend server:\n1. **Finds the time availability** for requested users.\n2. **Computes the pairwise intersection** of time slots.\n3. **Sends the result back to the Main Server**.\n\n### **Case 1: One User**\n- If only **one user** is in the request, the backend server directly sends **their availability**.\n\n#### **Example**\n```\nUser Input: amy\n```\nAmy’s availability from `b.txt`: `[[4,12]]`  \nBackend Server B returns:  \n```\n[[4,12]]\n```\n---\n\n### **Case 2: Two Users**\nIf two users exist, the backend server:\n- **Fetches both users’ time slots**.\n- **Finds overlapping intervals**.\n\n#### **Example (Alice \u0026 Bob)**\n```\nAlice: [[1,10],[11,12]]\nBob: [[5,9],[11,15]]\n```\n**Algorithm computes intersections**:\n- `[1,10]` and `[5,9]` → Intersection: **`[5,9]`**\n- `[11,12]` and `[11,15]` → Intersection: **`[11,12]`**\n\n#### **Backend Server A returns result**  \n```\n[[5,9],[11,12]]\n```\n---\n\n### **Case 3: More than Two Users**\nIf there are **three or more** users, the algorithm:\n1. **Finds the intersection between the first two users**.\n2. **Uses the result to find an intersection with the next user**.\n3. **Repeats this process until all users' availability is considered**.\n\n#### **Example (Alice, Bob, and Amy)**\n```\nAlice: [[1,10],[11,12]]\nBob: [[5,9],[11,15]]\nAmy: [[4,12]]\n```\n**Step 1: Find Alice \u0026 Bob’s intersection**\n```\n[[5,9],[11,12]]\n```\n**Step 2: Find intersection of result with Amy**\n```\n[[5,9],[11,12]] ∩ [[4,12]]\n```\n**Final Intersection Result**:  \n```\n[[5,9],[11,12]]\n```\nBackend Servers send results to the **Main Server**.\n\n---\n\n## **🔸 Step 4: Main Server Computes Final Intersection**\n1. **Main Server receives results** from **ServerA and ServerB**.\n2. **It runs another intersection algorithm** between the two sets.\n\n#### **Example**\n```\nServer A result: [[6,7],[10,12],[14,15]]\nServer B result: [[3,8],[9,15]]\n```\n **Final Intersection Computed by Main Server**  \n```\n[[6,7],[10,12],[14,15]]\n```\n**Main Server sends this to the client.**\n\n---\n\n## **🔸 Step 5: Client Receives and Displays Final Meeting Slots**\nOnce the **client receives the final result**, it **displays the available meeting times**:\n\n#### **Example Client Output**\n```\nTime intervals [[6,7],[10,12],[14,15]] work for alice, bob, amy.\n```\n\n **If no common slots exist, it returns** `[]`.\n\n---\n\n# 🔹Port Assignments\n\n| Process  | Protocol | Port Number|\n|----------|----------|------------|\n| Server A | UDP      | **21+XXX** |\n| Server B | UDP      | **22+XXX** |\n| Server M | UDP      | **24+XXX** |\n| Server M | UDP      | **23+XXX** |\n| Client   | TCP      | **Dynamic**|\n\nEach process communicates using the specified **ports** to ensure correct message routing.\n\n\n# 🔹How to Run the Meeting Scheduling System\n\n## 1. Compile the Code\nUse the `Makefile` to compile all server and client programs:\n```sh\nmake all\n```\n\nThis will generate the following executable files:\n- **serverM** (Main Server)\n- **serverA** (Backend Server A)\n- **serverB** (Backend Server B)\n- **client** (Client Program)\n\n---\n\n## 2.  Start the Servers and Client\n\n### Step 1: Start the Main Server\nOpen a new terminal window and run:\n```sh\n./serverM\n```\n**Expected Output:**\n```\nMain Server is up and running.\n```\n\n### Step 2: Start Backend Server A\nIn another terminal window, run:\n```sh\n./serverA\n```\n**Expected Output:**\n```\nServer A is up and running using UDP on port \u003cport number\u003e.\nServer A finished sending a list of usernames to Main Server.\n```\n\n### Step 3: Start Backend Server B\nIn a third terminal window, run:\n```sh\n./serverB\n```\n**Expected Output:**\n```\nServer B is up and running using UDP on port \u003cport number\u003e.\nServer B finished sending a list of usernames to Main Server.\n```\n\n### Step 4: Start the Client\nIn a fourth terminal window, run:\n```sh\n./client\n```\n**Expected Output:**\n```\nClient is up and running.\nPlease enter the usernames to check schedule availability:\n```\n\n---\n\n## 3. Enter User Input in the Client\nOnce the client is running, enter a list of usernames:\n```\nPlease enter the usernames to check schedule availability:\nalice bob charlie\n```\n\nThe client will process the request and return available time slots.\n\n---\n\n## 4. Communication Flow \u0026 Expected Messages\n\n### 1. Client to Main Server\nAfter sending the request:\n```\nClient finished sending the usernames to Main Server.\n```\n\nIf some usernames do not exist:\n```\nClient received the reply from Main Server using TCP over port \u003cport number\u003e:\n\u003cusername1, username2, …\u003e do not exist.\n```\n\nIf usernames exist:\n```\nMain Server received the request from client using TCP over port \u003cport number\u003e.\nFound \u003cusername1, username2, …\u003e located at Server \u003cA or B\u003e. Send to Server \u003cA or B\u003e.\n```\n\n### 2. Main Server to Backend Servers\n```\nMain Server received the username list from server A using UDP over port \u003cport number\u003e.\nMain Server received the username list from server B using UDP over port \u003cport number\u003e.\n```\n\nEach backend server will process its data and return the intersection:\n```\nServer \u003cA or B\u003e received the usernames from Main Server using UDP over port \u003cport number\u003e.\nFound the intersection result: \u003c[[t1_start, t1_end], [t2_start, t2_end], … ]\u003e for \u003cusername1, username2, …\u003e.\nServer \u003cA or B\u003e finished sending the response to Main Server.\n```\n\n### 3. Main Server Processing the Final Result\n```\nMain Server received from server A the intersection result using UDP over port \u003cport number\u003e:\n\u003c[[t1_start, t1_end], [t2_start, t2_end], … ]\u003e.\nMain Server received from server B the intersection result using UDP over port \u003cport number\u003e:\n\u003c[[t1_start, t1_end], [t2_start, t2_end], … ]\u003e.\nFound the intersection between the results from server A and B:\n\u003c[[t1_start, t1_end], [t2_start, t2_end], … ]\u003e.\nMain Server sent the result to the client.\n```\n\n### 4. Client Receives the Final Meeting Time\n\n```\nClient received the reply from Main Server using TCP over port \u003cport number\u003e:\nTime intervals \u003c[[t1_start, t1_end], [t2_start, t2_end], … ]\u003e works for \u003cusername1, username2, …\u003e.\n```\n\n---\n\n## 5. Stop the Servers\nOnce you're done testing, stop all servers and the client by pressing:\n```sh\nCTRL + C\n```\nThis will terminate the processes.\n\n\n## 🔹 References\nThis project references concepts from:\n1. **Beej's Guide to Network Programming**: [Beej's Guide](http://www.beej.us/guide/bgnet/)\n   - Chapter 3: `structs`\n   - Chapter 4: `socket(), bind(), connect(), listen(), accept(), send()/recv(), sendto()/recvfrom()`\n   - Chapter 9.12: `htons()`\n   - Chapter 9.24: `struct sockaddr_in`\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frajnandinithopte%2Fsocket-programming--computer-networks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frajnandinithopte%2Fsocket-programming--computer-networks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frajnandinithopte%2Fsocket-programming--computer-networks/lists"}