Professional Documents
Culture Documents
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<unistd.h>
CODE:
#Include<stdio.h>
#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)
}
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;
printf("the sender has to send the frames: ");
for(int i=0;i<10;i++)
cout<<frames[i]<<" ";
cout<<endl;
int count = 0;
bool delay = false;
printf("\nSENDER\t\t\t\tRECEIVER\n");
do
{
bool timeout = false;
printf("sending frame: ",frames[count]);
cout.flush();
printf("\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)
{
printf("received frame : ", frames[count]," ");
if(delay)
{
printf("duplicate (frame)");
delay = false;
}
printf("\n\n");
count++;
}
else
{
printf("-------\n");
printf("timeout !!\n");
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 )
{
printf("delayed acknowledgement\n");
count--;
delay = true;
}
else if(!timeout)
printf("acknowledgement: ", frames[count]-1, "\n ");
}
}while(count!=10);
return 0;
}
OUTPUT:
3. C Program to implement Selective Repeat ARQ
CODE:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int N;
return 1;
if(rand()%2==0)
return -1;
else
return receiver_side(frame);
int sender_side()
int n,m;
scanf("%d",&n);
scanf("%d",&m);
N=pow(2,m);
int ack[sw];
for(i=0;i<n;){
int min=sw<n-i?sw:n-i;
for(j=i;j<i+min;j++)
printf("frame %d sent\n",j);
ack[sn++]=pipe(j);
if(ack[sn-1]<0)
continue;
sn=sn%sw;
sf++;
sf%=N;
int k=sw;
for(j=i;j<i+min;j++)
receiver_side(j);
sf++;
sf%=N;
i=j;
printf("\n");
int main()
printf("Satvika 20CI0289\n");
sender_side();
}
OUTPUT: