You are on page 1of 17

BIT STUFFING

AIM:

Implement the data link layer framing methods such as bit stuffing and destuffing.

THEORY:

The third method allows data frames to contain an arbitrary number of bits and allows character
codes with an arbitrary number of bits per character. At the start and end of each frame is a flag
byte consisting of the special bit pattern 01111110 . Whenever the sender's data link layer
encounters five consecutive 1s in the data, it automatically stuffs a zero bit into the outgoing bit
stream. This technique is called bit stuffing. When the receiver sees five consecutive 1s in the
incoming data stream, followed by a zero bit, it automatically destuffs the 0 bit. The boundary
between two frames can be determined by locating the flag pattern.

PROGRAM:

#include<stdio.h>

#include<string.h>

void main()

//decaring the three string arrays for storing the input frame(in str1) ,stuffed frame(in str2) and
destuffed frame(in str3)

char str1[20],str2[20],str3[20],ch;

int i,j=0,m,n,c=0; //initializing the count value as zero

printf("Enter Input Frame:(0's & 1's only)\n");

scanf("%s",str1);

n=strlen(str1); //calculating the input string size.

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

ch=str1[i]; //copying the each character into ch variable

str2[j++]=ch; //copy each character into string2 which consists stuffed string.

m=ch-48; //48 is the ascii equalent value of ‘0’.

//Converting the character value(ch) into interger value by subtracting 48 form ch.

if(m==1)//comparing the character with ‘1’

c++; //if character is ‘1’ then incrementing the count value

else
c=0;//if the character is not a ‘1’. Then count should be zero.

if(c==5)

//if count value equals to 5

str2[j++]='0';

c=0;//After five consecutive 1’s adding the ‘0’ to string2

str2[j]='\0';// end of string str2(string contains ‘\0’ at the end)

printf("After the bit stuffing Frame is:\n");

puts(str2);

//logic for destuffing bits

c=0;

j=0;

n=strlen(str2);//calculating the size of the stuffed string

for(i=0;i<n;i++)//loop will executes n times

ch=str2[i];

str3[j++]=ch;

m=ch-48;

if(m==1)

c++;

else

c=0;

if(c==5)

c=0;

i++; //incrementing the string2 index to remove ‘0’ in destuffing.


}

str3[j]='\0';//end of the string

printf("After the bit destuffing the Frame is\n");

puts(str3);

getch();

OUTPUT:

Enter Input Frame:(0's & 1's only)

101111100011

After the bit stuffing Frame is:

1011111000011

After the bit destuffing the Frame is

101111100011

CHARACTER STUFFING

AIM:

Implement the data link layer framing methods such as character stuffing and destuffing.

THEORY:

each frame starts with the ASCII character sequence DLE STX and ends with the sequence DLE
ETX.(where DLE is Data Link Escape, STX is Start of TeXt and ETX is End of TeXt.) This method
overcomes the drawbacks of the character count method. If the destination ever loses
synchronization, it only has to look for DLE STX and DLE ETX characters. If however, binary data is
being transmitted then there exists a possibility of the characters DLE STX and DLE ETX occurring in
the data. Since this can interfere with the framing, a technique called character stuffing is used. The
sender's data link layer inserts an ASCII DLE character just before the DLE character in the data. The
receiver's data link layer removes this DLE before this data is given to the network layer. However
character stuffing is closely associated with 8-bit characters and this is a major hurdle in transmitting
arbitrary sized characters.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{
int i=0,j=0,n;

char src[20],dest[50],rev[50];

printf("\n Enter Any string\n");

scanf("%s",&src); // src is an array which stores input data

n=strlen(src); // n holds length of the string

//dest is an array which holds the stuffed frame

dest[0]='D';

dest[1]='L';

dest[2]='E';

dest[3]='S';

dest[4]='T';

dest[5]='X';

j=6;//Jth variables will begins from 6th index onwards

//code for stuffing

while(i<n)//to copy to the dest loop execution ends with the consideration of length of the i/p string

if(src[i]=='d' && src[i+1]=='l' && src[i+2]=='e') // checking whether continuously dle occurs or not

dest[j]='d';

dest[j+1]='l';

dest[j+2]='e';

j=j+3;

dest[j]=src[i];// the source copied into dest

i++;

j++;

//insertion of frame end flags followed as

dest[j]='D';

dest[j+1]='L';
dest[j+2]='E';

dest[j+3]='E';

dest[j+4]='T';

dest[j+5]='X';

dest[j+6]='\0';

printf("\n Frame After Stuffing:\n");

printf("%s",dest);

j=0;//again j variable initialized with zero for destuffing

n=strlen(dest);

for(i=6;i<n-6;i++)//to remove flag bits in rev frame loop will starts from 6th location and ends n-6

if(dest[i]=='d'&&dest[i+1]=='l'&&dest[i+2]=='e')

i=i+3;//leave 3 positions to remove flag dle

rev[j++]=dest[i]; //destuffed strings copied into rev

else

rev[j++]=dest[i];//to copy irrespective of flag

rev[j]='\0';

printf("\n After destuffing\n");

printf("%s",rev);

getch();

OUTPUT:

Enter Any string

kmit

Frame After Stuffing:

DLESTXkmitDLEETX

After destuffing
kmit

CRC

AIM:

Implement on a data set of characters the three CRC polynomials – CRC 12, CRC 16 and CRC CCIP .

THEORY:

Solve the problem manually

PROGRAM:

#include<stdio.h>

void main()

int f[150],g[150],r[150],t[150],i,j,k,m,n,c=0;

printf("\nEnter frame size: ");

scanf("%d",&n);

printf("\nEnter frame(dividend): ");

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

scanf("%d",&f[i]);

t[i]=f[i];

printf("\nEnter divisor size: ");

scanf("%d",&m);

printf("\nEnter g(divisor): ");

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

scanf("%d",&g[i]);

for(i=n;i<n+m;i++)

t[i]=0;

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

k=i;
if(t[i]==1)

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

if(t[k]==g[j])

t[k]=0;

else

t[k]=1;

printf("\nCheck sum is : ");

for(i=n;i<m+n-1;i++)

printf(" %d ",t[i]);

f[i]=t[i];

printf("\n\nCodeword : ");

for(i=0;i<m+n-1;i++)

printf(" %d ",f[i]);

printf("\n\nEnter received data : \n");

for(i=0;i<m+n-1;i++)

scanf("%d",&r[i]);

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

k=i;

if(r[i]==1)

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

if(r[k]==g[j])
r[k]=0;

else

r[k]=1;

for(i=n;i<m+n-1;i++)

printf("%d",r[i]);

if(r[i]!=0)

c=1;

if(c==1)

printf("\nError ");

else

printf("\nNO error");

OUTPUT:

CRC – 4

Enter frame size: 4

Enter frame(dividend): 1 1 1 0

Enter divisor size: 5

Enter g(divisor): 1 0 0 1 1

Check sum is : 0 0 0 1

Codeword : 1 1 1 0 0 0 0 1

Enter received data :

11100001

0000

NO error

CRC-8

Enter frame size: 4


Enter frame(dividend): 1 1 1 0

Enter divisor size: 9

Enter g(divisor): 1 0 1 0 0 0 1 0 1

Check sum is : 0 1 1 1 1 0 0 1

Codeword : 1 1 1 0 0 1 1 1 1 0 0 1

Enter received data :

111001111001

00000000

NO error

CRC-12

Enter frame size: 4

Enter frame(dividend): 1 1 1 0

Enter divisor size: 13

Enter g(divisor): 1 1 1 1 0 0 1 0 1 0 1 0 0

Check sum is : 1 1 0 0 1 1 1 1 0 1 0 0

Codeword : 1 1 1 0 1 1 0 0 1 1 1 1 0 1 0 0

Enter received data :

1110110011110100

000000000000

NO error

DVR

AIM:

Take an example subnet graph with weights indicating delay between nodes. Now obtain Routing
table art each node using distance vector routing algorithm

THEORY:

A distance-vector routing protocol in data networks determines the best route for data packets
based on distance. Distance-vector routing protocols measure the distance by the number of routers
a packet has to pass, one router counts as one hop.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#define INFINITY 10000


void main()

int adj[50][50],length[50][50],path[50][50],set[50],i,j,n,s,t,c;

printf("Enter No Of Routers\n");

scanf("%d",&n);

printf("Enter Adjacency Matrix \n");

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

for(j=0;j<n;j++)

scanf("%d",&adj[i][j]);

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

for(j=0;j<n;j++)

if(adj[i][j]==0 && i!=j)

length[i][j]=INFINITY;

path[i][j]=0;

else

length[i][j]=adj[i][j];

path[i][j]=j;

if(i==j)

path[i][j]=131;

t=1;

while(t)

c=0;

for(s=0;s<n;s++)

for(j=0;j<n;j++)
{

if(adj[s][j])

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

if(length[s][j]+length[j][i]<length[s][i])

length[s][i]=length[s][j]+length[j][i];

path[s][i]=j;

for(s=0;s<n;s++)

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

if(length[s][i]==INFINITY)

c++;

if(c==0)

t=0;

else

t=1;

printf("\nRouting table\n\n");

for(i=65;i<(n+65);i++)

printf(" %c ",i);

printf("\n----------------------------------------------------\n");

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

printf(" l p");

printf("\n---------- ----- ------ ------ ------ ------ -------\n");

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

printf("%c",i+65);

for(s=0;s<n;s++)
{

printf(" %3d%3c |",length[s][i],path[s][i]+65);

printf("\n");

getch();

OUTPUT:

Enter No Of Routers

Enter Adjacency Matrix

0 9 0 0 8 10

7 0 6 0 0 15

050204

003050

12 0 0 4 0 12

11 16 4 0 10 0

Routing table

A B C D E F

----------------------------------------------------

l p l p l p l p l p l p

---------- ----- ------ ------ ------ ------ -------

A 0 ─ | 7 A | 12 B | 15 C | 12 A | 11 A |

B 9 B | 0 ─ | 5 B | 8 C | 12 D | 9 C |

C 14 F | 6 C | 0 ─ | 3 C | 7 D | 4 C |

D 12 E | 8 C | 2 D | 0 ─ | 4 D | 6 C |

E 8 E | 15 A | 7 D | 5 E | 0 ─ | 10 E |

F 10 F | 10 C | 4 F | 7 C | 11 D | 0 ─ |
SHORTEST PATH

AIM:

Implement Dijkstra‘s algorithm to compute the Shortest path thru a graph

THEORY:

Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph, which
may represent, for example, road networks. It was conceived by computer scientist Edsger W.
Dijkstra in 1956 and published three years later.
The algorithm exists in many variants; Dijkstra's original variant found the shortest path between
two nodes, but a more common variant fixes a single node as the "source" node and finds shortest
paths from the source to all other nodes in the graph, producing a shortest-path tree.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#define INFINITY 10000

void main()

int gptr[20][20],set[20],path[20],length[20],s,i,j,k,n,m,temp,c;

printf("Enter No Of Vertexes\n");

scanf("%d",&n);

printf("Enter Adjacency Matrix of a Graph \n");

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

scanf("%d",&gptr[i][j]);

printf("Enter Source node \n");

scanf("%d",&s);

for(i=1;i<=n;i++)

set[i]=0;

for(i=1;i<=n;i++)

if(gptr[s][i]==0)
{

length[i]=INFINITY;

path[i]=0;

else

length[i]=gptr[s][i];

path[i]=s;

set[s]=1;

length[s]=0;

temp=1;

while(temp)

c=0;

j=search_min(length,set,n);

set[j]=1;

for(i=1;i<=n;i++)

if(set[i]!=1)

if(gptr[i][j]!=0)

if(length[j]+gptr[i][j]<length[i])

length[i]=length[j]+gptr[i][j];

path[i]=j;

}}}}

for(i=1;i<=n;i++)

{
if(set[i]==0)

c++;

if(c==0)

temp=0;

else

temp=1;

printf("length\tpath\n");

for(i=1;i<=n;i++)

printf("%d\t%d\n",length[i],path[i]);

printf("\n-------------------------------------------------------\n");

printf("\tPath\t\tLength\tShortest path\n");

printf("----------------------\n");

printf("From(sourcevertex) To \n");

printf("--------------------------------------------------------\n");

printf("\t%d\n",s);

for(i=1;i<=n;i++)

if(i!=s)

printf("\t\t%4d\t%2d",i,length[i]);

j=i;

while(j!=s)

printf("\t%d->%d",j,path[j]);

j=path[j];

printf("\n");

getch();

}
search_min(int length[],int set[],int n)

int i,j,v,min=100;

for(i=1;i<=n;i++)

if(set[i]==0)

if(length[i]<min)

min=length[i];

v=i;

return v;

OUTPUT:

Enter No Of Vertexes

Enter Adjacency Matrix of a Graph

01600

10235

62042

03402

05220

Enter Source node

length path

0 0

1 1
3 2

4 2

5 3

-------------------------------------------------------

Path Length Shortest path

----------------------

From(sourcevertex) To

--------------------------------------------------------

2 1 2->1

3 3 3->2 2->1

4 4 4->2 2->1

5 5 5->3 3->2 2->1

You might also like