Sie sind auf Seite 1von 5

1. Write a program for a HLDC frame to perform the following.

i) Bit stuffing
Description:

 The new technique allows data frames to contain an arbitrary number if bits and allows
character codes with an arbitrary no of bits per character.
 Each frame begins and ends with special bit pattern, 01111110, called a flag byte.
 Whenever the sender’s data link layer encounters five consecutive one’s in the data, it
automatically stuffs a 0 bit into the outgoing bit stream.
 This bit stuffing is analogous to character stuffing, in which a DLE is stuffed into the
outgoing character stream before DLE in the data

Source code:

#include<stdio.h>
int main()
{
int a[15];
int i,j,k,n,c=0,pos=0;
printf("\n Enter the number of bits");
scanf("%d",&n);
printf("\n Enter the bits");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]==1)
{
c++;
if(c==5)
{
pos=i+1;
c=0;
for(j=n;j>=pos;j--)
{
k=j+1;
a[k]=a[j];
}
a[pos]=0;
n=n+1;
}
}
else
c=0;
}
printf("\n DATA AFTER STUFFING \n");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}

}
ii) Character stuffing.
Description:

 The framing method gets around the problem of resynchronization after an error by having
each frame start with the ASCII character sequence DLE STX and the sequence DLE ETX.
 If the destination ever losses the track of the frame boundaries all it has to do is look for DLE
STX or DLE ETX characters to figure out.
 The data link layer on the receiving end removes the DLE before the data are given to the
network layer.
 This technique is called character stuffing

Source code:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

int main()
{
char c[50],d[50],t[50];
int i,m,j;
printf("enter the number of
characters\n");
scanf("%d",&m);
printf("\n enter the characters\n");
for(i=0;i<m+1;i++)
{
scanf("%c",&c[i]);
}
printf("\n original data\n");
for(i=0;i<m+1;i++)
printf("%c",c[i]);
d[0]='d';
d[1]='l';
d[2]='e';
d[3]='s';
d[4]='t';
d[5]='x';
for(i=0,j=6;i<m+1;i++,j++)
{
if((c[i]=='d'&&c[i+1]=='l'&& c[i+2]=='e'))
{
d[j]='d';
j++;
d[j]='l';
j++;
d[j]='e';
j++;
m=m+3;
}
d[j]=c[i];
}
m=m+6;
m++;
d[m]='d';
m++;
d[m]='l';
m++;
d[m]='e';
m++;
d[m]='e';
m++;
d[m]='t';
m++;
d[m]='x';
m++;
printf("\n\n transmitted data: \n");
for(i=0;i<m;i++)
{
printf("%c",d[i]);
}
for(i=6,j=0;i <m-6;i++,j++)
{
if(d[i]=='d'&&d[i+1]=='l'&&d[i+2]=='e'&&d[i+3]=='d'&&d[i+4]
=='l'&&d[i+5]=='e')
{
i=i+3;
}
t[j]=d[i];
}
printf("\n\nreceived data:");
for(i=0;i<j;i++)
{
printf("%c",t[i]);
}
}
6. Write a program for congestion control using leaky bucket algorithm
Description:

 In Leaky bucket, each host is connected to the network by an interface containing a leaky
bucket that is a finite internal queue.
 If a packet arrives at the queue when it is full, the packet is discarded.

Source code:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int bucket_size;
void bucket_input ( int pkt_sz, int op_rt )
{
if( pkt_sz > bucket_size )
printf(" \n\nBucket overflow\n ");
else
{
sleep(1);
while ( pkt_sz > op_rt )
{
printf(" \n %d bytes outputted ", op_rt );
pkt_sz-= op_rt;
sleep(1);
}
if ( pkt_sz > 0 )
printf(" \nLast %d bytes sent\n", pkt_sz );
printf(" \n Bucket output successful \n" );
}
}
int main()
{
int i, op_rate, packet_size;
printf("\n Enter Bucket Size: " );
scanf( "%d", &bucket_size );
printf(" \n Enter output rate: " );
scanf( "%d", &op_rate );
for( i=1; i<=5; i++ )
{
sleep(1);
packet_size = random()%1000;
printf(" \n Packet number [%d] \t Packet size = %d ", i, packet_size );
bucket_input( packet_size, op_rate );
}
return 0;
}