NC Da2
NC Da2
Course Course
Class Group Course Title Class Id Slot
Code Type
General Network and Embedded
CSE1004 VL2021220502954 L51+L52
(Semester) Communication Lab
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
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
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
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;
}
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
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
for(i=1;i<=f;i++)
scanf("%d",&frames[i]);
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;
}
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
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
char *p,*q;
char temp;
char in[MAXSIZE];
char stuff[MAXSIZE];
char destuff[MAXSIZE];
int count=0;
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