0% found this document useful (0 votes)
26 views

Computer Network Lab File

The document discusses several computer network experiments including bit stuffing, character stuffing, byte stuffing and cyclic redundancy check (CRC). It provides code examples in C++ to implement these data link layer concepts. The experiments aim to help students learn important computer network fundamentals.

Uploaded by

gauravlodhi983
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Computer Network Lab File

The document discusses several computer network experiments including bit stuffing, character stuffing, byte stuffing and cyclic redundancy check (CRC). It provides code examples in C++ to implement these data link layer concepts. The experiments aim to help students learn important computer network fundamentals.

Uploaded by

gauravlodhi983
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

lOMoARcPSD|22567737

Computer Network -Lab file

Computer Networks (Delhi Technological University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Gaurav Lodhi ([email protected])
lOMoARcPSD|22567737

LABORATORY FILE

Computer Networks
(SE-407)
2021

DELHI TECHNOLOGICAL UNIVERSITY


(Formerly Delhi College of Engineering)

Submitted to: Submitted By:


Dr. Anil Singh Parihar ~ Ajay Kumar
2K18/SE/014

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

INDEX

Page
S. No Topic
No
1. Experiment 1A 2
2. Experiment 1B 4

3. Experiment 1C 6

4. Experiment 2 8
5. Experiment 3 11

6. Experiment 4 16

7. Experiment 5 18
8. Experiment 6 22

9. Experiment 7 26

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

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;
}
}

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

else
{
b[j]=a[i];
}
i++;
j++;
}
cout<<"After Bit Stuffing :";
for(i=0; i<j; i++)
cout<<b[i];
return 0;
}

Output:

Learning Outcome:
Data link layer Bit Stuffing using program in C was learnt

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

PROGRAM – 1B
Objective: To implement data link layer Character Stuffing.

Theory: In byte stuffing (or character stuffing), a particular byte is added to


the data section of the frame when there is a character with the same pattern
as the flag. The data section is stuffed with an extra byte. This byte is usually
known as the escape character (ESC), which has a predefined bit pattern.
Whenever the receiver encounters the ESC character, it deletes it from the
data section and treats the next character as data, not a delimiting flag.

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;
}

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

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

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

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++];
}

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

stuff += add;
cout << "The stuffed bit string is: " << stuff << endl;
return 0;
}

Output:

Learning Outcome:
Concept of Byte Stuffing in Data Link Layer

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

PROGRAM – 2
Objective: Program to implement Cyclic Redundancy Check (CRC).

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.

Encoding using 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

• The receiver divides the incoming data unit by the divisor.


• If there is no remainder, the data unit is assumed to be correct and is
accepted.
• Otherwise, it is understood that the data is corrupted and is therefore
rejected. The receiver may then send an erroneous acknowledgement back
to the sender for retransmission.

Code:
#include <iostream>
using namespace std;
void division(int temp[], int gen[], int n, int r)

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

{
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
<< endl
<< "At Receiver's End " << endl;

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

10

cout << "Enter the received message : ";


for (int i = 0; i < n + r; i++)
cin >> message[i];
for (int i = 0; i < n + r; i++)
temp[i] = message[i];
division(temp, gen, n, r);
for (int i = 0; i < r; i++)
{
if (temp[n + i])
{
cout << "Error detected in received message.";
return 0;
}
}
cout << "No error in received Message.\nReceived Message : ";
for (int i = 0; i < n; i++)
cout << message[i] << " ";
return 0;
}

Output:

Learning Outcome:
Concept of Cyclic Redundancy Check (CRC) is learned.

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

11

PROGRAM – 3
Objective: 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.

The primitives of stop and wait protocol are:

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.

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

12

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();
cout << "\t\t";

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

13

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 Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

14

Output:

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

15

Learning Outcome:
Stop and Wait protocol implementation was learnt.

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

16

PROGRAM – 4
Objective: To implement sliding window protocol.

Theory: In computer networks sliding window protocol is a method to


transmit data on a network. Sliding window protocol is applied on the Data
Link Layer of OSI model. At data link layer data is in the form of frames. In
Networking, Window simply means a buffer which has data frames that needs
to be transmitted.

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.

As soon as sender receives the acknowledgement of a frame it is replaced by


the next frames to be transmitted by the sender. If receiver sends a collective
or cumulative acknowledgement to sender then it understands that more than
one frames are properly received

Code:
#include <iostream>

using namespace std;

int main()
{
int w, i, f, frames[50];

cout << "Enter window size: ";


cin >> w;

cout << "\nEnter number of frames to transmit: ";


cin >> f;

cout << "\nEnter " << f << " frames: ";

for (i = 1; i <= f; i++)


cin >> frames[i];

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

17

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";

for (i = 1; i <= f; i++)


{
if (i % w == 0)
{
cout << frames[i] << "\n";
cout << "Acknowledgement of above frames sent is received by sender
\n\n";
}
else
cout << frames[i] << " ";
}

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

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

18

PROGRAM – 5
Objective: To implement and find Class, Network Id and Host Id of given
IPV4 address.

Theory: Every IP address (even though it looks to be in four parts) is broken


down into two segments but those segments aren’t equal. Part of the IP
address is used for <network ID, and the rest of the address is used for the
<host ID.= Because address allotted to the network ID varies, depending on the
address. Most IP addresses fall into the following address classes:

• 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;

char findClass(string str)


{
char arr[4];
int i = 0;
while (str[i] != '.')
{
arr[i] = str[i];
i++;
}
i--;

int ip = 0, j = 1;
while (i >= 0)
{
ip = ip + (str[i] - '0') * j;
j = j * 10;
i--;

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

19

// 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 else
return 'E';
}

void separate(string str, char ipClass)


{
char network[12], host[12];
for (int k = 0; k < 12; k++)
network[k] = host[k] = '\0';

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;

while (dotCount < 2)


{

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

20

network[i++] = str[j++];
if (str[j] == '.')
dotCount++;
}
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 == 'C')
{
int i = 0, j = 0, dotCount = 0;

while (dotCount < 3)


{
network[i++] = str[j++];
if (str[j] == '.')
dotCount++;
}

i = 0;
j++;

while (str[j] != '\0')


host[i++] = str[j++];

cout << "Network ID is: " << network << endl;


cout << "Host ID is: " << host;
}
else
cout << "In this Class, IP address is not divided into Network and Hos
t ID";
}

int main()
{
string str;
cout << "Enter the IPv4 address: ";
cin >> str;
char ipClass = findClass(str);
cout << "Given IP address belongs to Class: " << ipClass << endl;
separate(str, ipClass);

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

21

return 0;
}

Output:

Learning Outcome:
Concept of determining class, host ID and network ID from the given IPV4
address is learned.

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

22

PROGRAM – 6
Objective: To implement distance vector routing algorithm

Theory: A distance-vector routing protocol requires that a router informs


its neighbours of topology changes periodically. Compared to link-state
protocols, which require a router to inform all the nodes in a network of
topology changes, distance-vector routing protocols have less computational
complexity and message overhead. The term distance vector refers to the fact
that the protocol manipulates vectors (arrays) of distances to other nodes in
the network. The vector distance algorithm was the original ARPANET routing
algorithm and was also used in the internet under the name of RIP.
Routers using distance-vector protocol do not have knowledge of the entire
path to a destination.
Instead they use two methods:
1. Direction in which router or exit interface a packet should be
forwarded.
2. Distance from its destination
Distance-vector protocols are based on calculating the direction and distance
to any link in a network. "Direction" usually means the next hop address and
the exit interface. "Distance" is a measure of the cost to reach a certain node.
The least cost route between any two nodes is the route with minimum
distance. Each node maintains a vector (table) of minimum distance to every
node. The cost of reaching a destination is calculated using various route
metrics. RIP uses the hop count of the destination whereas IGRP takes into
account other information such as node delay and available bandwidth.
Updates are performed periodically in a distance-vector protocol where all or
part of a router's routing table is sent to all its neighbours that are configured
to use the same distance-vector routing protocol. RIP supports cross-platform
distance vector routing whereas IGRP is a Cisco Systems proprietary distance
vector routing protocol. Once a router has this information it is able to amend
its own routing table to reflect the changes and then inform its neighbours of
the changes.

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

23

Code:
#include <stdio.h>
#include <iostream>

using namespace std;

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++)
{
cout << "\n\n For router: " << i + 1;
for (j = 0; j < nodes; j++)

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

24

cout << "\t\n node " << j + 1 << " via " << DVR[i].from[j] + 1 << "
Distance " << DVR[i].dist[j];
}
cout << " \n\n ";
return 0;
}

Output:

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

25

Learning Outcome:
Distance vector routing algorithm implementation was learnt

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

26

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 std;

int main()
{
int count, i, j, k, w, v, n;
int cost_matrix[100][100], dist[100], last[100], min;
int flag[100];

cout<<"----------------link state routing algorithm----------------"<<endl;


cout << "\n Enter the number of routers : ";
cin >> count;
cout << "\n Enter the cost matrix values : \n";

for (i = 0; i < count; i++)


{

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

27

for (j = 0; j < count; j++)


{
cout << i << " to " << j << ": ";
cin >> cost_matrix[i][j];
if (cost_matrix[i][j] < 0)
cost_matrix[i][j] = 1000;
}
}

for (int sr = 0; sr < count; sr++)


{
cout << "--------------------------------------------------------------
";
cout << "\nfor source router " << sr << " :\n";
for (v = 0; v < count; v++)
{
flag[v] = 0;
last[v] = sr;
dist[v] = cost_matrix[sr][v];
}
flag[sr] = 1;
for (i = 0; i < count; i++)
{
min = 1000;
for (w = 0; w < count; w++)
{
if (!flag[w])
if (dist[w] < min)
{
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++)

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

28

{
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:

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

29

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

30

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

31

Downloaded by Gaurav Lodhi ([email protected])


lOMoARcPSD|22567737

32

Learning Outcome:
Link state routing algorithm implementation was learnt

Downloaded by Gaurav Lodhi ([email protected])

You might also like