Stop-and-Wait Protocol

Description

Stop-and-Wait is a simple flow control protocol used in data communications to ensure reliable transmission of data over a network. It operates by sending a single data frame at a time and waiting for an acknowledgment (ACK) from the receiver before sending the next frame. This protocol helps in preventing congestion and ensures data integrity.

C++ Code (without comments)

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <unistd.h>

using namespace std;

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. Resending..." << endl;
        return false;
    }
}

int main() {
    int frameNumber = 1;

    while (frameNumber <= 5) {
        bool ack = sendData(frameNumber);

        if (ack) {
            frameNumber++;
        }

        sleep(TIMEOUT);
    }

    cout << "Data transmission completed." << endl;
    return 0;
}

Time and Space Complexity

Operation Time Complexity Space Complexity
Stop-and-Wait Protocol O(n) O(1)

Where:

  • n: Number of frames to be transmitted.

← Back to Homepage