Professional Documents
Culture Documents
#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";
//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
// 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];
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";
//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';
// end of stuffing
di = strlen(dest) - strlen(flag2);
dest[di] = '\0'; // to remove trailing flag
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
}
CRC
#include <stdio.h>
#define degree 4
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;
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];
for(i=0;i<len-degree;i++)
printf("%d",frm[i]);
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]);
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);
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;
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("\n");
}
} //end of main
LEAKY BUCKET
#include<stdio.h>
#include<stdlib.h>
#include<ctime>
#include<time.h>
#define bucketSize 512
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;
}
}
SELECTIVE REPEAT ALGORITHM
#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++)
{
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
} // 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));
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");
} //main end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%