Experiment No 01-1
Experiment No 01-1
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.
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
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
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;
}
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];
}