0% found this document useful (0 votes)
71 views

CCN - Part-B Programs

The document contains 5 experiments related to computer networking concepts: 1. The first experiment implements bit-stuffing and destuffing to transmit data over a communication channel. 2. The second experiment implements character stuffing to encode special characters during data transmission. 3. The third experiment uses the distance vector algorithm to find optimal paths between nodes in a network. 4. The fourth experiment implements CRC checksum to detect errors in data transmission. 5. The fifth experiment implements the Stop-and-Wait and Sliding Window protocols for reliable data transmission.

Uploaded by

Suhas h r
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

CCN - Part-B Programs

The document contains 5 experiments related to computer networking concepts: 1. The first experiment implements bit-stuffing and destuffing to transmit data over a communication channel. 2. The second experiment implements character stuffing to encode special characters during data transmission. 3. The third experiment uses the distance vector algorithm to find optimal paths between nodes in a network. 4. The fourth experiment implements CRC checksum to detect errors in data transmission. 5. The fifth experiment implements the Stop-and-Wait and Sliding Window protocols for reliable data transmission.

Uploaded by

Suhas h r
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Experiment – 1

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

/* Prog 1B: Character Stuffing */


#include<stdio.h>
#define DLE 16
#define STX 2
#define ETX 3
#define MAX 100

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");
}

else if(array[j] == ETX)


{
printf("ETX");
}
else
{
printf("%c",array[j]);
}
}
getch();
}
Experiment – 2
Write a program for distance vector algorithm to find suitable path for transmission

#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

void dijkstra(int G[MAX][MAX],int n,int startnode);

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]);

printf("\nEnter the starting node:");


scanf("%d",&u);
dijkstra(G,n,u);

return 0;
}

void dijkstra(int G[MAX][MAX],int n,int startnode)


{

int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;

//pred[] stores the predecessor of each node


//count gives the number of nodes seen so far
//create the cost matrix
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];

//initialize pred[],distance[] and visited[]


for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}

distance[startnode]=0;
visited[startnode]=1;
count=1;

while(count<n-1)
{
mindistance=INFINITY;

//nextnode gives the node at minimum distance


for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}

//check if a better path exists through nextnode


visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])

if(mindistance+cost[nextnode][i]<distance[i])
{

distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}

//print the path and distance of each node


for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);

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 CRC(int length);


int getnext(int*array,int pos);

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;

// Duplicate the input data

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()
}

void CRC(int length)


{
int ccitt[] = {1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};
int i=0,pos=0,newpos;
while(pos<length-DEGREE)
{
for(i=pos;i<pos+DEGREE+1;++i)
{
result[i] = MOD2ADD(result[i],ccitt[i-pos]);
}
newpos = getnext(result,pos);
if(newpos>pos+1)

{
pos = newpos-1;
}
++pos;
}
}

int getnext(int*array,int pos)


{
int i=pos;
while(array[i] == 0)
{
++i;
}
return i;
}
Experiment – 5
Implementation of Stop and Wait Protocol and Sliding Window Protocol

i) Stop and Wait Protocol

#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;
}

You might also like