You are on page 1of 16

Network and Communication - CSE1004

Digital Lab Assignment 3

Slot: L15+L16 Reg. No.: 20BCI0289

1. C Program to implement Go Back N ARQ

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<unistd.h>

bool arrival_msg = false;


int i,num,m,window_size,N=1; //window size is N here
int rn=0;
void receiver_side();
void frame_resend(int,int);
void sender_side()
{
int sf=0;
int sn=0;
while(num!=0)
{
int j=0;
printf("\n");
while(j < window_size)
{
printf("sender's frame %d has been sent\n",sn);
sn++;
sn=sn%N;
num--;
j++;
if(num==0)
{
break;
}
sleep(1);
}
while((sn-sf)!=0)
{
receiver_side();
if(arrival_msg)
{
printf("sender: the acknowledgement %d has been received\n",rn);
arrival_msg=false;
sf++;
sf=sf%N;
}
else{
frame_resend(sn,sf);
}
}
}
}
void frame_resend(int sn,int sf)
{
i=sf;
while((sn-i)!=0)
{
printf("frame %d has been lost\nresending...\n",i);
printf("sender: resend frame %d\n",i);
i++;
i=i%N;
sleep(1);
}
}
void receiver_side()
{
int seqno;
printf("enter the sequence no of received frame: ");
scanf("%d",&seqno);
if(seqno==rn)
{
printf("receiver: frame %d has been received\n",rn);
rn++;
rn=rn%N;
arrival_msg=true;
sleep(1);
}
else{
arrival_msg=false;
}
}
int main()
{
printf("Satvika Mahapatra 20BCI0289\n\n");
printf("enter the number of frames to be sent: ");
scanf("%d",&num);
printf("please input m value: ");
scanf("%d",&m);
while(m!=0)
{
N=N*2;
m--;
}
window_size=N-1;
printf("window size: %d\n",window_size);
sender_side();
}
OUTPUT:
2. C Program to implement Stop and Wait ARQ

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;

int receiver_side(int frame)

static int rs=0;


rs=(rs+1)%N;

printf("frame %d received! \nacknowledgment for frame %d\n",frame,rs);

return 1;

int pipe(int frame)

if(rand()%2==0)

return -1;

else

return receiver_side(frame);

int sender_side()

int n,m;

printf("enter the number of frames: ");

scanf("%d",&n);

printf("enter the number of bits: ");

scanf("%d",&m);

N=pow(2,m);

int sw=N/2, i,j,sn=0,sf=0;

int ack[sw];

memset( ack, 0, sw*sizeof(int));

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)

printf("negative acknowledgment. Sorry frame lost!\n\n");


sn=sn%sw;

continue;

sn=sn%sw;

sf++;

sf%=N;

int k=sw;

for(j=i;j<i+min;j++)

if(ack[sw - k--] < 0)

printf("frame %d lost \nresending...\n",j);

receiver_side(j);

sf++;

sf%=N;

i=j;

printf("\n");

int main()

printf("Satvika 20CI0289\n");

sender_side();

}
OUTPUT:

You might also like