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.
- Transmit frames within the current sliding window.
- Each frame is assigned a sequence number.
- Frames may be randomly lost during transmission (simulated by probability).
- The receiver acknowledges individual frames as they arrive.
- Out-of-order frames are buffered until missing frames are received.
- If an acknowledgment for a frame is not received, only that specific frame is retransmitted.
- Once the lowest-numbered frame (base) is acknowledged, the window slides forward.
- The sender can now transmit new frames.
- The process continues until all frames are sent and acknowledged.
- 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.
#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:
- Window Size: The
WINDOW_SIZE
variable determines the number of frames that can be in transit without acknowledgment. - Frame Loss Simulation: Random numbers are used to simulate the loss of frames and acknowledgments.
- Sliding Window: The
base
variable keeps track of the lowest-numbered frame in the window. - Frame Transmission: The program attempts to send frames in the window, and unacknowledged frames are retransmitted until acknowledgment is received.
- Acknowledgment Handling: If an acknowledgment is lost, the corresponding frame is retransmitted.
- 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
Post a Comment