Computer Networks Lab: Delhi Technological University
Computer Networks Lab: Delhi Technological University
Computer Networks Lab: Delhi Technological University
THEORY: Security and error detection are the most prominent features that are to be
provided by any application which transfer data from one end to the other end.
One of such a mechanism in tracking errors which may add up to the original data during
transfer is known as stuffing.
In Bit Stuffing, 01111110 is append within the original data while transfer of it.
In Byte Stuffing, DLESTX and DLEETX are used to denote start and end of character data with
some constraints as shown in program below clearly.
Bit stuffing:
Allows frame to contain arbitrary number of bits and arbitrary character size. The frames
are separated by separating flag.
Each frame begins and ends with a special bit pattern, 01111110 called a flag byte. When
five consecutive l's are encountered in the data, it automatically stuffs a '0' bit into outgoing
bit stream.
In this method, frames contain an arbitrary number of bits and allow character codes with
an arbitrary number of bits per character. In his case, each frame starts and ends with a
special bit pattern, 01111110.
In the data a 0 bit is automatically stuffed into the outgoing bit stream whenever the
sender's data link layer finds five consecutive 1s.
This bit stuffing is similar to byte stuffing, in which an escape byte is stuffed into the
outgoing character stream before a flag byte in the data.
When the receiver sees five consecutive incoming i bits, followed by a o bit, it automatically
destuffs (i.e., deletes) the 0 bit. Bit Stuffing is completely transparent to network layer as
byte stuffing. The figure1 below gives an example of bit stuffing.
This method of framing finds its application in networks in which the change of data into
code on the physical medium contains some repeated or duplicate data. For example, some
LANs encodes bit of data by using 2 physical bits.
Byte stuffing:
In this method, start and end of frame are recognized with the help of flag bytes. Each
frames starts with and ends with a flag byte. Two consecutive flag bytes indicate the end of
one frame and start of the next one. The flag bytes used in the figure 2 used is named as
“ESC” flag byte.
A frame delimited by flag bytes. This framing method is only applicable in 8-bit character
codes which are a major disadvantage of this method as not all character codes use 8-bit
characters e.g. Unicode.
Four example of byte sequences before and after stuffing:
CODE:
#include<bits/stdc++.h>
using namespace std;
int main()
{
// BIT STUFFING
string s;
cout << "BIT STUFFING:\nEnter the data bits : ";
cin >> s;
int set_bits=0, i;
string stuffed="", delimiter="01111110";
for(i = 0; i < s.length(); i++)
{
if(s[i]=='1')
set_bits++;
else
set_bits=0;
stuffed+=s[i];
if(set_bits==5)
{
stuffed+='0';
set_bits=0;
}
}
stuffed=delimiter+stuffed+delimiter;
string destuffed="";
set_bits=0;
for(i=8; i<stuffed.length()-8; i++)
{
if(stuffed[i]=='1')
set_bits++; else set_bits=0;
destuffed+=stuffed[i];
if(set_bits==5)
{
i++;
set_bits=0;
}
}
cout << "data after stuffing : " << stuffed << endl;
cout << "data after destuffing : " << destuffed << endl;
// BYTE STUFFING
int n;
cout << "\nBYTE STUFFING:\nEnter number of bytes : ";
cin >> n;
cout << "Enter the data stream : ";
vector<string> input, byte_stuffed, byte_destuffed;
for(i=0; i<n; i++)
{
cin >> s;
input.push_back(s);
}
for(i=0; i<n; i++)
{
byte_stuffed.push_back(input[i]);
if(input[i]=="FLAG" || input[i]=="ESC")
byte_stuffed.insert(byte_stuffed.end()-1, "ESC");
}
for(i=0; i<byte_stuffed.size(); i++)
{
if(byte_stuffed[i]=="ESC")
i++;
byte_destuffed.push_back(byte_stuffed[i]);
}
cout << "\ndata after stuffing : ";
for(auto&x:byte_stuffed)
cout << x << " ";
cout << endl;
cout << "\ndata after destuffing : ";
for(auto&x:byte_destuffed)
cout << x << " ";
cout << endl;
return 0;
}
OUTPUT:
In Byte Stuffing, DLESTX and DLEETX are used to denote start and end of character data with
some constraints as shown in program below clearly.
The advantage of Bit stuffing is that only a bit not a byte is inserted in the data stream and
that only when the content of the data stream fails to provide a timing signal to the receiver.
Thus, very nearly 100% of the bits transported are useful data.
Bit stuffing is used for various purposes, such as for bringing bit streams that do not
necessarily have the same or rationally related bit rates up to a common rate, or to fill buffers
or frames.