Translate

Monday, October 8, 2012

ME NETWORKS LAB-->SIMULATION OF SLIDING WINDOW PROTOCOL


SIMULATION OF SLIDING WINDOW PROTOCOL

 #include<stdio.h>

#include<stdlib.h>

#include<math.h>

int n,r;

struct frame

{

char ack;

int data;

}frm[10];

int sender(void);

void recvfrm(void);

void resend(void);

void resend1(void);

void goback(void);

void selective(void);

int main()

{

int c;

do

{

printf("\n\n1.Selective repeat ARQ\n2.Goback ARQ\n3.exit");

printf("\nEnter ur choice:");

scanf("%d",&c);

switch(c)

{

case 1:

selective();

break;

case 2:

goback();

break;

case 3:

exit(0);

break;

}

}while(c!=4);

}

 

 

 

void goback()

{

sender();

recvfrm();

resend1();

printf("\n all packets sent successfully\n");

}

 

void selective()

{

sender();

recvfrm();

resend();

printf("\nAll packets sent successfully");

}

 

int sender()

{

int i;

printf("\nEnter the no. of packets to be sent:");

scanf("%d",&n);

for(i=0;i<n;i++)

{

printf("\nEnter data for packets[%d]",i);

scanf("%d",&frm[i].data);

frm[i].ack='y';

}

return 0;

}

 

void recvfrm()

{

int i;

rand();

r=rand()%n;

frm[r].ack='n';

for(i=0;i<n;i++)

{

if(frm[i].ack=='n')

printf("\nThe packet number %d is not received\n",r);

}

}

 

void resend()

{

printf("\nresending packet %d",r);

sleep(2);

frm[r].ack='y';

printf("\nThe received packet is %d",frm[r].data);

}

 

void resend1()

{

int i;

printf("\n resending from packet %d",r);

for(i=r;i<n;i++)

{

sleep(2);

frm[i].ack='y';

printf("\nReceived data of parent %d is %d",i,frm[i].data);

}

}

 

OUTPUT:

 

root@localhost ~]# vi slide.c

[root@localhost ~]# cc slide.c

[root@localhost ~]# ./a.out  

 

1.Selective repeat ARQ

2.Goback ARQ         

3.exit               

Enter ur choice:1    

 

Enter the no. of packets to be sent:2

 

Enter data for packets[0]12

 

Enter data for packets[1]45

 

The packet number 0 is not received

 

resending packet 0

The received packet is 12

All packets sent successfully

 

1.Selective repeat ARQ

2.Goback ARQ

3.exit

Enter ur choice:2

 

Enter the no. of packets to be sent:2

 

Enter data for packets[0]24

 

Enter data for packets[1]34

 

The packet number 1 is not received

 

 resending from packet 1

Received data of parent 1 is 34

 all packets sent successfully

 

1.Selective repeat ARQ

2.Goback ARQ

3.exit

Enter ur choice:3

No comments:

Post a Comment