C++ Program To Implement Stop and Wait Flow Control Protocol

In this strategy for flow control, the sender sends a single frame to receiver and waits for an affirmation. • The following frame is sent by sender just when affirmation of past frame is gotten. • This process of sending a frame and waiting for an affirmation proceeds as long as the sender has data to send. • To finish up the transmission sender transmits end of transmission (EOT) frame. • The fundamental favorable position of stop and wait protocols is its precision. Next frame is transmitted just when the principal frame is recognized. So there is zero chance of frame being lost. • The fundamental drawback of this technique is that it is wasteful. It makes the transmission process moderate.

C++ program to Implement Stop and Wait Flow Control Protocol
#include<iostream>
#include <time.h>
#include <cstdlib>
#include<ctime>
#include <unistd.h>
using namespace std;
class timer {
   private:
   unsigned long begTime;
   public:
   void start() {
   begTime = clock();
   }
unsigned long elapsedTime() {
   return ((unsigned long) clock() - begTime) / CLOCKS_PER_SEC;
  }
  bool isTimeout(unsigned long seconds) {
   return seconds >= elapsedTime();
   }
};
int main()
{
int frames[] = {1,2,3,4,5,6,7,8,9,10};
unsigned long seconds = 5;
srand(time(NULL));
timer t;
cout<<"Sender has to send frames : ";
for(int i=0;i<10;i++)
    cout<<frames[i]<<" ";
cout<<endl;
int count = 0;
bool delay = false;
cout<<endl<<"Sender\t\t\t\t\tReceiver"<<endl;
do
{
    bool timeout = false;
    cout<<"Sending Frame : "<<frames[count];
    cout.flush();
    cout<<"\t\t";
    t.start();
    if(rand()%2)
    {
        int to = 24600 + rand()%(64000 - 24600)  + 1;
        for(int i=0;i<64000;i++)
            for(int j=0;j<to;j++) {}
    }
    if(t.elapsedTime() <= seconds)
    {
        cout<<"Received Frame : "<<frames[count]<<" ";
        if(delay)
        {
            cout<<"Duplicate";
            delay = false;
        }
        cout<<endl;
        count++;
    }
    else
    {
        cout<<"---"<<endl;
        cout<<"Timeout"<<endl;
        timeout = true;
    }
    t.start();
    if(rand()%2 || !timeout)
    {
        int to = 24600 + rand()%(64000 - 24600)  + 1;
        for(int i=0;i<64000;i++)
            for(int j=0;j<to;j++) {}
        if(t.elapsedTime() > seconds )
        {
            cout<<"Delayed Ack"<<endl;
            count--;
            delay = true;
        }
        else if(!timeout)
            cout<<"Acknowledgement : "<<frames[count]-1<<endl;
    }
}while(count!=10);
return 0;
}

OUTPUT

Sender has to send frames : 1 2 3 4 5 6 7 8 9 10


Sender    Receiver
Sending Frame : 1    Received Frame : 1
Acknowledgement : 1
Sending Frame : 2    ---
Timeout
Sending Frame : 2    Received Frame : 2
Acknowledgement : 2
Sending Frame : 3    Received Frame : 3
Acknowledgement : 3
Sending Frame : 4    Received Frame : 4
Acknowledgement : 4
Sending Frame : 5    Received Frame : 5
Acknowledgement : 5
Sending Frame : 6    Received Frame : 6
Acknowledgement : 6
Sending Frame : 7    Received Frame : 7
Delayed Ack
Sending Frame : 7    Received Frame : 7 Duplicate
Delayed Ack
Sending Frame : 7    ---
Timeout
Sending Frame : 7    Received Frame : 7 Duplicate
Acknowledgement : 7
Sending Frame : 8    ---
Timeout
Delayed Ack
Sending Frame : 7    Received Frame : 7
Acknowledgement : 7
Sending Frame : 8    Received Frame : 8
Acknowledgement : 8
Sending Frame : 9    Received Frame : 9
Delayed Ack
Sending Frame : 9    ---
Timeout
Sending Frame : 9    Received Frame : 9 Duplicate
Delayed Ack
Sending Frame : 9    Received Frame : 9 Duplicate
Acknowledgement : 9
Sending Frame : 10    ---
Timeout
Sending Frame : 10    Received Frame : 10
Acknowledgement : 10



You must be also searching for these programming languages :


tags : C++ ,programs of C++ , C++ programs , C++ tutorials , tutorialspoint, stop and flow control ,learn C++.computer networks, learn programming. packets.

5 Comments

  1. Hello, I do not understand the body of the if code (rand ()% 2), can you explain why that is not? Thanks.

    ReplyDelete
    Replies
    1. that rand()%2 part is to delay the program so that the next line if(t.elapsedTime() <= seconds)will success or fail depend upon rand()%2 = {0,1}. Its like a simulation for time out or not(frame transporation time within the fixed time limit that is 5secs). So if rand()%2 == 0 then this(t.elapsedTime() <= seconds)will work else not. It's just for a clean presentation, in this example.

      Delete
  2. GB Instagram APK Download for free with new interface. get new instagram update for free. Gb Instagram app is an amazing and top-rated mod app of official Instagram. GB insta has much more exciting, and additional key tools are included

    ReplyDelete
  3. Please explain this part
    {
    int to = 24600 + rand() % (64000 - 24600) + 1;
    for (int i = 0; i < 64000; i++)
    for (int j = 0; j < to; j++)
    {
    }
    }

    ReplyDelete
Previous Post Next Post