Simulate and implement stop and wait protocol for noisy channel in Computer Network -KML Tutorial

 Aim:- Simulate and implement stop and wait protocol for noisy channel.

Program:-

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. int main(){
  5.     int n,i=0,sn=0,rn=0,proba=0;
  6.     srand(time(0));
  7.     printf("Enter the number of frames:");
  8.     scanf("%d",&n);
  9.     while(i<n){
  10.         sn=i%2;
  11.         printf("Sender: sending frame no. %d with sequence number %d\n",i,sn);
  12.         proba=rand()%100;
  13.         if(proba>80){
  14.             printf("****Frame is corrupted****\n");
  15.         }else{
  16.             if(sn==rn){
  17.                 rn=(rn+1)%2;
  18.                 printf("Receiver: sending acknowledgment No. %d\n",rn);
  19.                 proba=rand()%100;
  20.                 if(proba>80){
  21.                     printf("****Acknowledgment frame corrupted****\n");
  22.                 }else{
  23.                     i++;
  24.                 }
  25.             }else{
  26.                 printf("Receiver: sending acknowledgment No. %d\n",rn);
  27.                 proba=rand()%100;
  28.                 if(proba>80){
  29.                     printf("**Acknowledgment frame corrupted**\n");
  30.                 }else{
  31.                     i++;
  32.                 }
  33.             }
  34.         }
  35.     }
  36.     return 0;
  37. }

Outpute:-

Enter the number of frames:5
Sender: sending frame no. 0 with sequence number 0
Receiver: sending acknowledgment No. 1
Sender: sending frame no. 1 with sequence number 1
****Frame is corrupted****
Sender: sending frame no. 1 with sequence number 1
Receiver: sending acknowledgment No. 0
Sender: sending frame no. 2 with sequence number 0
Receiver: sending acknowledgment No. 1
Sender: sending frame no. 3 with sequence number 1
Receiver: sending acknowledgment No. 0
Sender: sending frame no. 4 with sequence number 0
Receiver: sending acknowledgment No. 1

Comments