Selective Repeat Protocol
Description
Selective Repeat is a sliding window protocol used in network communications to achieve reliable data transmission over an unreliable network. Unlike Go-Back-N, Selective Repeat allows the receiver to individually acknowledge correctly received frames, and the sender retransmits only those frames for which it does not receive an acknowledgment within a specified timeout period.
C++ Code
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
using namespace std;
const int WINDOW_SIZE = 4;
const int TIMEOUT = 3;
bool sendData(int frameNumber) {
srand(time(0));
bool ackReceived = (rand() % 10 < 8);
if (ackReceived) {
cout << "Frame " << frameNumber << " sent successfully." << endl;
return true;
} else {
cout << "Frame " << frameNumber << " not acknowledged. Retransmitting only this frame..." << endl;
return false;
}
}
int main() {
int base = 1;
int nextSeqNum = 1;
while (base <= 5) {
while (nextSeqNum < base + WINDOW_SIZE && nextSeqNum <= 5) {
bool ack = sendData(nextSeqNum);
if (ack) {
nextSeqNum++;
}
}
sleep(TIMEOUT);
}
cout << "Data transmission completed." << endl;
return 0;
}
Time and Space Complexity
Operation | Time Complexity | Space Complexity |
---|---|---|
Selective Repeat Protocol | O(n) | O(1) |
Where:
- n: Number of frames to be transmitted.