Skip to content

Instantly share code, notes, and snippets.

@sks147
Last active September 2, 2016 04:55
Show Gist options
  • Save sks147/1cf94201ca89f073e21059110dbd928e to your computer and use it in GitHub Desktop.
Save sks147/1cf94201ca89f073e21059110dbd928e to your computer and use it in GitHub Desktop.
//Simulating selective repeat sliding window protocol
#include <iostream>
#include <cstdlib>
using namespace std;
void receiver(int r);
int sent_packets=0;
int received_packets=0;
bool ack;
void sender()
{
int buffer[10];
for(int i=0;i<10;i++)
{
buffer[i]=sent_packets+1;
sent_packets++;
cout<<"sending packet "<<buffer[i]<<endl;
receiver(buffer[i]);
while(!ack)
{
cout<<"resending packet "<<buffer[i]<<endl;
receiver(buffer[i]);
}
}
if(sent_packets<200)
sender();
}
void receiver(int r)
{
int num=rand()%50;
if(num>15)
{
cout<<"Received packet "<<r<<endl;
received_packets++;
ack=true;
}
else
{
ack=false;
cout<<"error"<<endl;
}
}
int main() {
sender();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment