Você está na página 1de 4

Ex No: 7 SIMULATION OF SLIDING WINDOW PROTOCOL

AIM:
To simulate sliding window protocol.

DESCRIPTION:

In the sliding window method of flow control, the sender can transmit several frames before
needing an acknowledgement. The receiver acknowledges only some of the frames, using a single
ACK to confirm the receipt of multiple data frames.

The sliding window refers to imaginary boxes at both sender and the receiver. To keep track
of which frames have been received and which have been transmitted, sliding window introduces
an identification scheme based on window size. The frames are numbered modulo-n ie. from 0 to n-
1.when the receiver sends an ACK,it includes the number of the next frame it expects to receive. A
maximum of n-1 frames can be sent before an acknowledgment is required.

SENDER WINDOW:

At the beginning of the transmission, the sender window contains n-1 frames. The sliding window
of the sender shrinks from the left when frames of data are sent. The sliding window of the sender
expands to the right when acknowledgements are received. Once an ACK arrives, the window
expands to allow in a number of new frames equal to the number of frames acknowledged by that
ACK.

RECIEVER WINDOW:

At the beginning the receiver window contains not n-1 frames but n-1 space for the frames. As new
frames come in, the size of the receiver window shrinks. The receiver window represents not the
number of frames received but the number of frames that may still be received before an ACK must
be sent. The sliding window of the receiver shrinks from the left when frames of data are received
and expands to right when acknowledgment is sent. The window expands to include a number of
new frame spaces equal to the number of the most recently acknowledged frame.

ALGORITHM:
1. Get the number of packets (n) to be sent and the window size.
2. Generate the bandwidth for data transfer using (rand ( ) %( 2*window size)).
3. Initialize sender with n number of 3s to indicate that packets are not sent.
4. Initialize receiver with n number of 1s to indicate that packets are not received.
5. If bandwidth<=window size, then the sender must send (bandwidth) number of packets to the
receiver at a time.
6. Else, make bandwidth=window size and then send (bandwidth) number of packets to the
receiver.
7. Indicate the sent packets by 4 on the sender side and received packets by 2 on the receiver side.
8. After the packets are received by the receiver, acknowledgement is sent to the sender which is
indicated by 5 on the sender side.
9. Repeat the steps 5 to 8 until all the packets are sent, received and acknowledged successfully.
10.Stop

Source Code:-
#include<stdlib.h>
#include<iostream.h>
#include<unistd.h>
char send[15],recv[15];
void initialise(int a)
{
for(int i=0;i<a;i++)
{
send[i]='3';
recv[i]='1';
}
}
void sender(int m,int n)
{
for(int i=0;i<n;i++)
{
if(send[i]=='4')
send[i]='5';
if(send[i]=='3')
{
for(int j=0;j<m;j++)
{
send[i]='4';
i++;
}
break;
}
}
send[n]='\0';
}
void receiver(int a,int b)
{
for(int i=0;i<b;i++)
{
if(recv[i]=='1')
{
for(int j=0;j<a;j++)
{
recv[i]='2';
i++;
}
break;
}
}
recv[b]='\0';
}
void choose(int x,int y,int z)
{
switch(x)
{
case 0:
sender(x,z);
break;
default:
if(x>y)
x=y;
sender(x,z);
receiver(x,z);
break;
}
}
main()
{
int i,band_w,empty,recd,nottrans,trans,ack,wsize,pack,pack_trans=0;
cout<<"\nEnter the window size:";
cin>>wsize;
cout<<"\nEnter the number of packets:";
cin>>pack;
initialise(pack);
cout<<"\nThe initial sender is:"<<send;
cout<<"\nThe initial receiver is:"<<recv;
do
{
band_w=rand()%(2*wsize);
cout<<"\nBandwidth::"<<band_w;
choose(band_w,wsize,pack);
pack_trans+=band_w;
cout<<"\nSender is:: "<<send;
cout<<"\nReceiver is:: "<<recv;
}while(send[pack-1]!='5');
}

OUTPUT:
TOTAL NO OF PACKETS?? 6
WINDOW SIZE??2
SENDER: 3 3 3 3 3 3
RECEIVER: 1 1 1 1 1 1
BANDWIDTH: 2
SENDER: 4 4 3 3 3 3
RECEIVER: 2 2 1 1 1 1
BANDWIDTH: 2
SENDER: 5 5 4 4 3 3
RECEIVER: 2 2 2 2 1 1
BANDWIDTH: 2
SENDER: 5 5 5 5 4 4
RECEIVER: 2 2 2 2 2 2
FINALLY:
SENDER: 5 5 5 5 5 5
RECEIVER: 2 2 2 2 2 2
RESULT:

Thus a program to implement sliding window protocol has been performed successfully.

Você também pode gostar