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

CN Experiment_3

The document presents a program demonstrating Bit and Byte Stuffing techniques at the Data Link Layer. It includes C code for bit stuffing, which inserts a 0 after five consecutive 1s, and byte stuffing, which uses escape sequences for specific flags. The code provides examples of how to implement these techniques and display the results.

Uploaded by

dishashukla0110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

CN Experiment_3

The document presents a program demonstrating Bit and Byte Stuffing techniques at the Data Link Layer. It includes C code for bit stuffing, which inserts a 0 after five consecutive 1s, and byte stuffing, which uses escape sequences for specific flags. The code provides examples of how to implement these techniques and display the results.

Uploaded by

dishashukla0110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

EXPERIMENT 3

AIM: Write a program for demonstrating Bit and Byte Stuffing at Data Link Layer.
BIT STUFFING
#include <stdio.h>
#include <string.h>
// Function for bit stuffing
void bitStuffing(int N, int arr[])
{
// Stores the stuffed array
int brr[30];
// Variables to traverse arrays
int i, j, k;
i = 0;
j = 0;
// Loop to traverse in the range [0, N)
while (i < N) {
// If the current bit is a set bit
if (arr[i] == 1) {
// Stores the count of consecutive ones
int count = 1
// Insert into array brr[]
brr[j] = arr[i];
// Loop to check for
// next 5 bits
for (k = i + 1;
arr[k] == 1 && k < N && count < 5; k++) {
j++;
brr[j] = arr[k];
count++;
// If 5 consecutive set bits
// are found insert a 0 bit
if (count == 5) {
j++;
brr[j] = 0;
}
i = k;
}
}
// Otherwise insert arr[i] into
// the array brr[]
else {
brr[j] = arr[i];
}
i++;
j++;
}
// Print Answer
for (i = 0; i < j; i++)
printf("%d", brr[i]);
}
// Driver Code
int main()
{
int N = 6;
int arr[] = { 1, 1, 1, 1, 1, 1 };
bitStuffing(N, arr);
return 0;
}

OUTPUT:
BYTE STUFFING
#include <stdio.h>
#include <string.h>
void main()
{
char frame[50][50], str[50][50];
char flag[10];
strcpy(flag, "flag");
char esc[10];
strcpy(esc, "esc");
int i, j, k = 0, n;
strcpy(frame[k++], "flag");
printf("Enter length of String : \n");
scanf("%d", &n);
printf("Enter the String: ");
for (i = 0; i <= n; i++)
{
gets(str[i]);
}
printf("\nYou entered :\n");
for (i = 0; i <= n; i++)
{
puts(str[i]);
}
printf("\n");
for (i = 1; i <= n; i++)
{
if (strcmp(str[i], flag) != 0 && strcmp(str[i], esc) != 0)
{
strcpy(frame[k++], str[i]);
}
else
{
strcpy(frame[k++], "esc");
strcpy(frame[k++], str[i]);
}
}
strcpy(frame[k++], "flag");
printf("------------------------------\n\n");
printf("Byte stuffing at sender side:\n\n");
printf("------------------------------\n\n");
for (i = 0; i < k; i++)
{
printf("%s\t", frame[i]);
}
}

OUTPUT:

You might also like