C Program to implement Stop and Wait (With github link and steps)

Approach in Stop and Wait:

Sender’s Side

  • Send Frame.
  • Wait For acknowledgement.
  • If acknowledgement is received, send next frame.

Receiver’s Side

If correct frame in order is received, send acknowledgement.

Code

#include <stdio.h>
#include <stdbool.h>

// Receiver Function
void receiver(int frame) {
    printf("Receiver: Received frame %d\n", frame);
}

int main() {
    int totalFrames;
    printf("Enter total frames to send.\n");
    scanf("%d", &totalFrames);

    int frameToSend = 1;
    int ack;

    while (frameToSend <= totalFrames) {
        printf("Sending frame %d\n", frameToSend);
        printf("Enter acknowledgment for frame %d (1 for received): ", frameToSend);
        scanf("%d", &ack);

        if (ack == 1) {
            receiver(frameToSend);
            frameToSend++;
        }
    }

    return 0;
}

Happy Coding.

Leave a Comment

Your email address will not be published. Required fields are marked *