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>

main()

{

int i,k,a,count=0; char c[50];

printf(“\nEnter the data to be send:”);

fflush(stdin);

gets(c); a=strlen(c);

for(i=0;i<a;i++)

{

if(c[i]==’1′)

{

count++; if(count==6)

{

}

}

else

for(k=a;k>i+1;k–)

c[k]=c[k-1]; a=a+1;

count=0; c[i+1]=’0′;

count=0;

}

printf(“\nData after stuffing:”); puts(c);

count=0; for(i=0;i<a;i++)

{

if(c[i]==’1′)

{

count++; if(count==5)

{

for(k=i+1;k<a;k++) c[k]=c[k+1];

count=0;

}

}

}

}

Output:

Enter the data to be send: 11111111

Data after stuffing: 111110111

Program:(character-stuffing)

#include<stdio.h>

main()

{

char a[50],b[50],c[50];

int i,j,k,m=0,count,l1,l2;

printf(“\nEnter the data to send:”);

fflush(stdin);

gets(a);

printf(“\nEnter the delimeter:”);

fflush(stdin);

gets(b); l1=strlen(a); l2=strlen(b);

c[0]=’S’;

for(i=0;i<l2;i++)

c[i+1]=b[i]; for(j=0;j<l1;j++)

{

count=0; for(k=0,m=j;k<l2;k++,m++)

{

if(a[m]!=b[k])

count=1;

}

if(count==0)

{

for(k=l1+l2-1;k>m;k–)

a[k]=a[k-l2]; for(k=0;k<l2;k++)

a[m++]=b[k];

j=j+l2; l1=l1+l2;

}

}

for(k=0;k<l1;k++) c[++i]=a[k];

for(k=0;k<l2;k++) c[++i]=b[k];

c[++i]=’E’;

printf(“\nData after stuffing:”);

for(k=0;k<=i;k++)

printf(“%c”,c[k]);

for(j=0;j<l1-l2;j++)

{

count=0; m=j;

for(k=0;k<l2;k++)

{

if(a[m++]!=b[k]) count=1;

}

if(count==0)

{

for(k=m;k<l1-l2;k++) a[k]=a[k+l2];

l1=l1-l2;

}

}

printf(“\nData after destuffing:”);

for(i=0;i<l1;i++)

printf(“%c”,a[i]);

}

Output:

Enter the data to send : hi hello hai

Enter the delimeter: l

Data after stuffing: slhi hellllo hai le

Data after destuffing: hi hello hai