Skip to main content

Selective Repeat ARQ


Selective Repeat ARQ Protocol - Simple Steps

Initialization
  • Set up a window size (N) to control how many frames can be sent before receiving acknowledgments.
  • Initialize counters for sent frames and received acknowledgments.
Send Frames
  • Transmit frames within the current sliding window.
  • Each frame is assigned a sequence number.
Simulate Frame Loss (Optional)
  • Frames may be randomly lost during transmission (simulated by probability).
Receive Acknowledgments
  • The receiver acknowledges individual frames as they arrive.
  • Out-of-order frames are buffered until missing frames are received.
Retransmit Lost Frames
  • If an acknowledgment for a frame is not received, only that specific frame is retransmitted.
Slide the Window
  • Once the lowest-numbered frame (base) is acknowledged, the window slides forward.
  • The sender can now transmit new frames.
Repeat Until Completion
  • The process continues until all frames are sent and acknowledged.
Key Points
  • Efficient Use of Bandwidth: Only lost frames are retransmitted, avoiding unnecessary duplication.
  • Out-of-Order Handling: The receiver can accept and store frames out of sequence.
  • Sliding Window: Controls the flow of data by limiting the number of unacknowledged frames.
Selective Repeat ARQ protocol simulation

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>


#define WINDOW_SIZE 4 // Sliding window size
#define TOTAL_FRAMES 10 // Total number of frames to send
#define LOSS_PROBABILITY 20 // Percentage probability of frame loss (0-100)


// Simulate frame transmission
int send_frame(int frame_number) {
printf("Sending frame %d...\n", frame_number);
sleep(1); // Simulate delay
int rand_value = rand() % 100; // Generate random number between 0 and 99
if (rand_value < LOSS_PROBABILITY) {
printf("Frame %d lost during transmission!\n", frame_number);
return 0;
}
printf("Frame %d sent successfully.\n", frame_number);
return 1;
}


// Simulate receiving acknowledgment
int receive_ack(int frame_number) {
printf("Receiving acknowledgment for frame %d...\n", frame_number);
sleep(1); // Simulate delay
int rand_value = rand() % 100;
if (rand_value < LOSS_PROBABILITY) {
printf("Acknowledgment for frame %d lost!\n", frame_number);
return 0;
}
printf("Acknowledgment for frame %d received.\n", frame_number);
return 1;
}


// Selective Repeat ARQ Protocol
void selective_repeat_arq() {
int sent_frames[TOTAL_FRAMES] = {0}; // Track sent frames
int ack_received[TOTAL_FRAMES] = {0}; // Track acknowledgments received
int base = 0; // Start of sliding window


while (base < TOTAL_FRAMES) {
// Send frames within the window
for (int i = base; i < base + WINDOW_SIZE && i < TOTAL_FRAMES; i++) {
if (!sent_frames[i]) {
sent_frames[i] = send_frame(i); // Send frame if not sent
}
}


// Check for acknowledgments
for (int i = base; i < base + WINDOW_SIZE && i < TOTAL_FRAMES; i++) {
if (sent_frames[i] && !ack_received[i]) {
ack_received[i] = receive_ack(i); // Mark acknowledgment if received
}
}


// Slide the window if the base frame is acknowledged
while (base < TOTAL_FRAMES && ack_received[base]) {
printf("Sliding window forward. Frame %d fully acknowledged.\n", base);
base++;
}
}
printf("All frames sent and acknowledged successfully.\n");
}


// Main function
int main() {
srand(time(0)); // Seed random number generator
selective_repeat_arq();
return 0;
}

Explanation of the Code:

  1. Window Size: The WINDOW_SIZE variable determines the number of frames that can be in transit without acknowledgment.
  2. Frame Loss Simulation: Random numbers are used to simulate the loss of frames and acknowledgments.
  3. Sliding Window: The base variable keeps track of the lowest-numbered frame in the window.
  4. Frame Transmission: The program attempts to send frames in the window, and unacknowledged frames are retransmitted until acknowledgment is received.
  5. Acknowledgment Handling: If an acknowledgment is lost, the corresponding frame is retransmitted.
  6. Sliding the Window: The window slides forward only when the base frame (lowest in the window) is acknowledged.

This code is a simplified simulation and does not implement a real network stack. It focuses on the core logic of the Selective Repeat ARQ protocol.

Comments

Popular posts from this blog

Server/Client Communication-python

The basic mechanisms of client-server setup are: A client app send a request to a server app.  The server app returns a reply.  Some of the basic data communications between client and server are: File transfer - sends name and gets a file.  Web page - sends url and gets a page.  Echo - sends a message and gets it back.  Client server communication uses socket.              To connect to another machine, we need a socket connection. What's a connection?  A relationship between two machines, where two pieces of software know about each other. Those two pieces of software know how to communicate with each other. In other words, they know how to send bits to each other. A socket connection means the two machines have information about each other, including network location (IP address) and TCP port. (If we can use anology, IP address is the phone number and the TCP port is the extension).  A so...

Banker's Algorithm

Banker's algorithm is a deadlock avoidance algorithm. It is named so because this algorithm is used in banking systems to determine whether a loan can be granted or not. Consider there are n account holders in a bank and the sum of the money in all of their accounts is S. Everytime a loan has to be granted by the bank, it subtracts the loan amount from the total money the bank has. Then it checks if that difference is greater than S. It is done because, only then, the bank would have enough money even if all the n account holders draw all their money at once. Banker's algorithm works in a similar way in computers. Whenever a new process is created, it must exactly specify the maximum instances of each resource type that it needs. Let us assume that there are n processes and m resource types. Some data structures are used to implement the banker's algorithm. They are: Available: It is an array of length m . It represents the number of available resourc...

Inter Process Communication-Message Queue

Interprocess communication (IPC) is a set of programming interfaces that allow a programmer to coordinate activities among different program processes that can run concurrently in an operating system. This allows a program to handle many user requests at the same time. Since even a single user request may result in multiple processes running in the operating system on the user's behalf, the processes need to communicate with each other. The IPC interfaces make this possible. Each IPC method has its own advantages and limitations so it is not unusual for a single program to use all of the IPC methods . Message Based Communication Messages are a very general form of communication. Messages can be used to send and receive formatted data streams between arbitrary processes. Messages may have types. This helps in message interpretation. The type may specify appropriate permissions for processes. Usually at the receiver end, messages are put in a queue. Messages may also be fo...