0% found this document useful (0 votes)
27 views5 pages

Experiment No 01-1

The document outlines the implementation of data link layer framing methods, specifically character and bit stuffing. It describes the purpose of bit stuffing to avoid confusion with frame delimiters by inserting non-information bits, and byte stuffing to handle cases where the delimiter appears in the message. Additionally, it provides example programs for both bit and character stuffing, including input and output demonstrations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

Experiment No 01-1

The document outlines the implementation of data link layer framing methods, specifically character and bit stuffing. It describes the purpose of bit stuffing to avoid confusion with frame delimiters by inserting non-information bits, and byte stuffing to handle cases where the delimiter appears in the message. Additionally, it provides example programs for both bit and character stuffing, including input and output demonstrations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment no 01

Aim: Implement the datalink layer framing methods such as character and bit stuffing.
Purpose of Bit Stuffing
In Data Link layer, the stream of bits from the physical layer is divided into data frames. The
data frames can be of fixed length or variable length. In variable – length framing, the size of
each frame to be transmitted may be different. So, a pattern of bits is used as a delimiter to
mark the end of one frame and the beginning of the next frame. However, if the pattern occurs
in the message, then mechanisms needs to be incorporated so that this situation is avoided.

The two common approaches are −

Byte – Stuffing − A byte or character is stuffed in the message to differentiate from the delimiter
for This is also called character-oriented framing.
Bit – Stuffing − Bit stuffing is the mechanism of inserting one or more non-information bits into a
message to be transmitted, to break up the message sequence, for synchronization
upper layers as text, graphics, audio, video etc. A frame has the following parts −

Frame Header − It contains the source and the destination addresses of the
Payload field − It contains the message to be
Trailer − It contains the error detection and error correction
Flags − A bit pattern that defines the beginning and end bits in a It is generally of 8- bits. Most
protocols use the 8-bit pattern 01111110 as flag.
payload-of-variable number-of-bits

Bit Stuffing Mechanism


In a data link frame, the delimiting flag sequence generally contains six or more consecutive 1s.
In order to differentiate the message from the flag in case of the same sequence, a single bit is
stuffed in the message. Whenever a 0 bit is followed by five consecutive 1bits in the message,
an extra 0 bit is stuffed at the end of the five 1s.

When the receiver receives the message, it removes the stuffed 0s after each sequence of five
1s. The un-stuffed message is then sent to the upper layers.

bit-stuffing-mechanism

Purpose of Byte Stuffing


In Data Link layer, the stream of bits from physical layer is divided into data frames. The data
frames can be of fixed length or variable length. In variable – length framing, the size of each
frame to be transmitted may be different. So, a pattern of bits is used as a delimiter to mark the
end of one frame and the beginning of the next frame. However, if the pattern occurs in the
message, then mechanisms needs to be incorporated so that this situation is avoided.

Frame in a Character – Oriented Framing


In character – oriented protocols, the message is coded as 8-bit characters, using codes like
ASCII codes.

A frame has the following parts −


Frame Header − It contains the source and the destination addresses of the
Payload field − It contains the message to be
Trailer − It contains the error detection and error correction
Flags − 1- byte (8-bits) flag at the beginning and at end of the It is a protocol – dependent
special character, signalling the start and end of the frame.
payload-of-variable-length

Byte Stuffing Mechanism


If the pattern of the flag byte is present in the message byte, there should be a strategy so that
the receiver does not consider the pattern as the end of the frame. In character – oriented
protocol, the mechanism adopted is byte stuffing.

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.

Implement the data link layer farming methods such as character, character stuffing, and bit
stuffing.

byte-stuffing

Program (Bit-Stuffing)
#include <stdio.h>
#include <string.h>
int main() {
int i, k, a, count = 0;
char c[100];
printf("Enter the data to be sent (only 0s and 1s): ");
scanf("%s", c); // Read input without spaces
a = strlen(c); // Get the length of input
// Bit stuffing process
for (i = 0; i < a; i++) {
if (c[i] == '1') {
count++;
if (count == 5) { // If 5 consecutive 1s are found
// Shift characters to the right to insert '0'
for (k = a; k > i + 1; k--) {
c[k] = c[k - 1];
}
c[i + 1] = '0'; // Insert '0' after 5 ones
a++; // Increase the size of the string
count = 0; // Reset the '1' count
}
} else {
count = 0; // Reset count if '0' is encountered
}
}
c[a] = '\0'; // Ensure the string is properly terminated
printf("\nData after stuffing: %s\n", c);
// Bit unstuffing process
count = 0;
for (i = 0; i < a; i++) {
if (c[i] == '1') {
count++;
if (count == 5) { // When 5 ones are encountered
// Shift characters to remove the stuffed '0'
for (k = i + 1; k < a; k++) {
c[k] = c[k + 1];
}
a--; // Reduce the size of the string
count = 0; // Reset the '1' count
}
} else {
count = 0; // Reset count if '0' is encountered
}
}
c[a] = '\0'; // Properly terminate the unstuffed string
printf("Data after unstuffing: %s\n", c);
return 0;
}

Example Input & Output


Input:
Enter the data to be sent (only 0s and 1s): 111110111
Output:
Data after stuffing: 11111010111
Data after unstuffing: 111110111

Program (Character-Stuffing)
#include <stdio.h>
#include <string.h>
int main() {
char data[100], delimiter[50], stuffed[200];
int i, j, k, m, count, data_len, delim_len;
// Input data and delimiter
printf("\nEnter the data to send: ");
fgets(data, sizeof(data), stdin);
data[strcspn(data, "\n")] = '\0'; // Remove newline character
printf("\nEnter the delimiter: ");
fgets(delimiter, sizeof(delimiter), stdin);
delimiter[strcspn(delimiter, "\n")] = '\0'; // Remove newline character
data_len = strlen(data);
delim_len = strlen(delimiter);
// Start constructing the stuffed data
m = 0;
stuffed[m++] = 'S'; // Start flag
// Append the delimiter at the beginning
for (i = 0; i < delim_len; i++) {
stuffed[m++] = delimiter[i];
}
// Perform character stuffing
for (j = 0; j < data_len; j++) {
count = 0;
// Check if the substring matches the delimiter
for (k = 0; k < delim_len; k++) {
if (data[j + k] != delimiter[k]) {
count = 1;
break;
}
}
if (count == 0) { // If delimiter found in the data, insert extra delimiter
for (k = 0; k < delim_len; k++) {
stuffed[m++] = delimiter[k];
}
j += delim_len - 1; // Move the index by the length of the delimiter
} else {
stuffed[m++] = data[j]; // Copy data normally
}
}
// Append the delimiter and end flag to the stuffed data
for (k = 0; k < delim_len; k++) {
stuffed[m++] = delimiter[k];
}

stuffed[m++] = 'E'; // End flag


stuffed[m] = '\0'; // Properly terminate the stuffed data
// Output the stuffed data
printf("\nData after stuffing: %s\n", stuffed);
// Destuffing process
char destuffed[100];
int destuffed_index = 0;
// Ignore the start flag and first delimiter
j = delim_len + 1;
while (j < m - (delim_len + 1)) { // Ignore last delimiter and end flag
count = 0;
// Check if the substring matches the delimiter
for (k = 0; k < delim_len; k++) {
if (stuffed[j + k] != delimiter[k]) {
count = 1;
break;
}
}
if (count == 0) { // If an extra delimiter is found, skip it
j += delim_len;
}
destuffed[destuffed_index++] = stuffed[j++];
}
destuffed[destuffed_index] = '\0'; // Properly terminate the destuffed string
// Output the destuffed data
printf("\nData after destuffing: %s\n", destuffed);
return 0;
}
Example Input & Output
Input:
Enter the data to send: Hello Coders
Enter the delimiter: 2
Output:
Data after stuffing: S2Hello Coders2E
Data after destuffing: Hello Coders

You might also like