lab record for computer networks for b.tech students
TRANSCRIPT
Serial NO.
Name OF Practicals Page no.
Remarks
1 Cyclic
RedundencyCheck 1 to 3
2 Dijkstra Algorithm 3 to 8
3 Hamming Code 9 to 11
4 Bit Stuffing 12 to
13
5 Character stuffing 14 to
15
6 Distance VECTOR
Routing Algo 16 to
17
7 RSA Algorithm 18 to
19
/* 1.Program For Cyclic Redundancy
Check: */
#include <stdio.h>
#include <string.h>
#include<conio.h>
char i[81],p[81],r[81];
intcrc(int mode)
{ intj,k;
char g[] = {"1011"};
strcpy(p,i);
if(mode)
strcat(p,"000");
for(j=0;j<strlen(i);j++)
if(p[j] == '1')
for(k=0;k<strlen(g);k++)
if(p[j+k] == g[k])
p[j+k] = '0';
else
p[j+k] = '1';
for(j=0;j<strlen(p);j++)
if(p[j] == '1')
return 1;
return 0;
}
void main()
{ printf("\nEnter No. : ");
scanf("%s",i);
crc(1);
printf("\nThe CRC code is %s%s : ",i,p+strlen(i));
printf("\nEnterrecieved code : ");
scanf("%s",i);
if(!crc(0))
printf("\nNo Error ");
else
printf("\nError");
getch();
}
/**************output screen for CRC ***************/
Enter No. : 1001
The CRC code is 1001110 :
Enter recievedcode : 1001110
No Error
***********************************
Enter No. : 1001
The CRC code is 1001110 :
Enter recievedcode : 1001100
Error
/* SINGLE SOURCE SHORTEST PATH BY DIJKSTRA APPORACH */
#include<stdio.h>
#include<conio.h>
#define max 20
#define temp 0
#define perma 1
int w[max][max],dist[20],pred[20],status[20],current,dc,newdist,s,d;
int n;
main()
{
inti,v,ch,k,j;
clrscr();
cg();
printf("\n weight matrix\n");
display();
printf("\n enter the source vertex\n");
scanf("%d",&s);
printf("\n enter the destination vertex\n");
scanf("%d",&d);
dijkstra(s,d);
printf("\n \t Shortest Path from %d to %d\t\n\n",s,d);
display2();
getch();
}
cg()
{
inti,j,medge,org,des;
printf("enter no. of node: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\n enter edge %d,%d: ",i,j);
scanf("%d",&w[i][j]);
}
}
}
dijkstra(ints,int d)
{
inti,min,k;
for(i=1;i<=n;i++)
{
dist[i]=9999;
pred[i]=-1;
status[i]=temp;
}
status[s]=perma;
dist[s]=0;
current=s;
while(current!=d)
{
dc=dist[current];
for(i=1;i<=n;i++)
{
if((w[current][i]!=0)&&(status[i]==temp))
{
newdist=(dc+(w[current][i]));
if(newdist<dist[i])
{
dist[i]=newdist;
pred[i]=current;
}
}
}
min=9999;
for(i=1;i<=n;i++)
{
if(status[i]==temp&&min>dist[i])
{
min=dist[i];
k=i;
}
}
current=k;
status[k]=perma;
}
}
display()
{
inti,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%3d",w[i][j]);
printf("\n");
}
printf("\n\n");
}
display2()
{
int i;
i=d;
printf("\t\t");
while(i!=s)
{
printf("%d<-",i);
i=pred[i];
}
printf("%d",s);
printf(" \n\n\t\t TOTAL DISTANCE COVERED=%d ",dist[d]);
}
/* OUTPUT SCREEN FOR DIJKSTRA ALGORITHM OUTPUT*/
Enter no. of nodes: 4
enter edge 1,1: 99
enter edge 1,2: 1
enter edge 1,3: 99
enter edge 1,4: 4
enter edge 2,1: 99
enter edge 2,2: 99
enter edge 2,3: 2
enter edge 2,4: 99
enter edge 3,1: 2
enter edge 3,2: 99
enter edge 3,3: 99
enter edge 3,4: 99
enter edge 4,1: 99
Shortest Path from 1 to 3 3<-2<-1
TOTAL DISTANCE COVERED=3
enter edge 4,2: 99
enter edge 4,3: 3
enter edge 4,4: 99
weight matrix
99 1 99 4
99 99 2 99
2 99 99 99
99 99 3 99 ------------------- enter the source vertex 1 enter the destination vertex
3
/* FOR THE CALCULATION OF HAMMING CODE FOR ERROR CHECKING */
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define XOR(x,y) ('0'+(x!=y))
int main()
{
charinp[100], ham[100], rec[100], t;
int m, n, r, i, j, e;
clrscr();
printf("\nEnter the data to be sent(0's & 1's)\n");
scanf("%[10]",inp+1);
m=strlen(inp+1);
for(r=1;m+r>=(1<<r);r++) {}
n=m+r;
for(j=i=1;i<=n;i++)
ham[i] = (i&(i-1))? inp[j++] : '0';
ham[n+1]=0;
for(i=1;i<=n;i<<=1)
for(j=i;j<=n;j++)
if(i&j)
ham[i] = XOR(ham[i], ham[j]);
printf("Transmitted data(Hamming code):\n");
printf("%s\n",ham+1);
printf("\nEnter the data received:\n");
scanf(" %[01]",rec+1);
for(i=1,e=0;i<=n;i<<=1)
{
for(t='0',j=i;j<=n;j++)
if(i&j)
t=XOR(t,rec[j]);
if(t!='0')
e+=i;
}
if(e)
{ printf("Transmission error has occured(at bit %d)\n",e);
rec[e] = rec[e]=='0'? '1' :'0';
}
else
printf("Transmission error has not occured\n");
printf("Actual data sent is:\n");
for(i=1;i<=n;i++)
if(i&(i-1))
putchar(rec[i]);
getch(); return 0;
}
Enter the data to be sent(0's & 1's)
101101
Transmitted data(Hamming code):
0010011101
Enter the data received:
0010011101
Transmission error has not occured
Actual data sent is:
101101
-----------------------------------------
Enter the data to be sent(0's & 1's)
101
Transmitted data(Hamming code):
101101
Enter the data received:
100101
Transmission error has occured(at bit 3)
Actual data sent is:
101
/************* 4. BIT STUFFING PROGRAM *****************/
#include"stdio.h"
#include"conio.h"
main()
{
char a[20],fs[50]=" ",t[6],r[5];
inti,j,p=0,q=0;
clrscr();
printf("enter bit string : ");
scanf("%s",a);
strcat(fs,"01111110");
if(strlen(a)<5)
{
strcat(fs,a);
}
else
{
for(i=0;i<strlen(a);i++) {
for(j=i;j<strlen(a);j++) {
t[p++]=a[j];
}
t[p]='\0';
if(strcmp(t,"11111")==0)
{
strcat(fs,"0111110");
i=j-1;
}
else
{
r[0]=a[i];
r[1]='\0';
strcat(fs,r);
}
p=0;
}
for(q=i;q<strlen(a);q++) {
t[p++]=a[q];
}
t[p]='\0';
strcat(fs,t);
}
strcat(fs,"01111110");
printf("After stuffing : %s",fs);
getch();
}
/************ output screen for bit stuffing*************** */
enter bit string : 01111110
After stuffing : 011111100111111001111110
/*********** 5.CHARACTER STUFFING PROGRAM***********/
#include<stdio.h>
#include<conio.h>
main()
{
char a[30],fs[50]=" ",t[3],sd,ed,x[3],s[3],d[3],y[3];
inti,j,p=0,q=0;
clrscr();
printf("Enter characters to be stuffed : ");
scanf("%s",a);
printf("\nEnter a character that represents starting delimiter : ");
scanf(" %c",&sd);
printf("\nEnter a character that represents ending delimiter : ");
scanf(" %c",&ed);
x[0]=s[0]=s[1]=sd;
x[1]=s[2]='\0';
y[0]=d[0]=d[1]=ed;
d[2]=y[1]='\0';
strcat(fs,x);
for(i=0;i<strlen(a);i++)
{
t[0]=a[i];
t[1]='\0';
if(t[0]==sd)
strcat(fs,s);
else
if(t[0]==ed)
strcat(fs,d);
else
strcat(fs,t);
}
strcat(fs,y);
printf("\nAfter stuffing : %s",fs);
getch();
}
***********CHARACTER STUFFING OUTPUT*****************
Enter characters to be stuffed :goodday
Enter a character that represents starting delimiter : d
/*6. Take an example subnet graph weights indicating delay between nodes.
Now obtain routing table at each node using distance vector routing algorithm*/
#include<stdio.h>
struct node
{
unsigneddist[20];
unsigned from[20];
}rt[10];
int main()
{
intdmat[20][20];
intn,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");
}
/********** 7. Program for RSA algorithm *************/
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{ intp,q,e,d,n,fi,a,M;
long k=1;
intc,i;
clrscr();
printf("\n Enter the plain message");
scanf("%d",&a);
printf("\n Enter the value of p & q respectively");
scanf("%d%d",&p,&q);
n=p*q;
fi=(n-1)*(q-1);
printf("\n Enter the value of e & d");
scanf("%d%d",&e,&d);
for(i=1;i<=e;i++)
{k*=(a%n);
}
c=(k%n);
printf(" after encryption process >> \n");
printf("\n cipher text is:%d",c);
printf(" ----------- decryption ------------- \n");
printf("\n\nEnter the Cipher text\t: ");
scanf("%d",&c);
M = 1;
for(i=0;i<d;i++)
M=M*c%n;
M = M%n;
printf("\n\tDecrypted keyword : %d",M);
getch();
}
/**********output screen for RSA algorithm ************/
Enter the plain message 5
Enter the value of p & q respectively 7 11
Enter the value of e & d 13 37