You are on page 1of 15

BIT STUFFING AND DESTUFFING

#include <stdio.h>
#include <string.h>
#define MAX 1000

int main()
{
int si = 0,
di = 0,
count = 0; /* si = source index, di = destination index*/
char str[MAX], dest[MAX];
char flag[MAX] = "01111110";

printf("Enter the user data in 1's & 0's :");


scanf("%s", str);

di=strlen(flag); //get the string length of 'flag'


strcpy(dest,flag); //copy 'flag' to 'dest'

//start of stuffing

while(str[si] != '\0')
{
if(str[si] == '1')
count++;
else
count = 0; // restart the counter

dest[di++] = str[si++];

if(count == 5)
{
count = 0; // restart the counter
dest[di++] = '0'; //bit stuffed
}
} // end of while loop

dest[di] = '\0'; //add null char to the end of 'dest'


strcat(dest, flag); //concatenate 'dest' & 'flag'

printf("Stuffed message is :%s", dest);

// end of stuffing
di = strlen(dest) - strlen(flag);
dest[di] = '\0'; // to remove trailing flag and to show the end of string

// to remove flags
for(di = strlen(flag), si = 0; dest[di]!='\0'; si++, di++)
str[si] = dest[di];

str[si] = '\0'; //add null char to the end of 'str'


//start of bit destuffing
si = 0, di = 0, count = 0;

while(str[si] != '\0')
{
if(str[si] == '1')
count++;
else
count = 0; // restart the counter

dest[di++] = str[si++];

if(count == 5)
{
count = 0; // restart the counter
si++; //skip the bit '0' that was stuffed
}
} // end of while loop

dest[di] = '\0';
//end of destuffing
printf("\nDestuffed message is :%s", dest);

………………………………………………………………………………………………………………………………………………………………………………………………………………..
CHARACTER STUFFING AND DESTUFFING
#include <stdio.h>
#include <string.h>
#define MAX 1000

int main()
{
int si = 0, di = 0; /* si = source index, di = destination index*/
char src[MAX], dest[MAX];
char flag1[] = "DLESTX", flag2[] = "DLEETX";

clrscr(); //clear the screen


printf("Enter the STRING data : ");
scanf("%s", src);

di=strlen(flag1); //get the string length of 'flag1'


strcpy(dest,flag1); //copy 'flag1' to 'dest'

//start of stuffing
while(src[si] != '\0')
{
if(src[si] == 'D' && src[si+1] == 'L' && src[si+2] == 'E')
{
dest[di] = 'D', dest[di+1] = 'L', dest[di+2] = 'E', dest[di+3] = 'D',
dest[di+4] = 'L', dest[di+5] = 'E';

di +=6; // update dest index


si+=3; // update source index
}
else
dest[di++] = src[si++];
} // end of while loop

dest[di] = '\0'; //add null char to the end of 'dest'


strcat(dest, flag2); //concatenate 'dest' & 'flag2'

printf("Stuffed message is : %s", dest);

// end of stuffing
di = strlen(dest) - strlen(flag2);
dest[di] = '\0'; // to remove trailing flag

for(di = strlen(flag1), si = 0; dest[di]!='\0'; si++, di++)


src[si] = dest[di];

src[si] = '\0'; //add null char to the end of 'src'


//start of destuffing
si = 0, di = 0;

while(src[si] != '\0')
{
if(src[si] == 'D' && src[si+1] == 'L' && src[si+2] == 'E')
{
dest[di] = 'D', dest[di+1] = 'L', dest[di+2] = 'E';
si +=6; // update source index by skipping the three positions of DLE
di+=3; // update dest index
}
else
dest[di++] = src[si++];
} // end of while loop

dest[di] = '\0';

//end of destuffing

printf("\nDestuffed message is : %s", dest);

}
CRC
#include <stdio.h>
#define degree 4

int frm[30]; //frame size of max 30 bits including data+crc

// function to remove leading zeroes in the frame


int getnext(int frm[], int pos)
{
int i=pos;
while(frm[i]==0)
++i; //count of leading zero's in res[]
return i;
}

// function to perform xor operation and find the CRC


void crc(int len)
{
//int cp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1}; // for degree 16
int cp[]={1,0,1,1,1}; // for degree 4
int i=0, pos=0, newpos;

while(pos < len - degree)


{
for(i=pos; i<pos+degree+1; i++)
frm[i]=(frm[i]==cp[i-pos])?0:1; // xor conditional operator

newpos=getnext(frm,pos);
if(newpos>pos+1)
pos=newpos-1;
++pos;
}
}

//main function
int main()
{
int array[30];
int len, i=0, j=0;

printf("enter the length of data stream");


scanf("%d",&len);

//to input the data


printf("\nEnter the data stream press enter key after entering each bit\n\n : ");

for(i=0;i<len;i++)
scanf("%d",&array[i]);
printf("\n");

for(i=0;i<len;i++)
printf("\n%d",array[i]);

printf("\n");

//appending zeroes
for(i=0;i<degree;++i)
array[i+len]=0;
len += degree;
//duplicating the data from array to frm
for(i=0;i<len;i++)
frm[i]=array[i];

printf("\nTransmitted Frame: ");

for(i=0;i<len-degree;i++)
printf("%d",frm[i]);

crc(len); //calculate CRC

for(i=len-degree;i<len;++i)
printf("%d",frm[i]);

printf("\nEnter receiver data press enter key after entering each bit---\n\n \n");

for(i=0;i<len;i++)
scanf("%d",&array[i]);

//duplicate the array


for(i=0;i<len;i++)
frm[i]=array[i];

crc(len);
printf("\nChecksum : ");
for(i=len-degree;i<len;++i)
printf("%d",frm[i]);

for(i=len-degree;i<len;i++)
if(frm[i]==1)
++j; //count of 1's in the checksum

if(j==0)
printf("\n\n\nNO ERROR in the Frame!!");
else
printf("\n\n\nERROR in the Frame!!");

}
DIJKSTAR ALGORITHM

#include <stdio.h>
#include <conio.h> //for getch()
int p[10][10];

int main()
{
int i,j,k,n,t;
int m[10][10];
void path(int i,int j);

//to input the data


printf("\nEnter the no of nodes : ");
scanf("%d",&n);
printf("Enter node connection matrix");
printf("\n To indicate no connection b/w two nodes enter weight as 100 \n\n");

for(i=1;i<=n;i++)
printf("\t%d",i); // print column nos.

for(i=1;i<=n;i++)
{
printf("\n%d\t",i); // print row nos.
for(j=1;j<=n;j++)
{
scanf("%d",&m[i][j]); // accept cost matrix from the user
p[i][j]=0; // initialize path matrix
}
}

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

for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(m[i][k]+m[k][j]<m[i][j])
{
m[i][j]=m[i][k]+m[k][j]; // update cost matrix
p[i][j]=k; // update path matrix
}

do
{
printf("\nEnter the source & destination nodes :");
scanf("%d %d",&i,&j);
printf("\nThe weight is = %d",m[i][j]);
printf("\nThe path is :\n");
printf("\n %d ---> ",i);
path(i,j) ;
printf("%d",j);
printf("\nTo Repeat press r");
}
while (getch()=='r');
} //end of main
void path(int i,int j)
{
int k;
k=p[i][j];
if(k!=0)
{
path(i,k);
printf("%d ---> ",k);
path(k,j);
}
}

-----------------------------------------------------------------------------------
DISTANCE VECTOR

#include <stdio.h>

int main()
{
int next_hop[20][20];
int cost[20][20], n;
int i,j,k;

printf( "Enter no of nodes.\n" );


scanf("%d",&n);
printf( "Enter the distance(cost or weight)matrix:\n");
printf("enter zero for same node connection and 100 for no direct connection\n");

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


printf("\t%d",i); // print column nos.

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


{
printf("\n%d\t",i); // print row nos.

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


{
scanf("%d",&cost[i][j]); // accept cost matrix from user
next_hop[i][j]=j; // initialize the next hop matrix
}

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


{
for ( i = 1; i <=n; i++)
{
for ( j = 1; j <=n; j++)
{
if(cost[i][j]>cost[i][k]+cost[k][j])
{
cost[i][j]= (cost[i][k]+cost[k][j]); // update cost matrix
next_hop[i][j]=k; // update next hop matrix
}
}
}
}

//print cost matrix

printf(" cost matrix: \n");


for ( i = 1; i <=n; i++)
printf("\t%d",i);

for(i=1;i<=n;i++)
{
printf("\n%d\t",i);
for ( j = 1; j <= n; j++)
{
printf("%d\t",cost[i][j]);
}
printf("\n");
}
//print next hop matrix

printf(" Next hop matrix: \n");


for ( i = 1; i <=n; i++)
{
printf("\t%d",i);
for(i=1;i<=n;i++)
{
printf("\n%d\t",i);
for ( j = 1; j <= n; j++)
{
printf("%d\t",next_hop[i][j]);

}
printf("\n");
}

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


{
printf( "Routing table info for router:%d \n", i );
printf("Dest\tNext Hop\tDist\n" );

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


printf("%d\t%d\t\t%d\n", j,next_hop[i][j], cost[i][j]);
}

} //end of main
LEAKY BUCKET
#include<stdio.h>
#include<stdlib.h>
#include<ctime>
#include<time.h>
#define bucketSize 512

void bktInput(int p_size,int oprate) // p_size = packet size , oprate=output rate


{

if(p_size>bucketSize)
printf("\n\t\tBucket overflow");

else
{
while(p_size>oprate)
{
printf("\n\t\t%d",oprate);
printf(" bytes passed.");
p_size=p_size-oprate;
}
if (p_size>0)
printf("\n\t\tLast %d ",p_size);printf(" bytes sent\t");
printf("\n\t\tBucket output successful");
}
}

// Main function
int main()
{
int i, op_rate, pktSize;

srand(time(0)); //for randomizing the process, this function is used

printf("Enter output rate : ");


scanf("%d",&op_rate);
for( i=1;i<=5;i++)
{

pktSize=rand()%600; // greater than bucketsize


printf("\nPacket no %d",i);
printf("\tPacket size = %d",pktSize);
bktInput(pktSize,op_rate);

}
}
SELECTIVE REPEAT ALGORITHM

//Selective repeat ARQ algorithm in selective repeat algorithm window size is


no.of frames/2

// This program is implemented to transmit single window

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<unistd.h> //For sleep()
#include<math.h>

int main()
{
int i,s,j,no_of_frame,no_of_lost_frames;
int choice;
int wsize; //window size
int Lost_frames[100];
int rn; //for Random number
printf("enter no.frames to be transmitted\n ");
printf("enter even number as the no. of frames:\n");
scanf("%d",&no_of_frame);

wsize=no_of_frame/2;

//start:
printf("window size is %d\n",wsize);
printf("...............................\n");
printf("%d frames arrived from network layer \n",no_of_frame);
printf("outof %d frames %d frames are transmitted in one
window\n",no_of_frame,wsize);
printf("Frames transmitted with sequence no.\n\n");
srand(time(0)); //randomize the process at the start of execution

for (i=0,s=0;i<wsize;i++)
{

printf("frame %d is transmitted with sequence no. %d\n",i,i);


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

sleep(2);//sleep for s seconds


rn=rand()%3;//rn=1-->postive ack,rn=0-->timeout for ack ,rn=2 -->frames lost in N/W

if(rn==1)
{
printf("<---------------------------------------------------------------------\n");
printf("acknoweldgemet for frame%d is received; send frame%d \n",i,i+1);

sleep(1);
printf("\n");

}
else if(rn==0)
{
printf("Timeout : retransmit frame%d\n",i);
printf("*********************\n\n\n\n");
sleep(1);
printf("frame %d is retransmitted because of timeout \n\n",i);
sleep(2);
printf("ack for frame %d sent; transmit next frame %d\n",i,i+1);
printf("<-----------------------------------------------------------------\n\n\n");

else
{
Lost_frames[s++]=i;
}
} //end of for

no_of_lost_frames=s;
printf("\n\nThe total no. of lost frames while transmission:
%d\n\n",no_of_lost_frames);

if(no_of_lost_frames!=0)
{
printf("checking for arrival of ack for frames in sent window \n");
int k;

for (k=0;k<no_of_lost_frames;k++)
{
printf(" frame %d ACKNOWLEDGEMENT is not received \n",Lost_frames[k]);
sleep(1);
}

for (k=0;k<no_of_lost_frames;k++)
{
printf("retransmitted frame %d succesfully\n",Lost_frames[k]);
printf("--------------------------------------------------------------------->\n");
sleep(3);
printf("ack is received for frame %d\n\n",Lost_frames[k]);
printf("<---------------------------------------------------------------------\n");
sleep(1);
}

}//end of if

printf("\n\n\nAll frames are transmitted in one window\n");


printf("\n\n\nNext, the frame no. in the next set, frame %d, will be transmitted by
the sliding window.\n",i);

} // end main
STOP AND WAIT PROTOCOL
// let window size = 1

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h> //for sleep()
#include<time.h>

int main()
{
int n,i,rn;
int seq[50]={1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1};
srand(time(0));
printf("enter the no of frames to be transmitted select within 30\n");
scanf("%d",&n);
srand(time(0));

// rn=0 ack lost in the network


//rn=1 proper ack
//rn=2 time out ; retransmission of same frame
// rn=3 frame lost in the network

for (i=1;i<=n;i++)
{
srand(time(0)); // to randomize the process
printf(" \tTRANSMITTER :-frame %d is transmitted \n \n",seq[i]);
sleep(4);

rn=rand()%4;

if(rn==0)
{
printf("\tRECEIVER:-No ack for FRAME %d \n\n",seq[i]);
printf("\tTRANSMITTER :-Frame %d \n\n",seq[i]);
sleep(5);
printf("\tRECEIVER:-- Ack %d \n\n",seq[i+1]);
printf("--------------------------------------------------------------------\n\n");

else if(rn==1)
{
printf("\tRECEIVER:--Ack %d \n\n",seq[i+1]);
printf("-------------------------------------------------------------------\n\n");
}

else if(rn==2)
{
printf("\tTRANSMITTER:-Time out frame %d \n\n",seq[i]);
sleep(5);
printf("--------------------------------------------------------------------\n\n");
printf("\tTRANSMITTER :-Frame %d retransmitted \n\n",seq[i]);
printf("\tRECEIVER:--Ack%d \n\n",seq[i+1]);

printf("--------------------------------------------------------------------\n\n");
}
else
{
printf("\tFRAME %d is lost in the network \n\n",seq[i]);
printf("\tTRANSMITTER :-Frame %d retransmitted \n\n",seq[i]);
sleep(5);
printf("\tRECEIVER:-- Ack %d \n\n",seq[i+1]);
printf("--------------------------------------------------------------------\n\n");

printf("%d frames transmitted successfully\n",i-1);

} //main end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

You might also like