Stop and Wait protocol is a data link layer protocol for transmission of data over noiseless channels. It is a flow control protocol used in noiseless channels. In this article we would write a C program to implement this protocol.
You can use any code editor you want to run this program. However, using either Codeblocks or VS Code is recommended. To learn how to setup VScode to compile and run C/C++ code, click here.
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;
}
The github repository for the C implementation of this protocol along with other networking and TCP/IP protocol suite protocols, click here.
Happy Coding.