Professional Documents
Culture Documents
Theory:
Byte – Stuffing − A byte is stuffed in the message to differentiate from the delimiter. This is
also called character-oriented framing.
Bit – Stuffing − A pattern of bits of arbitrary length is stuffed in the message to differentiate
from the delimiter. This is also called bit – oriented framing.
Algo:
1) Start.
4) Check each bit and count for consecutive 1’s in the bit pattern using
as a flags.
9) Stop.
Code:
Byte Stuffing:
#include<stdio.h>
#include<string.h>
int main(){
char frame[50][50],str[50][50];
char flag[10];
strcpy(flag,"flag");
char esc[10];
strcpy(esc,"esc");
int i,j,k=0,n;
strcpy(frame[k++],"flag");
scanf("%d",&n);
for(i=0;i<=n;i++)
gets(str[i]);
for(i=0;i<=n;i++)
puts(str[i]);
printf("\n");
for(i=1;i<=n;i++)
strcpy(frame[k++],str[i]);
else
strcpy(frame[k++],"esc");
strcpy(frame[k++],str[i]); }
strcpy(frame[k++],"flag");
for(i=0;i<k;i++)
printf("%s\t",frame[i]);
return 0;
Bit Stuffing:
#include <bits/stdc++.h>
#include <iostream>
int brr[30];
int i, j, k;
i = 0;
j = 0;
int count = 1;
while (i < N)
if (arr[i] == 1)
brr[j] = arr[i];
k++)
j++;
brr[j] = arr[k];
count++;
if (count == 5)
j++;
brr[j] = 0;
i = k;
}
else
brr[j] = arr[i];
i++;
j++;
int main()
int N = 6;
int arr[] = { 1, 1, 1, 1, 1, 1 };
bitStuffing(N, arr);;
return 0;
Output:
Byte Stuffing:
Bit Stuffing:
Discussion:
Writing efficient code for the required programs while maintaining readability and good logic
for the code.
We have learnt what framing is, how framing works and the common approaches used to
tackle problems in framing: character-oriented protocols, where we learnt about byte
stuffing and bit oriented protocols, where we learnt about bit stuffing.
Theory:
A cyclic redundancy check (CRC) is an error-detecting code commonly used in digital networks
and storage devices to detect accidental changes to raw data. Blocks of data entering these
systems get a short check value attached, based on the remainder of a polynomial division of
their contents. On retrieval, the calculation is repeated and, in the event the check values do
not match, corrective action can be taken against data corruption. CRCs can be used for error
correction.
Algo:
1. 2) The newly formed data unit i.e., original data + string of n as are divided by the
divisor using binary division and remainder is obtained. This remainder is called CRC.
3) Now, string of n 0’s appended to data unit is replaced by the CRC remainder (which is
also of n bit).
5) The receiver on receiving it divides data unit + CRC by the same divisor & checks
the remainder.
6) If the remainder of division is zero, receiver assumes that there is no error in data
7) If remainder is non-zero then there is an error in data and receiver rejects it.
Code:
#include <iostream>
#include <conio.h>
int main()
{
int i, j, k, l;
//Get Frame
int fs;
if (fs > 8)
return 0;
int f[20];
//Get Generator
int gs;
int g[20];
{
cin >> g[i];
//Append 0’s
int rs = gs – 1;
cout << “\n Number of 0’s to be appended: ” <<
f[i] = 0;
int temp[20];
temp[i] = f[i];
//Division
j = 0;
k = i;
0)) {
temp[k] = 0;
else
{
temp[k] = 1;
//CRC
int crc[15];
crc[i] = temp[j];
int tf[15];
for (i = 0; i < fs; i++)
tf[i] = f[i];
tf[i] = crc[j];
temp[i] = tf[i];
//Division
j = 0;
k = i;
if (temp[k] >= g[j])
0)) {
temp[k] = 0;
else
temp[k] = 1;
int rrem[15];
for (i = fs, j = 0; i < fs + rs; i++, j++)
rrem[j] = temp[i];
int flag = 0;
if (rrem[i] != 0)
flag = 1;
if (flag == 0)
Correct”; }
else
Error”; }
getch();
}
Output:
Discussion:
Developing the various functions required for our task was challenging and required lots of
further study to be able to implement them efficiently and tweak the functionality of the
functions to meet the specific needs of the CRC program.
We have learnt that CRC is mainly designed and used to protect against common errors on
communication channels and is not suitable for protection against intentional alteration of
data.
Theory:
Stop and wait protocol is the simplest flow control method. In this, the sender will
transmit one frame at a time to the receiver. The sender will stop and wait for the
acknowledgement from the receiver.
Algo:
Step 2: Generate a random that gives the total number of frames to be transmitted.
Step 7: If an acknowledgement is not received for a particular frame retransmit that frame
alone again.
Step 8: Repeat the steps 5 to 7 till the number of remaining frames to be send becomes
zero.
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
int i,j,noframes,x,x1=10,x2;
clrscr();
for(i=0;i<200;i++)
rand();
noframes=rand()/200;
i=1;
j=1;
noframes = noframes / 8;
while(noframes>0)
srand(x1++);
x = rand()%10;
if(x%2 == 0)
sleep(x2);
}
srand(x1++);
x = rand()%10;
noframes-=1;
i++;
j++;
Output:
No of frames is 6
Sending frame 1
Sending frame 2
Sending frame 3
Sending frame 4
Sending frame 5
Retransmitting frame 5
Acknowledgement for frame 5
Sending frame 6
Sending frame 6
Discussion:
• Stop and Wait ARQ executes Sliding Window Protocol with Window Size
1. For noisy link, pure stop and wait protocol will break down, and solution is to
incorporate someerror control mechanism.
2. The main shortcoming of the stop-and-wait algorithm is that it allows the sender to
have only one outstanding frame on the link at a time. The sender should wait till it
gets an ACK of previous frame before it sends next frame. As a result, it wastes a
substantial amount of network bandwidth.
3. Stop and Wait ARQ may work fine where propagation delay is very less for example
LAN connections, but performs badly for distant connections like satellite
connection
Theory:
Protocol).
In this technique, each frame has sent from the sequence number. The
sequence numbers are used to find the missing data in the receiver end. The
Algo:
Go back n:
transmitted.
Step 4: Generate a random number less than or equal to the size of the current
window
Step 5: Transmit the frames and receive the acknowledgement for the frames
sent.
the
Step 9: Repeat the steps 4 to 8 till the number of remaining frames to be send
becomes
zero.
Selective repeat:
transmitted.
Step 4: Generate a random number less than or equal to the size of the current
window
Step 5: Transmit the frames and receive the acknowledgement for the frames
sent.
that frame
alone again.
Step 9: Repeat the steps 4 to 8 till the number of remaining frames to be send
becomes
zero.
Code:
Go back n:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
int temp1,temp2,temp3,temp4,i,winsize=8,noframes,moreframes;
char c;
int reciever(int);
int simulate(int);
clrscr();
temp4=0,temp1=0,temp2=0,temp3=0;
for(i=0;i<200;i++)
rand();
noframes=rand()/200;
getch();
moreframes=noframes;
while(moreframes>=0)
temp1=simulate(winsize);
winsize-=temp1;
temp4+=temp1;
if(temp4 >noframes)
temp4 = noframes;
for(i=temp3+1;i<=temp4;i++)
getch();
temp2=reciever(temp1);
temp3+=temp2;
temp3 = noframes;
getch();
moreframes-=temp2;
temp4=temp3;
if(winsize<=0)
winsize=8;
getch();
int i;
for(i=1;i<100;i++)
rand();
i=rand()%temp1;
return i;
}
int temp1,i;
for(i=1;i<50;i++)
temp1=rand();
if(temp1==0)
temp1=simulate(winsize);
i = temp1%winsize;
if(i==0)
return winsize;
else
return temp1%winsize;
Selective repeat:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
int temp1,temp2,temp3,temp4,temp5,i,winsize=8,noframes,moreframes;
char c;
int reciever(int);
int simulate(int);
int nack(int);
clrscr();
temp4=0,temp1=0,temp2=0,temp3=0,temp5 = 0;
for(i=0;i<200;i++)
rand();
noframes=rand()/200;
getch();
moreframes=noframes;
while(moreframes>=0)
temp1=simulate(winsize);
winsize-=temp1;
temp4+=temp1;
if(temp4 >noframes)
temp4 = noframes;
for(i=noframes - moreframes;i<=temp4;i++)
getch();
temp2=reciever(temp1);
temp3+=temp2;
temp3 = noframes;
temp2 = nack(temp1);
temp5+=temp2;
if (temp5 !=0)
getch();
for(i=1;i<temp5;i++)
getch();
moreframes-=temp1;
if(winsize<=0)
winsize=8;
getch();
int i;
for(i=1;i<100;i++)
rand();
i=rand()%temp1;
return i;
int i;
for(i=1;i<100;i++)
rand();
i=rand()%temp1;
return i;
int temp1,i;
for(i=1;i<50;i++)
temp1=rand();
if(temp1==0)
temp1=simulate(winsize);
i = temp1%winsize;
if(i==0)
return winsize;
else
return temp1%winsize;
}
Output:
Go back n:
Number of frames: 55
Sending frame 1
Sending frame 2
Sending frame 3
Sending frame 1
Sending frame 1
Sending frame 55
Selective repeat:
Number of frames: 55
Sending frame 1
Sending frame 2
Sending frame 3
Sending frame 4
Sending frame 5
Sending frame 6
Retransmitting frame 2
Sending frame 3
Sending frame 4
Sending frame 54
Sending frame 55
Discussions:
controlling and optimising packet flow between a sender and receiver. The
protocol requires the receiver to acknowledge receipt of each data packet and
allows the receiver to confirm the delivery of several packets with a single
acknowledgement (ACK).
To handle the flow of data packets, the receiver keeps a buffer. The receive
buffer stores packets that have been sent but not yet processed by the sender.
The sender is informed of the amount of free space in the receive buffer by the
receiver during data transmission. This area is known as the receive window,
and it is defined as the buffer size minus the quantity of unprocessed data.
The sender cannot send more data packets than the amount of space available
rates, when packet sizes are exceedingly high, substantial buffers on the
sender side may be required to hold them and on mediums with low
Aim:
To implement and find the class, network and host ID from a given IPv4 address.
Theory:
Given a valid IPv4 address in the form of string and it follows Class Full addressing. The task
is to determine the class of the given IPv4 address as well as separate the Network and
Host ID parts from it.
Algo:
1. For determining the class: The idea is to check the first octet of the IP addresses. As
we know, for class A first octet will range from 1 – 126, for class B first octet will
range from 128 – 191, for class C first octet will range from 192- 223, for class D first
octet will range from 224 – 239, for class E first octet will range from 240 – 255.
2. For determining the Network and Host ID: We know that Subnet Mask for Class A is
8, for Class B is 16 and for Class C is 24 whereas Class D and E are not divided into
Network and Host ID.
Code:
#include<stdio.h>
#include<string.h>
char arr[4];
int i = 0;
arr[i] = str[i];
i++;
i--;
int ip = 0, j = 1;
while (i >= 0)
ip = ip + (str[i] - '0') * j;
j = j * 10;
i--;
return 'B';
return 'C';
return 'D';
else
return 'E';
if (ipClass == 'A')
int i = 0, j = 0;
i = 0;
j++;
host[i++] = str[j++];
int i = 0, j = 0, dotCount = 0;
network[i++] = str[j++];
if (str[j] == '.')
dotCount++;
i = 0;
j++;
host[i++] = str[j++];
printf("Network ID is %s\n", network);
int i = 0, j = 0, dotCount = 0;
network[i++] = str[j++];
if (str[j] == '.')
dotCount++;
i = 0;
j++;
host[i++] = str[j++];
}
else
int main()
ipClass);
separate(str, ipClass);
return 0;
Output:
2. IPv4 addresses may be represented in any notation expressing a 32-bit integer value.
They are most often written in the dot-decimal notation, which consists of four octets of
Theory:
In this algorithm, each router evaluates the distance between itself and every achievable
destination. This is accomplished by assessing the distance between a router and all of its
immediate router neighbours and adding each neighbouring routers computations for the
distance between that neighbour and its close neighbours.
Algo:
At each node x,
Initialization
loop
for each y in N:
Dx(y) = minv{c(x,v)+Dv(y)}
Code:
#include<stdio.h>
struct node
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
int costmat[20][20];
int nodes,i,j,k,count=0;
for(j=0;j<nodes;j++)
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
rt[i].from[j]=j;
do
count=0;
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}while(count!=0);
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
printf("\n\n");
getch();
Output:
Discussions:
In Distance Vector Routing: 1. Only distance vectors are exchanged. 2. “Next hop”values are
not exchanged. This is because it results in exchanging the large amount of data which
consumes more bandwidth. 3. While preparing a new routing tableA router takes into
consideration only the distance vectors it has obtained from its neighboring routers. 4. It
does not take into consideration its old routing table. The algorithm keeps on repeating
periodically and never stops. This is to update the shortest path in case any link goes down
or topology changes. Routing tables are prepared total (n-1) times if there are n routers in
the given network. This is because shortest path between any 2 nodes contains at most n-
1 edges if there are n nodes in the graph
We learnt about distance vector routing algorithm and its implementation and significance
in computer networks.
Aim:
Theory:
Initialization
if v adjacent to A
loop
Add w to N
#include<iostream>
int main()
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int count,src_router,i,j,w,v,min;
int cost_matrix[100][100],dist[100],last[100];
int flag[100];
for(i=0;i<count;i++)
for(j=0;j<count;j++)
cin>>cost_matrix[i][j]; if(cost_matrix[i][j]<0)
{
cost_matrix[i][j]=1000;
cin>>src_router;
for(v=0;v<count;v++)
flag[v]=0; last[v]=src_router;
dist[v]=cost_matrix[src_router][v];
flag[src_router]=1;
for(i=0;i<count;i++)
min=1000;
for(w=0;w<count;w++)
v=w; min=dist[w];
}
flag[v]=1;
for(w=0;w<count;w++)
if(!flag[w])
if(min+cost_matrix[v][w]<dist[w])
dist[w]=min+cost_matrix[v][w]; last[w]=v;
for(i=0;i<count;i++)
while(w!=src_router)
cout<<"<--"<<last[w]; w=last[w];
}
return 0;
Output:
Discussions:
4. All items in the database must be sent to neighbors to form link state packets.
We learnt about link state routing protocol and its implementation and significance in
computer networks.