Monday 24 December 2012

CN 3-2 lab programs

PROGRAM 1: Implement the data link layer framing methods such as character stuffing and bit stuffing.

                                                 CHARACTER STUFFINIG 

DESCRIPTION:

The first framing method is used in the header to specify the number of characters in frame.
In second method delimit by DLESTX DLEETX.
One way to solve this is to have the senders data link layer, insert an ASCII DLE char just before each accidental flag bit character in data.

FOR EXAMPLE:

‘AB’

Data send by network layer is A B X.
Data after being character stuffing by data link layer DLE STX A DLE ETX.
After destuffing we get original data.

i.e., AB \0

ALGORITHM:

- First read the bit stream clearly.

- Scan for data stream for the continuous 6 is if it is form the in stream bit delimiter.

- In character stuffing also read the given frame clearly.

- Insert DLESTX, data link escaping character starting of the frame.

- If DLE occurs then in the frame insert another DLE after DLE.

- At the end of frame insert DLEETX data link escaping character ending text.

- To destuff the above stuffed frame use simply remove appended DLESTX, DLE and DLEETX.

                                                       BIT STUFFING:
DESCRIPTION:

Starting and ending flags with bit stuffing is one of the framing methods to specify the number of characters or bits in a frame.
Each frame in this techniques begins and ends with a special bit pattern 01111110 called a flag byte.
Whenever the sender’s data link layer encodes five consecutive ones in the data, it automatically stuffs a 0 bit into the outgoing bit stream.

FOR EXAMPLE:


Original data
011011111111111111110010

Data as they appear on the line
01101111101111101111100010

Stuffed Bits

Data stored in the receiver’s memory after destuffing
011011111111111111110010

C CODE FOR BIT STUFFING: 

#include<stdio.h>
#include<conio.h>
void ins(char,int);
void del(int);
char fr[50];
main()
{
int i,cnt=0;
clrscr();
printf("Enter the frame:\t");
gets(fr);
for(i=0;i<strlen(fr);i++)
{
if(fr[i]!='0'&&fr[i]!='1')
{
printf("ivalid input\n press any key to exit");
getch();
exit(0);
}
}
/*Stuffing the given bit*/
for(i=0;i<strlen(fr);i++)
{
if(cnt==5)
{
cnt=0;
ins('0',i);
}
if(fr[i]=='1'){cnt+=1;}else{cnt=0;}
}
printf("\n\nStuffed Frame is \t%s\n\n",fr);
getch();
/*Destuffing of aframe*/
cnt=0;
for(i=0;i<strlen(fr);i++)
{
if(cnt==5)
{
cnt=0;
del(i);
}
if(fr[i]=='1'){cnt+=1;}else{cnt=0;}
}
printf("Destuffed Frame is \t%s",fr);
getch();
return 0;
}
void ins(char in,int p)
{
char dup[50];
int i;
strcpy(dup,fr);
fr[p]=in;
for(i=p+1;i<strlen(fr)+1;i++)
{
fr[i]=dup[i-1];
}
}
void del(int q)
{
int i;
for(i=q;i<strlen(fr);i++)
fr[i]=fr[i+1]; 
}

No comments:

Post a Comment