You are on page 1of 6

Experiment No:2 Implementation of Sliding window protocol

and stop and wait protocol

AIM

To implement Sliding window protocol and Stop and Wait protocol


ALGORITHM:
Sliding window protocol
Step l: start the program.
Step 2: Open the input file in read mode.
Step 3: Read the size ofthe window
Step 4: Select randomly the number of packets is to be transferred.
Step 5: Read the content of theinput file.
Step 6:Transfer the packet until it reaches the maximum defined size.
Step 7: Resume the window size and repeat the above two steps until packets in.
Step 8: Close the file.
Step 9: Stop the program.
ALGORITHM:
Stop and wait protocol
Step 1: Sender sends a data packet to the receiver.
Step 2: Sender stops and waits for the
receiver. acknowledgement for the sent packet from the
Step 3: Receiver receives and processes the data
Step 4: Receiver sends an packet.
Step 5: After receiving the acknowledgement to the sender.
receiver. acknowledgement, sender sends the next data packet to the
PROGRAM:
Sliding window
main()
int i.m.n.j.w,1:char c:
FILE*#:
f-fopen( "text.txt""r");
printf("window size"):
scanf("ºod".&n):
m=n: while(! fof()

i-rand()%n+1:j-i;
1=i:
if(m>i)

m=m-i:i(m>0)

printf(" n"): while(i>0 & !feof(f)


C=gete(f): printf("%c".c);i--;

printf(" nod transferred"j);if(j>3)


printf(" n 1acknowledgement received" );else
printf "n acknowledgement received"j+1);

m=mtj-l;:

OUTPUT:
ENTER THE WINDOWS SIZE: 10

SENDER WINDOW IS EXPANDED TOSTORE MESSAGE OR WINDOW


ENTER THE DATA TO BE SENT: ForgetCode.com
MESSAGE SEND BY THE SENDER:
ForgetCode.com
WINDOW SIZE OF RECEIVER IS EXPANDED

ACKNOWLEDGEMENT FROM RECEIVER

ACK:5
MESSAGE RECEIVED BY RECEIVER IS : Forget Code
WINDOW SIZE OF RECEIVER IS SHRINKED
PROGRAM:
protocol
Stop and wait
#include <conio.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h> #define
TIMEOUT 5
#define MAX SEQ1
#define TOT PACKETS 8
k=0;typedef struct
#define inc(k) if(k<MAX SEQ) k++; else
int data:
packet: typedef struct
int kind;int seq; int
ack:
packet info;int err;
frame: frame DATA;
typedef enum {frame arrival,err,timeout,no_event} event type;void
from network layer(packet *);
void to network layer(packet *); void
to physical layer(frame *); void
from_ physical_layer(frame *);
void wait for event
wait for event sender(event type *); void
void sender(); reciever(event type *);void reciever);
int i-1:
char turn: I/Data to be sent by sender
int DISCONNECT-0; lIr,s

*/void main()
clrscr(); randomize();
whi le(!DISCONNECT)
sender( ): delay(400);
reciever();
getch();
*/void sender()
static int frame to send=0;static frame s:
packet buffer; event type event;
static int flag-0;

if(flag--0)

from network layer(&buffer);s. info = buffer:


S.seq = frame to send:
printf("SENDER: Info =%d Seq No = %d ",s.info,s. seq);
turn ='r':
to physicallayer( &s);flag = 1;
wait for_event_sender(&event);if(turn==s))

if(event==frame arrival)
from network_layer(&buffer);inc(frame_ to _send);
s.info = buffer;
S.seq = frame to send:;
printf("SENDER: Info = %d Seq No =%d",s.info,s.seq);turn ='r;
to_physical _layer( &s):
if(event==timeout)
printf("SENDER:Resending Frame");turn ='r;
to_physical_layer(&s);

*/void reciever()

static int frame expected-0;frame r,s;


event type event;

reciever(&event);if(turn==r)
wait for event
iflevent=-frame arrival)
if(r.seq==frame_expected)
from physicallayer(&r);
laver&r,info);inc(trame expected):
to netwok
else Acknowledgement Resentln"):
print"RECIEVER :
s:ophysical layer(&s):

itËeventerr)
"RECIEVER: Garbled Framen"):turn ='s': /it
print
frame not reeieved
l/sender shold send it again

void from network layer(packet *buffer)


(*buffer).data = i;itt;

void to physical layer( frame *s)


//0 means error
->err = random(4): //non zero means no errorDATA = *s;
llprobability of error=1/4

void to network layer(packet *buffer)


printf "RECIEVER :Packet %d recieved, Ack Sentln",
ifi>TOT PACKETS) (*buffer).data):
Iif all packets recieved then disconnect
DISCONNECT =1;
printf(" nDISCONNECTED"):

void fro physical layer(frame *buffer)


*buffer =DATA:
void wait for event _sender(event type * e)
static int timer=0:

if( turn=='s')

timert +; it(timer==TIMEOUT)

*e= timeout:
printt"SENDER: Ack not recieved=> TIMEOUTn"):timer = 0;
return:

if( DATA.err==0)
*e= err:else

timer = 0:
*e = frame arrival:

type * e)
void wait for event reciever(event
if(turn==r)

if(DATA.err=-0)
*e= err:else
*e= frame arrival;

You might also like