CCN - Part-B Programs
CCN - Part-B Programs
i) Bit-stuffing
#include<string.h>
#include<stdio.h>
#include<conio.h>
void main()
{
char ch, array[50]={"01111110"},read_array[50];
int counter=0, i=8,j,k;
clrscr();
printf("\n enter the original data stream for bit stuffing\t");
while ((ch=getche())!='\r')
{
if(ch=='1')
++counter;
else
counter=0;
array[i++]=ch;
if(counter==5)
{
array[i++]='0';
counter=0;
}
}
strcat(array,"01111110");
printf("\n the stuffed data stream:\t");
for(j=0;j<i+8;++j)
printf("%c",array[j]);
//DESTUFFING
counter=0;
printf("\n The destuffed data stream is: \t");
for(j=8,k=0;j<i+8;++j)
{
if (array[j]=='1')
++counter;
else
counter=0;
read_array[k++]=array[j];
if((counter==5)&&(array[j+1]=='0'))
{
j++;
counter=0;
}
}
for(j=0;j<k-strlen("01111110");++j)
printf("%c",read_array[j]);
getch();
}
ii) Character stuffing
void main()
{
char ch;
char array[100] = {DLE,STX};
int i=2, j;
clrscr();
printf("\n enter the data stream(ctrl+b->STX,ctrl+c-
>ETX,ctrl+p->DLE):");
while ((ch=getche())!='\r')
{
printf("%c",ch);
getche();
if(ch==DLE)
{
array[i++] = DLE;
printf("DLE");
}
else if(ch==STX)
{
printf("STX");
}
else if (ch==ETX)
{
printf("ETX");
}
else
{
printf("%c",ch);
}
array[i++]=ch;
}
array[i++]=DLE;
array[i++]=ETX;
printf("\n the character stuffed data stream is:");
for(j=0;j<i;++j)
{
if(array[j]==DLE)
{
printf("DLE");
}
else if(array[j]==STX)
{
printf("STX");
}
else if(array[j]==ETX)
{
printf("ETX");
}
else
{
printf("%c", array[j]);
}
}
/* Character Destuffing */
printf("\n The Character destuffed data stream is:");
for(j=2;j<i-2;++j)
{
if(array[j] == DLE)
{
printf("DLE");
++j;
}
else if(array[j] == STX)
{
printf("STX");
}
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int dmat[20][20];
int n,i,j,k,count=0;
clrscr();
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
printf("\nEnter the cost matrix :\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&dmat[i][j]);
dmat[i][i]=0;
rt[i].dist[j]=dmat[i][j];
rt[i].from[j]=j;
}
do
{
count=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j])
{
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0);
for(i=0;i<n;i++)
{
printf("\n\nState value for router %d is \n",i+1);
for(j=0;j<n;j++)
{
printf("\t\nnode %d via %d
Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
return 0;
}
Experiment – 3
Implement Dijkstra’s algorithm to compute the shortest routing path.
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
int main()
{
int G[MAX][MAX],i,j,n,u;
clrscr();
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
return 0;
}
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}
Experiment – 4
For the given data, use CRC-CCITT polynomial to obtain CRC code.
//CRC CODE
#include<stdio.h>
#define DEGREE 16
#define MOD2ADD(x,y)(x==y? 0:1)
//globals
int result[30];
//Function prototype
void main()
{
int array[30],ch;
int length,i = 0;
clrscr();
printf("Enter the data stream:");
while((ch = getche()) !='\r')
{
array[i++] = ch-'0';
}
length = i;
// Append zeros
for(i=0;i<DEGREE;i++)
{
array[i+length] = 0;
}
length+= DEGREE;
for(i=0;i<length;i++)
{
result[i] = array[i];
}
CRC(length);
printf("\n The transmitted frame is:");
for(i=0;i<length-DEGREE;++i)
{
printf("%d",array[i]);
}
for(i=length-DEGREE;i<length;++i)
{
printf("%d",result[i]);
}
printf("\n Enter the stream for which CRC has to be
checked:");
i = 0;
while((ch = getche()) != '\r')
{
array[i++] = ch-'0';
}
length = i;
for(i=0;i<length;i++)
{
result[i] = array[i];
}
CRC(length);
printf("\n CHECKSUM:");
for(i=length-DEGREE;i<length;++i)
{
printf("%d",result[i]);
}
//getchar()
}
{
pos = newpos-1;
}
++pos;
}
}
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int i,j,noframes,x,x1,x2;
clrscr();
for(i=0;i<2;i++)
rand();
noframes=10;
i=1;
j=1;
noframes=noframes/8;
printf("number of frames is %d ",noframes);
scanf("%d",&noframes);
//getch();
while(noframes>0)
{
printf("\nsending frames is %d",i);
srand(x1++);
x=rand()%15;
if(x%5==0)
{
for(x2=1;x2<2;x2++)
{
printf("\n waiting for %d seconds\n",x2);
sleep(x2);
}
printf("\n sending frames %d\n",i);
srand(x1++);
x=rand()%10;
}
printf("\n ack for frame %d\n",j);
noframes=noframes-1;
i++;
j++;
}
printf("\n end of stop and wait protocol\n");
}
ii) Sliding Window Protocol
#include<stdio.h>
int main()
{
int w,i,f,frames[50];
clrscr();
printf("Enter window size:");
scanf("%d",&w);
printf("\nEnter number of frames to transmit:");
scanf("%d",&f);
printf("\nEnter %d frames:",f);
for(i=1;i<=f;i++)
scanf("%d",&frames[i]);
printf("\nWith sliding window protocol the frames will be sent in
the following manner (assuming no corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for
acknowledgement sent by the receiver\n\n",w);
for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by
sender\n\n");
}
else
printf("%d ",frames[i]);
}
if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by
sender\n");
return 0;
}
Experiment – 6
Write a program for congestion control using leaky bucket algorithm
#include<stdio.h>
#include<stdlib.h>
#define MIN(x,y) (x>y)?y:x
int main()
{
int orate,drop=0,cap,x,count=0,inp[10]={0},i=0,nsec,ch;
clrscr();
printf("\n enter bucket size : ");
scanf("%d",&cap);
printf("\n enter output rate :");
scanf("%d",&orate);
do
{
printf("\n enter number of packets coming at second %d :",i+1);
scanf("%d",&inp[i]);
i++;
printf("\n enter 1 to contiue or 0 to quit..........");
scanf("%d",&ch);
}
while(ch);
nsec=i;
printf("\n second \t recieved \t sent \t dropped \t remained \n");
for(i=0;count || i<nsec;i++)
{
printf(" %d",i+1);
printf(" \t%d\t ",inp[i]);
printf(" \t %d\t ",MIN((inp[i]+count),orate));
if((x=inp[i]+count-orate)>0)
{
if(x>cap)
{
count=cap;
drop=x-cap;
}
else
{
count=x;
drop=0;
}
}
else
{
drop=0;
count=0;
}
printf(" \t %d \t %d \n",drop,count);
}
return 0;
}