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

Practical File of Computer Network

dtu lab file

Uploaded by

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

Practical File of Computer Network

dtu lab file

Uploaded by

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

lOMoARcPSD|489 882 65

LABORATORY FILE

Computer Networks
(SE-407)
2021

DELHI TECHNOLOGICAL UNIVERSITY


(Formerly Delhi College of Engineering)

Submitted to: Submitted By:


Mr. Vikas Sharma Tatwagye Sheel
2K22/ME/265

Downloaded by Depressed Pigeon ([email protected])


lOMoARcPSD|489 882 65

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 Depressed Pigeon ([email protected])


lOMoARcPSD|489 882 65

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 Depressed Pigeon ([email protected])


lOMoARcPSD|489 882 65

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 Depressed Pigeon ([email protected])


lOMoARcPSD|489 882 65

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 Depressed Pigeon ([email protected])


lOMoARcPSD|489 882 65

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 Depressed Pigeon ([email protected])


lOMoARcPSD|489 882 65

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 Depressed Pigeon ([email protected])


lOMoARcPSD|489 882 65

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 Depressed Pigeon ([email protected])


lOMoARcPSD|489 882 65

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 Depressed Pigeon ([email protected])


lOMoARcPSD|489 882 65

{
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 Depressed Pigeon ([email protected])


lOMoARcPSD|489 882 65

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 Depressed Pigeon ([email protected])


lOMoARcPSD|489 882 65

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 Depressed Pigeon ([email protected])


lOMoARcPSD|489 882 65

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 Depressed Pigeon ([email protected])


lOMoARcPSD|489 882 65

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 Depressed Pigeon ([email protected])


lOMoARcPSD|489 882 65

14

Output:

Downloaded by Depressed Pigeon ([email protected])


lOMoARcPSD|489 882 65

15

Learning Outcome:
Stop and Wait protocol implementation was learnt.

Downloaded by Depressed Pigeon ([email protected])

You might also like