0% found this document useful (0 votes)
37 views22 pages

NC Da2

This document contains an assessment submission by Megha Bhattacharya for their Networking and Communication lab. It includes implementations of various flow control mechanisms in C/C++, including stop-and-wait protocol without timer, stop-and-wait with timer, and sliding window protocol. For each, it provides related theory, algorithms, program code, outputs, and observations. The code was run on an online GDB compiler and screenshots are included.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views22 pages

NC Da2

This document contains an assessment submission by Megha Bhattacharya for their Networking and Communication lab. It includes implementations of various flow control mechanisms in C/C++, including stop-and-wait protocol without timer, stop-and-wait with timer, and sliding window protocol. For each, it provides related theory, algorithms, program code, outputs, and observations. The code was run on an online GDB compiler and screenshots are included.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Winter Semester 2021-22

Course Course
Class Group Course Title Class Id Slot
Code Type
General Network and Embedded
CSE1004 VL2021220502954 L51+L52
(Semester) Communication Lab

Student Details: Student Name: MEGHA BHATTACHARYA


Roll/Reg No: 20BCE0793
Email: [email protected]
Mobile: 6382933694
Faculty Details: Faculty Name: VENKATA PHANIKRISHNA B
School: SCOPE
Email: [email protected]
Assessment No. 2
Assessment Title. Implementation of Flow control
Mechanisms using C/C++
Date of Submission 22-Feb-2022
20BCE0793 MEGHA BHATTACHARYA LAB 02

TABLE OF CONTENTS
Setup used for coding: Online GDB Compiler -
https://www.onlinegdb.com/online_c_compiler

SNO QUESTION PG NO
01 Implementation of Stop 03
and Wait Protocol without
Timer
02 Implementation of Stop 07
and Wait Protocol using
Time ARQ
03 Implementation of Sliding 13
Window Protocol
04 Implementation of bit 17
stuffing and un-stuffing by
taking input from
keyboard

For each of the above –


Related Theory information, Algorithm, Program code,
Output, Observation is provided in the document.
Also for few codes some sites from the internet was
referred. The references are also provided at the end of the
document.
2
20BCE0793 MEGHA BHATTACHARYA LAB 02

01. Implementation of Stop and Wait


Protocol without Timer
Related Description:
This method is the easiest and simplest form of flow control. In this method,
basically message or data is broken down into various multiple frames, and
then receiver indicates its readiness to receive frame of data. When
acknowledgment is received, then only sender will send or transfer the next
frame.
This process is continued until sender transmits EOT (End of Transmission)
frame. In this method, only one of frames can be in transmission at a time. It
leads to inefficiency i.e. less productivity if propagation delay is very much
longer than the transmission delay.
Algorithm:
1. Start the program.
2. Generate a random number that gives total number of frames to be
transmitted.
3. Transmit the first frame.
4. Receive acknowledgement for first frame.
5. Transmit the next frame. Repeat above steps for remaining frames.
6. If acknowledgement is not received then resend that particular frame
only.
7. Stop the program.

Program Code:
#include<stdio.h>
int main()
{
printf ("NAME: MEGHA BHATTACHARYA\n");
printf ("REG NO: 20BCE0793\n");
printf ("SUB: CSE 1004 - NETWORKING AND COMMUNICATION\n");
printf ("LAB ASSESSMENT: 02\n");
printf ("STOP AND WAIT PROTOCOL WITHOUT TIMER IN C\n\n\n\n");

int fs,sent=0,ack,i;

3
20BCE0793 MEGHA BHATTACHARYA LAB 02

printf("Enter the number of frames: \n");


scanf("%d",&fs);
while(1)
{
for(i=0;i<fs;i++)
{
printf("Frame %d has been transmitted.\n",sent);
printf("Acknowledgement %d has been received.\n",sent);
sent++;
if(sent==fs)
break;
}
printf("Please enter the last acknowledgement received:\n");
scanf("%d",&ack);
if(ack>=fs)
break;
else
sent=ack;
}
return 0;
}
Screenshot of code in terminal:

4
20BCE0793 MEGHA BHATTACHARYA LAB 02

Output:

Observation:
 The frame is sent by sender just when affirmation of past frame is
gotten.
 This process of sending a frame and waiting for an affirmation proceeds
as long as the sender has data to send.
 To finish up the transmission sender transmits end of transmission (EOT)
frame.

5
20BCE0793 MEGHA BHATTACHARYA LAB 02

02. Implementation of Stop and Wait


Protocol with Time ARQ
Related Description:
It is same as Stop and Wait Protocol but a timer is used in this case.
 The general strategy of using acknowledgments and timeouts to
implement reliable delivery is sometimes called automatic repeat
request (normally abbreviated ARQ).
 After transmitting one frame, the sender waits for an acknowledgment
before transmitting the next frame. If the acknowledgment does not
arrive after a certain period of time, the sender times out and
retransmits the original frame.
 Suppose the sender sends a frame and the receiver acknowledges it, but
the acknowledgment is either lost or delayed in arriving, the sender
times out and retransmits the original frame.
 In both cases, the sender times out and retransmits the original frame,
but the receiver will think that it is the next frame, since it correctly
received and acknowledged the first frame. This has the potential to
cause duplicate copies of a frame to be delivered.
Algorithm:
The algorithm is almost similar except we need to take care about the timeout
factor also.
1. Start the program.
2. Generate a random number that gives total number of frames to be
transmitted.
3. Transmit the first frame.
4. Receive acknowledgement for first frame.
5. Transmit the next frame. Repeat above steps for remaining frames.
6. If acknowledgement is not received or it is timed out then resend that
particular frame only.
7. Stop the program.

Program Code:
#include <time.h>
#include <cstdlib>
6
20BCE0793 MEGHA BHATTACHARYA LAB 02

#include<ctime>
#include <unistd.h>
#include<iostream>
using namespace std;
class timer {
private:
unsigned long begTime;
public:
void start() {
begTime = clock();
}
unsigned long elapsedTime() {
return ((unsigned long) clock() - begTime) / CLOCKS_PER_SEC;
}
bool isTimeout(unsigned long seconds) {
return seconds >= elapsedTime();
}
};
int main()
{
cout<<"NAME: MEGHA BHATTACHARYA\n";
cout<<"REG NO: 20BCE0793\n";
cout<<"SUB: CSE 1004 - NETWORKING AND COMMUNICATION\n";
cout<<"LAB ASSESSMENT: 02\n";
cout<<"STOP AND WAIT PROTOCOL WITH TIME ARQ\n\n\n";
int frames[] = {1,2,3,4,5};
unsigned long seconds = 5;
srand(time(NULL));
timer t;
cout<<"Sender has to send frames : ";
for(int i=0;i<5;i++)
cout<<frames[i]<<" ";
cout<<endl;
int count = 0;
bool delay = false;
cout<<endl<<"Sender\t\t\t\t\tReceiver"<<endl;
do
{
bool timeout = false;
cout<<"Sending Frame : "<<frames[count];

7
20BCE0793 MEGHA BHATTACHARYA LAB 02

cout.flush();
cout<<"\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)
{
cout<<"Received Frame : "<<frames[count]<<" ";
if(delay)
{
cout<<"Duplicate";
delay = false;
}
cout<<endl;
count++;
}
else
{
cout<<"---"<<endl;
cout<<"Timeout"<<endl;
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 )
{
cout<<"Delayed Ack"<<endl;
count--;
delay = true;
}
else if(!timeout)
cout<<"Acknowledgement : "<<frames[count]-1<<endl;

8
20BCE0793 MEGHA BHATTACHARYA LAB 02

}
}while(count!=5);
return 0;
}

Screenshot of code in terminal:

9
20BCE0793 MEGHA BHATTACHARYA LAB 02

10
20BCE0793 MEGHA BHATTACHARYA LAB 02

Output:

Observation:
It can be seen that the sender send a frame and an acknowledgement is sent
from the receiver side. If the frame is not received after a particular time frame
or if the acknowledgement is not received then the frame is re-sent by the
sender. Due to this a duplicacy occurs in this method. But in this method the
frames do not get lost and it more reliable than the previous method.

11
20BCE0793 MEGHA BHATTACHARYA LAB 02

03. Implementation of Sliding Window


Protocol
Related Description:
This protocol improves the efficiency of stop and wait protocol by allowing
multiple frames to be transmitted before receiving an acknowledgment.
The working principle of this protocol can be described as follows −
• Both the sender and the receiver has finite sized buffers called windows.
The sender and the receiver agrees upon the number of frames to be
sent based upon the buffer size.
• The sender sends multiple frames in a sequence, without waiting for
acknowledgment. When its sending window is filled, it waits for
acknowledgment. On receiving acknowledgment, it advances the
window and transmits the next frames, according to the number of
acknowledgments received.

Algorithm:
1. Start the program.
2. Read the window size.
3. Read the number of frames to be transmitted.
4. Transfer the packet until it reaches the maximum defined size.
5. Resume the window size and repeat the above steps until all frames are
finished.
6. Stop the program.
Program Code:
#include<stdio.h>
int main()
{
printf ("NAME: MEGHA BHATTACHARYA\n");
printf ("REG NO: 20BCE0793\n");
printf ("SUB: CSE 1004 - NETWORKING AND COMMUNICATION\n");
printf ("LAB ASSESSMENT: 02\n");
printf ("SLIDING WINDOW PROTOCOL IN C\n\n\n\n");

12
20BCE0793 MEGHA BHATTACHARYA LAB 02

int w,i,f,fr [50];

printf("Enter window size: ");


scanf("%d",&w);

printf("\nEnter number of frames to transmit: ");


scanf("%d",&f);

printf("\nEnter %d frames: ",f);

for(i=1;i<=f;i++)
scanf("%d",&frames[i]);

printf("\nWith sliding window protocol the frames will be sent in the


following manner (assuming no corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for
acknowledgement sent by the receiver\n\n",w);

for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",fr [i]);
printf("Acknowledgement of above frames sent is received by sender\n\
n");
}
else
printf("%d ",fr [i]);
}

if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\
n");

return 0;
}

Screenshot of code in terminal:

13
20BCE0793 MEGHA BHATTACHARYA LAB 02

Output:

14
20BCE0793 MEGHA BHATTACHARYA LAB 02

Observation:
Both the sender and the receiver has finite sized buffers called windows, in this
case 2 or 3. The sender and the receiver agrees upon the number of frames to
be sent based upon the buffer size. The sender sends multiple frames in a
sequence, without waiting for acknowledgment. When its sending window is
filled, it waits for acknowledgment. After the acknowledgement is received
then it again sends the next lot of frames. This way the Sliding Window
Protocol is executed.

15
20BCE0793 MEGHA BHATTACHARYA LAB 02

04. Implementation of bit stuffing and un-


stuffing by taking input from keyboard
Related Description:
It simply views the frames as a collection of bits. A pattern of bits of arbitrary
length is stuffed in the message to differentiate from the delimiter. This is also
called bit – oriented framing.
Bit stuffing is the process of adding one extra 0 whenever five consecutive 1s
follow a 0 in the data, so that the receiver does not mistake the pattern
0111110 for a flag.
When the receiver sees 5 consecutive 1’s followed by a 0 it automatically
destuffs the 0 bit. This is how bit stuffing and destuffing is done.

Algorithm:
1. Start the program.
2. Read the string from the user.
3. Measure length of the string.
4. Check each bit and count for consecutive 1’s in the bit pattern using
counter.
5. If number of 1’s is 5 then add a 0 after the one. Reset the counter.
6. Repeat this until completion of entire string.
7. Display the bit stuffing result.
8. For destuffing, count the 1’s. If there are five 1’s followed by a 0 then
remove the 0.
9. Repeat this until completion of entire string.
10.Display the bit de-stuffing result.

Program Code:
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100

int main()
{

16
20BCE0793 MEGHA BHATTACHARYA LAB 02

printf ("NAME: MEGHA BHATTACHARYA\n");


printf ("REG NO: 20BCE0793\n");
printf ("SUB: CSE 1004 - NETWORKING AND COMMUNICATION\n");
printf ("LAB ASSESSMENT: 02\n");
printf ("BIT STUFFING AND DESTUFFING IN C\n\n\n\n");

char *p,*q;
char temp;
char in[MAXSIZE];
char stuff[MAXSIZE];
char destuff[MAXSIZE];

int count=0;

printf("Enter the input string (0's & 1's only):\n");


scanf("%s",in);

p=in;
q=stuff;

while(*p!='\0')
{
if(*p=='0')
{
*q=*p;
q++;
p++;
}
else
{
while(*p=='1' && count!=5)
{
count++;
*q=*p;
q++;
p++;
}

if(count==5)
{

17
20BCE0793 MEGHA BHATTACHARYA LAB 02

*q='0';
q++;
}
count=0;
}
}
*q='\0';
printf("\nThe string after bit stuffing is");
printf("\n%s",stuff);

p=stuff;
q=destuff;
while(*p!='\0')
{
if(*p=='0')
{
*q=*p;
q++;
p++;
}
else
{
while(*p=='1' && count!=5)
{
count++;
*q=*p;
q++;
p++;
}
if(count==5)
{
p++;
}
count=0;
}
}
*q='\0';
printf("\nThe string after bit destuffing is");
printf("\n%s\n",destuff);
return 0;

18
20BCE0793 MEGHA BHATTACHARYA LAB 02

}
Screenshot of code in terminal:

19
20BCE0793 MEGHA BHATTACHARYA LAB 02

Output:

20
20BCE0793 MEGHA BHATTACHARYA LAB 02

Observation:
Bit stuffing considers the string bitwise i.e., single characters. It is the process
of adding one extra 0 whenever five consecutive 1s follow a 0 in the data, so
that the receiver does not mistake the pattern for a flag. When the receiver
sees 5 consecutive 1’s followed by a 0 it automatically de-stuffs the 0 bit. This
is how bit stuffing and bit de-stuffing is done. This avoids confusion between
the payload and the flag. It helps to avoid framing error while transmitting
frames.

PTO 

21
20BCE0793 MEGHA BHATTACHARYA LAB 02

REFERENCES:
The following site was referred to understand and devise the code for Stop
and Wait Protocol with Timer ARQ:

_____________________________________________
THE END
THANK YOU
_____________________________________________

DONE BY:

Student Details:
Student Name: MEGHA BHATTACHARYA
Roll/Reg No: 20BCE0793
Email: [email protected]
Mobile: 6382933694

22

You might also like