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

CSL 332 Networking Lab KTU 2019 Scheme - Dr Binu V P

CSL 332 Networking Lab KTU BTech 2019 Scheme About Me Scheme Syllabus Experiments 1.Learn the Networking Commands and Network Configuration Files     Basic networking commands     More Networking commands     Network configuration Files     View the configuration and address of your network interface     Network Connectivity      View Active TCP connections     MAC address of another machine using ARP  2.  System calls in Network Programming 3.  Simple TCP/IP Client Server Program 4.  Simple UDP Client Server Program 5.Application Programs     Concurrent UDP Time Server     Checking Prime Numbers 6. Simulate ARQ Protocols  / sliding window protocols          Stop and Wait           Go-Back-N          Selective Repeat  7. Routing Protocols - Distance Vector and Link State   ...

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...

Stop and Wait ARQ

Here's a simple C program that demonstrates the Stop-and-Wait ARQ protocol. This basic implementation simulates the sender transmitting packets one at a time and waiting for an acknowledgment from the receiver. If the acknowledgment is not received, the sender retransmits the packet. Key Points: The sender sends one packet at a time. If the receiver acknowledges it (ACK), the sender sends the next packet. If the acknowledgment is lost, the sender retransmits after a timeout. C Program: Stop-and-Wait ARQ Simulation #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h>  // for sleep() #define TIMEOUT 3  // Timeout duration in seconds #define TOTAL_PACKETS 5  // Number of packets to send int simulate_acknowledgment() {     // Simulate a 70% chance of successful acknowledgment     return rand() % 10 < 7; } int main() {     srand(time(0));  // Seed for random number generation     i...