CN LAB (2)
CN LAB (2)
Experiment 1
AIM: Implement the data link layer framing methods such as character, character-stuffing and
bit stuffing.
Theory: 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 −
Bit - Stuffing − A pattern of bits of arbitrary length is stuffed in the message to
differentiate from the delimiter. This is also called bit - oriented framing.
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.
Byte - Stuffing − A byte is stuffed in the message to differentiate from the delimiter.
This is also called character-oriented framing.
If the pattern of the flag byte is present in the message byte sequence, there should be a strategy
so that the receiver does not consider the pattern as the end of the frame. Here, 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.
1
Program Code: // BIT Stuffing program
#include<stdio.h>
#include<string.h>
void main()
{
int a[20],b[30],i,j,k,count,n; printf("Enter
frame length:"); scanf("%d",&n);
printf("Enter input frame (0's & 1's only):");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
i=0; count=1; j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1;a[k]==1 && k<n && count<5;k++)
{ j++;
b[j]=a[k];
count++;
if(count==5)
{ j++;
b[j]=0;
}
i=k;
}}
else
{
b[j]=a[i];
} i++;
j++;
}
printf("After stuffing the frame is:");
for(i=0;i<j;i++)
printf("%d",b[i]);
}
Output:
Enter frame length:5
Enter input frame (0's & 1's only): 1
1
2
1
1
1
After stuffing the frame is:111110
3
Program Code:
//Program for Character Stuffing
#include<stdio.h>
#include<string.h>
#include<process.h>
void main()
{
int i=0,j=0,n,pos; char
a[20],b[50],ch;
printf("Enter string\n");
scanf("%s",&a);
n=strlen(a);
printf("Enter position\n");
scanf("%d",&pos);
if(pos>n)
{
printf("invalid position, Enter again :");
scanf("%d",&pos);}
printf("Enter the character\n");
ch=getche();
b[0]='d';
b[1]='l';
b[2]='e';
b[3]='s';
b[4]='t';
b[5]='x'; j=6;
while(i<n)
{
if(i==pos-1)
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]=ch; b[j+4]='d';
b[j+5]='l';
b[j+6]='e';
j=j+7;
}
if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e')
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
4
j=j+3;
}
b[j]=a[i]; i+
+;
j++;
}
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]='e';
b[j+4]='t';
b[j+5]='x';
b[j+6]='\0';
printf("\nframe after stuffing:\n");
printf("%s",b);
}
Enter string
CMRCET
Enter position
2
Enter the character
frame after stuffing:
dlestxCdldleMRCETdleetx
5
Experiment 2
AIM: Write a program to compute CRC code for the polynomials CRC-12, CRC-16 and CRC,
CCIP.
Theory: CRC method can detect a single burst of length n, since only one bit per column will be
changed, a burst of length n+1 will pass undetected, if the first bit is inverted, the last bit is
inverted and all other bits are correct. If the block is badly garbled by a long burst or by multiple
shorter burst, the probability that any of the n columns will have the correct parity that is 0.5. so
the probability of a bad block being expected when it should not be 2 power(-n). This scheme
sometimes is known as Cyclic Redundancy Code.
9 bits (CRC-8)
17 bits (CRC-16)
33 bits (CRC-32)
65 bits (CRC-64)
Algorithm Steps:
Step 1: Declare int crc 16, SHIFT_CRC, shift Byte, Byte_SIZE as global variables.
Step 4: In cal CRC 16 each character from input shifted with shift-byte where value 987.
6
Source Code:
// program for Cyclic Redundancy Check
#include<stdio.h>
int i.data[10].dl.gen[10],gl,temp[10],c;
void left_shift()
for(i=0;i<gl-1;i++)
temp(i)=temp[i+1];
if(c<d1)
temp[gl-1]=data[c];
else
temp[gl-1]=0
void xor()
if(temp[0]==0
left_shift();
c++;
for(i=0;i<gl;i++)
if(gen(i)==temp(i)
temp(i)=0
else
temp[i]=1; } }
7
main()
int j; printf("1.generate2.check\
nchoice:"); scanf("%d",&j);
scanf("%d",&dl);
for(i=0;i<dl;i++)
scanf("%d";&data[i]);
scanf("%d",&gl);
for(i=0;i<gl;i++)
temp[i]=data[i]
if(j==1)
for(c=4;c<dl+gl-2;c++)
{
xor();
printf("crc:");
for(i=0;i<dl;i++)
printf"%d",data[i]);
else
8
{
for(c=4;c<dl;c++)
xor();
left_shift();
printf("crc");
for(i=1;i<gl;i++)
printf("%d"'temp[i]);
printf("\n");
1.generate
2.check
choice:1
Enter the length of data:6
Enter the data:1
0
1
1
0
1
Enter the length of generator:4
Enter the generator:1
1
0
1
crc:101101001
9
10
11
12