Practical File of Computer Network
Practical File of Computer Network
LABORATORY FILE
Computer Networks
(SE-407)
2021
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
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];
return 0;
}
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 – 2
Objective: Program to implement Cyclic Redundancy Check (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;
void division(int temp[], int gen[], int n, int r)
{
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;
10
Output:
Learning Outcome:
Concept of Cyclic Redundancy Check (CRC) is learned.
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.
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.
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";
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;
}
14
Output:
15
Learning Outcome:
Stop and Wait protocol implementation was learnt.