Practical CN
Practical CN
LABORATORY FILE
Computer Networks
(SE-407)
2021
INDEX
Page
S. No Topic
No
1.
2.
3.
4.
5.
6.
7.
8.
9.
PROGRAM – 1A
Objective: To implement data link layer Bit Stuffing.
Theory: Bit stuffing is the insertion of one or more bits into a transmission
unit as a way to provide signalling information to a receiver. The receiver
knows how to detect and remove or disregard the stuffed bits. Bit stuffing is
the insertion of non-information bits into data.
Code:
#include<iostream>
#include<string>
using namespace
std; int main()
{
int a[20], b[30], i, j, k,
count, n; cout<<"Enter frame
size:";
cin>>n;
cout<<"Enter the frame in the form of 0 and
1 :"; for(i=0; i<n; i++)
cin>>a[i];
i=0;
count=1
; j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1; a[k]==1 && k<n && count<5; k++)
{
j++;
b[j]=a[k];
count++;
if(count==5
)
{
j++;
b[j]=0
;
}
i=k;
else
{
b[j]=a[i];
}
i+
+;
j+
+;
}
cout<<"After Bit
Stuffing :"; for(i=0; i<j;
i++)
cout<<b[i]
Output:
Learning Outcome:
Data link layer Bit Stuffing using program in C was learnt
PROGRAM – 1B
Objective: To implement data link layer Character Stuffing.
Code:
#include
<stdio.h>
#include
<string.h> int
main()
{
int i, j, n, p, count = 0;
char a[30], b[4] = "dle";
printf("Enter the string:
"); scanf("%s", a);
n = strlen(a);
printf("length is %d \n", n);
printf("\nframe after stuffing:\
n"); printf("dlestx");
for (i = 0; i < n; i++)
{
count =
0; p = i;
for (int j = 0; j < 3; j++)
{
if (a[i] == b[j])
{
count = count +
1; i++;
}
}
if (count != 3)
{
i = p;
if (count == 3)
{
printf("dledle");
}
else
{
printf("%c", a[i]);
}
}
printf("dleetx\
n"); return 0;
}
Output:
Learning Outcome:
Data link layer Character Stuffing using program in C was learnt
PROGRAM – 1C
Objective: To implement data link layer Byte Stuffing.
Theory: In byte stuffing, a special byte called the escape character (ESC) is
stuffed before every byte in the message with the same pattern as the flag
byte. If the ESC sequence is found in the message byte, then another ESC byte
is stuffed before it.
Start
Append 01111110 at the beginning of the string
Check the data if character 01111110 is present in the string then add
another additional 01111110 ub the string
Append 01111110 in the end of the string
Display the string
Stop
Code:
#include
<iostream> using
namespace std; int
main()
{
string str;
cout << "Enter the input Data having 0s and 1s
only:"; cin >> str;
string stuff = "";
string add =
"01111110"; stuff +=
add;
int i = 0;
while (i < str.length())
{
if (str.length() - i >= 8 && str.substr(i, 8) == "01111110")
{
stuff += str.substr(i,
8); stuff += add;
i = i + 8;
}
else
stuff += str[i++];
stuff += add;
cout << "The stuffed bit string is: " << stuff <<
endl; return 0;
}
Output:
Learning Outcome:
Concept of Byte Stuffing in Data Link Layer
PROGRAM – 3
Objective: Program to implement Cyclic Redundancy Check Protocol (CRC).
The communicating parties agrees upon the size of message block and
the CRC divisor. For example, the block chosen may be CRC (7, 4),
where 7 is the total length of the block and 4 is the number of bits in
the data segment. The divisor chosen may be 1011.
The sender performs binary division of the data segment by the divisor.
It then appends the remainder called CRC bits to the end of data
segment. This makes the resulting data unit exactly divisible by the
divisor.
Decoding
Code:
#include
<iostream> using
namespace std;
{
for (int i = 0; i < n; i++)
{
if (gen[0] == temp[i])
{
for (int j = 0, k = i; j < r + 1; j++,
k++) if (!(temp[k] ^ gen[j]))
temp[k] =
0; else
temp[k] = 1;
}
}
}
int main()
{
int n, r, message[50], gen[50],
temp[50]; cout << "At Sender's End "
<< endl;
cout << "Enter the number of message bits
: "; cin >> n;
cout << "Enter the number of generator bits
: "; cin >> r;
cout << "Enter the message :
"; for (int i = 0; i < n; i+
+)
cin >> message[i];
cout << "Enter the generator :
"; for (int i = 0; i < r; i++)
cin >>
gen[i]; r--;
for (int i = 0; i < r; i+
+) message[n + i] = 0;
for (int i = 0; i < n + r;
i++) temp[i] =
message[i];
division(temp, gen, n,
r); cout << "CRC : ";
for (int i = 0; i < r; i++)
{
cout << temp[n + i] << "
"; message[n + i] = temp[n
+ i];
}
cout << endl
<< "Transmitted Message :
"; for (int i = 0; i < n + r;
i++)
cout << message[i] << "
"; cout << endl
Downloaded by Depressed Pigeon
lOMoARcPSD|48988265
10
<< endl
<< "At Receiver's End " << endl;
11
Output:
Learning Outcome:
Concept of Cyclic Redundancy Check (CRC) is learned.
12
PROGRAM – 4
Objective:- program to implement stop and wait protocol.
Theory: Here stop and wait means, whatever the data that sender wants to
send, he sends the data to the receiver. After sending the data, he stops and
waits until he receives the acknowledgment from the receiver. The stop and
wait protocol are a flow control protocol where flow control is one of the
services of the data link layer. It is a data-link layer protocol which is used
for transmitting the data over the noiseless channels. It provides
unidirectional data transmission which means that either sending or
receiving of data will take place at a time. It provides flow-control
mechanism but does not provide any error control mechanism. The idea
behind the usage of this frame is that when the sender sends the frame then
he waits for the acknowledgment before sending the next frame.
Sender side
Rule 1: Sender sends one data packet at a time.
Rule 2: Sender sends the next packet only when it receives the
acknowledgment of the previous packet.
Therefore, the idea of stop and wait protocol in the sender's side is very
simple, i.e., send one packet at a time, and do not send another packet before
receiving the acknowledgment.
Receiver side
Rule 1: Receive and then consume the data packet.
Rule 2: When the data packet is consumed, receiver sends the
acknowledgment to the sender.
Therefore, the idea of stop and wait protocol in the receiver's side is also
very simple, i.e., consume the packet, and once the packet is consumed, the
acknowledgment is sent. This is known as a flow control mechanism.
13
Code:
#include
<iostream>
#include <time.h>
#include <cstdlib>
#include <ctime>
#include
<unistd.h> 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()
{
int frames[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
unsigned long seconds =
5; srand(time(NULL));
timer t;
cout << "Sender has to send
frames : "; for (int i = 0; i < 10;
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]; cout.flush();
Downloaded by Depressed Pigeon
lOMoARcPSD|48988265
14
15
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;
}
} while (count !=
10); return 0;
Downloaded by Depressed Pigeon
lOMoARcPSD|48988265
16
17
Output:
18
Learning Outcome:
Stop and Wait protocol implementation was learnt.
19
PROGRAM – 5
Objective:- Program to implement sliding window protocol.
Both sender and receiver agree on some window size. If window size=w then
after sending w frames sender waits for the acknowledgement (ack) of the
first frame.
Code:
#include <iostream>
using namespace
20
cout << "\nWith sliding window protocol the frames will be sent in
the foll owing manner (assuming no corruption of frames)\n\n";
cout << "After sending " << w << " frames at each stage sender waits
for ac knowledgement sent by the receiver\n\n";
if (f % w != 0)
cout << "\nAcknowledgement of above frames sent is received by
sender\n
";
return 0;
Output:
Learning Outcome:
Sliding window protocol implementation was learnt
21
PROGRAM – 5
Objective: To implement and find Class, Network Id and Host Id of given
IPV4 address.
• Class A addresses: The first 8 bits of the IP address are used for the
network ID. The final 24 bits are used for the host ID.
• Class B addresses: The first 16 bits of the IP address are used for the
network ID. The final 16 bits are for the host ID.
• Class C addresses: The first 24 bits of the IP address are used for the
network ID. The final 8 bits are for the host ID.
Code:
#include
<iostream>
#include <cstring>
using namespace
std;
int ip = 0, j =
1; while (i >=
0)
{
22
// Class A
if (ip >= 1 && ip <=
126) return 'A';
// Class B
else if (ip >= 128 && ip <=
191) return 'B';
// Class C
else if (ip >= 192 && ip <=
223) return 'C';
// Class D
else if (ip >= 224 && ip <=
239) return 'D';
// Class E
if (ipClass == 'A')
{
int i = 0, j = 0;
while (str[j] !=
'.')
network[i++] = str[j+
+]; i = 0;
j++;
while (str[j] != '\0')
host[i++] = str[j+
+];
cout << "Network ID is: " << network <<
endl; cout << "Host ID is: " << host;
}
else if (ipClass == 'B')
{
int i = 0, j = 0, dotCount = 0;
23
24
network[i++] = str[j+
+]; if (str[j] == '.')
dotCount++;
}
i =
0; j+
+;
i =
0; j+
+;
int main()
{
string str;
cout << "Enter the IPv4
address: "; cin >> str;
char ipClass = findClass(str);
25
cout << "Given IP address belongs to Class: " << ipClass <<
endl; separate(str, ipClass);
26
return 0;
}
Output:
Learning Outcome:
Concept of determining class, host ID and network ID from the given IPV4
address is learned.
27
PROGRAM – 6
Objective: To implement distance vector routing algorithm
28
Code:
#include
<stdio.h>
#include
<iostream>
struct node
{
unsigned
dist[6];
unsigned
from[6];
} DVR[10];
int main()
{
cout << "\n\n---------Distance Vector Routing Algorithm-----------";
int costmat[6]
[6]; int nodes,
i, j, k;
cout << "\n\n Enter the number of
nodes : "; cin >> nodes;
cout << "\n Enter the cost matrix :
\n"; for (i = 0; i < nodes; i++)
{
for (j = 0; j < nodes; j++)
{
cin >> costmat[i][j];
costmat[i][i] = 0;
DVR[i].dist[j] = costmat[i]
[j]; DVR[i].from[j] = j;
}
}
for (i = 0; i < nodes; i++)
for (j = i + 1; j < nodes;
j++) for (k = 0; k <
nodes; k++)
if (DVR[i].dist[j] > costmat[i][k] + DVR[k].dist[j])
{
DVR[i].dist[j] = DVR[i].dist[k] +
DVR[k].dist[j]; DVR[j].dist[i] =
DVR[i].dist[j];
DVR[i].from[j] = k;
DVR[j].from[i] = k;
}
for (i = 0; i < nodes; i++)
Downloaded by Depressed Pigeon
lOMoARcPSD|48988265
29
{
cout << "\n\n For router: " << i
+ 1; for (j = 0; j < nodes; j++)
30
cout << "\t\n node " << j + 1 << " via " << DVR[i].from[j] +
1 << " Distance " << DVR[i].dist[j];
}
cout << " \n\n
"; return 0;
}
Output:
31
Learning Outcome:
Distance vector routing algorithm implementation was learnt
32
PROGRAM – 7
Objective: To implement link state routing algorithm
Theory: Link state routing is a technique in which each router shares the
knowledge of its neighbourhood with every other router in the
internetwork. The three keys to understand the Link State Routing
algorithm:
Knowledge about the neighbourhood: Instead of sending its routing
table, a router sends the information about its neighbourhood only. A
router broadcast its identities and cost of the directly attached links to
other routers.
Flooding: Each router sends the information to every other router on
the internetwork except its neighbours. This process is known as
Flooding. Every router that receives the packet sends the copies to all
its neighbours. Finally, each and every router receives a copy of the
same information.
Information sharing: A router sends the information to every other
router only when the change occurs in the information.
Code:
#include <iostream>
#include <bits/stdc+
+.h> #include
<string.h>
using namespace
33
34
{
cout << "\n " << sr << " to " << i << " : Path taken : "
<< i; w = i;
while (w != sr)
{
cout << " <-- " <<
last[w]; w = last[w];
}
cout << "\n Shortest path cost: " << dist[i] << "\n";
}
}
return 0;
}
Output:
35
36
37
38
Learning Outcome:
Link state routing algorithm implementation was learnt