Sunteți pe pagina 1din 20

Computer Networks Algorithms in C language

Contents:
1. Bit Stuffing and Character Stuffing
2. CRC,CRC-12,CRC-16,CRC-CCIT
3. Dijkstra's Shotest path routing algorithm
4. Distance vector routing algorithm
5. Link state routing algorithm
6. Floding
7.Broadcasting
1.IMPLEMENTATION OF BIT STUFFING AND CHARACTER STUFFING
#include
#include
#include
main()
{
int ch;
clrscr();
do
{
printf("\n********************:");
printf("\n1.BIT STUFFING:");
printf("\n2.CHARACTER STUFFING:");
printf("\n3.EXIT:");
printf("\n*********************");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:bit();
break;
case 2:character();
break;
case 3:exit(0);
}
}while(ch!=3);
getch();
}
bit()
{
FILE *fp,*fp1;
char ch;
int i;
if((fp=fopen("source.txt","w"))==NULL)
{
printf("\nError in opening the file");
exit(0);
}
printf("\nEnter data to send press 'e' to end :\n");
i=0;
while(1)
{
scanf("%c",&ch);
if(ch=='e')
break;
if(ch=='1')
i++;
else
i=0;
putc(ch,fp);
if(i==5)
{
putc('0',fp);
i=0;
}
}
fclose(fp);
i=0;
fp=fopen("source.txt","r");
fp1=fopen("dest.txt","w");
printf("\nDATA AFTER STUFFING\n");
while((ch=getc(fp))!=EOF)
{
putc(ch,stdout);
}
fseek(fp,0L,0);
printf("\nDATA AFTER UNSTUFFING\n");
while((ch=getc(fp))!=EOF)
{
if(ch=='1')
i++;
else
i=0;
if(i==5)
{
putc(ch,stdout);
putc(ch,fp1);
ch=getc(fp);
i=0;
}
else
{
putc(ch,stdout);
putc(ch,fp1);
}
}
fclose(fp);
fclose(fp1);
}
character()
{
FILE *fp,*fp1;
char ch,c[2],k[4],j[9];
long beg,end;
if((fp=fopen("Input.txt","w"))==NULL)
{
printf("\n Input.txt file opening problem...");
exit(0);
}
printf("\nEnter data to send at end put '}': \n\n");
while(1)
{
scanf("%c",&ch);
if(ch=='}')
break;
putc(ch,fp);
}
fclose(fp);
if((fp=fopen("Input.txt","r"))==NULL)
{
printf("\nInput.txt file opening problem...");
exit(0);
}
if((fp1=fopen("csource.txt","w"))==NULL)
{
printf("\ncsource.txt file opening problem...");
exit(0);
}
fputs(" DLE STX ",fp1);
while((ch=getc(fp))!=EOF)
{

if(ch=='D')
{

c[0]=getc(fp);
c[1]=getc(fp);
if(c[0]=='L'&&c[1]=='E')
fputs("DLE",fp1);
putc(ch,fp1);
putc(c[0],fp1);
putc(c[1],fp1);
}
else
putc(ch,fp1);
}
fputs(" DLE ETX ",fp1);
fclose(fp);
fclose(fp1);
if((fp=fopen("csource.txt","r"))==NULL)
{
printf("\ncsource.txt file opening problem...");
exit(0);
}
if((fp1=fopen("cdest.txt","w"))==NULL)
{
printf("\ncdest.txt file opening problem...");
exit(0);
}
beg=ftell(fp);
beg+=9;
fseek(fp,-0L,2);
end=ftell(fp);
end-=9;
fclose(fp);
printf("\nData after stuffing ");
fp=fopen("csource.txt","r");
while((ch=getc(fp))!=EOF)
putc(ch,stdout);
fclose(fp);
printf("\n");
printf("\nThe data after destuffing");
fp=fopen("csource.txt","r");
fgets(j,9,fp);
while(beg<=end)
{
ch=getc(fp);
if(ch=='D')
{
c[0]=getc(fp);
c[1]=getc(fp);
if(c[0]=='L'&&c[1]=='E')
{
fgets(k,4,fp);
beg+=4;
}
else
{
putc(ch,fp1);
putc(c[0],fp1);
putc(c[1],fp1);
putc(ch,stdout);
putc(c[0],stdout);
putc(c[1],stdout);
}
}
else
{
putc(ch,fp1);
putc(ch,stdout);
}
beg++;
}
fclose(fp);
fclose(fp1);
}

OUTPUT

********************:
1.BIT STUFFING:
2.CHARACTER STUFFING:
3.EXIT:
*********************
Enter your choice:1

Enter data to send press 'e' to end :


11100000111111111101e

DATA AFTER STUFFING

1110000011111011111001
DATA AFTER UNSTUFFING

11100000111111111101
********************:
1.BIT STUFFING:
2.CHARACTER STUFFING:
3.EXIT:
*********************
Enter your choice:2
Enter data to send at end put '}':

This is DLE idle DLE program}

Data after stuffing


DLE STX This is DLEDLE idle DLEDLE program DLE ETX

The data after destuffing


This is idle program
********************:
1.BIT STUFFING:
2.CHARACTER STUFFING:
3.EXIT:
*********************
Enter your choice:3

2. IMPLEMENTATION OF DIJKSTRA’S ALGORITHM*/


#include
#include
#define maxnode 10
#define perm 1
#define tent 2
#define infinity INT_MAX
typedef struct nodelabel
{
int predecessor;
int length;
int label;
}nodelabel;
int shortpath(a,n,s,t,path,dist)
int a[maxnode][maxnode],n,s,t,path[maxnode],*dist;
{
nodelabel state[maxnode];
int i,k,min,count,rpath[maxnode];
*dist=0;
for(i=1;i<=n;i++)
{
state[i].predecessor=0;
state[i].length=infinity;
state[i].label=tent;
}
state[s].predecessor=0;
state[s].length=0;
state[s].label=perm;
k=s;
do
{
for(i=1;i<=n;i++)
{
if(a[k][i]>0 && state[i].label==tent)
{
if(state[k].length+a[k][i]
{
state[i].predecessor=k;
state[i].length=state[k].length+a[k][i];
}
}
}
min=infinity;
k=0;
for(i=1;i<=n;i++)
{
if(state[i].label==tent&&state[i].length
{
min=state[i].length;
k=i;
}
}
if(k==0)
return 0;
state[k].label=perm;
}while(k!=t);
k=t;
count=0;
do
{
count++;
rpath[count]=k;
k=state[k].predecessor;
}while(k!=0);
for(i=1;i<=count;i++)
path[i]=rpath[count-i+1];
for(i=1;i
*dist+=a[path[i]][path[i+1]];
return count;
}
void main()
{
int a[maxnode][maxnode],i,j;
int path[maxnode];
int from,to,dist,count,n;
clrscr();
printf("Enter no. of nodes:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter node %d",i);
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
}
printf("\n From to");
scanf("%d %d",&from,&to);
count=shortpath(a,n,from,to,path,&dist);
if(dist)
{
printf("\n Short path");
printf("%d",path[1]);
for(i=2;i<=count;i++)
printf("-->%d",path[i]);
printf("\nMinimum distance is %d \n",dist);
}
else
printf("\n Path does not exists");

getch();
}

OUTPUT

Enter no. of nodes:4


Enter node 1
0340
Enter node 2
0 0 6 20
Enter node 3
0009
Enter node 4
15 0 0 0

From to 2 4

Short path2-->3-->4
Minimum distance is 15

3.DISTANCE VECTOR ROUTING ALGORITHM


#include
#include
#define LIMIT 10
#define INFINITY 10000
int n,m,i,j,mi,dist[LIMIT][LIMIT],a[LIMIT],adj[LIMIT],sn,dn,c=0,x[LIMIT],min;
main()
{
clrscr();
printf("Enter no.of nodes::");
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
dist[i][j]=-1;
for(i=1;i<=n;i++)
{
printf("\nEnter distance for %d node:",i);
for(j=1;j<=n;j++)
if(dist[i][j]==-1)
{
if(i==j)
{
dist[i][j]=0;
printf("\n 0");
}
else
{
scanf("%d",&m);
dist[i][j]=dist[j][i]=m;
}
}
else
printf("\n%d",dist[i][j]);
}
printf("\ndistance matrix is:");
for(i=1;i<=n;i++)
{
printf("\n");
for(j=1;j<=n;j++)
printf("%d\t",dist[i][j]);
}
printf("\nnEnter the sorce node :");
scanf("%d",&sn);
printf("Enter the distance node:");
scanf("%d",&dn);
j=1;m=0;
for(i=1;i<=n;i++)
{
if(dist[sn][i]>0)
{
m++;
adj[j++]=i;
}
}
min=0;
mi=INFINITY;
for(i=1;i<=m;i++)
{
printf("\nVector table for %d to all nodes:",adj[i]);
for(j=1;j<=n;j++)
{
if(adj[i]==j)
x[j]=0;
else
x[j]=spath(adj[i],j,min);
if(j==dn)
if(x[j]
{
mi=x[j];
c=adj[i];
}
printf("\n%d",x[j]);
}
}
for(i=1;i<=n;i++)
{
if(i==sn)
a[i]=0;
else
if(i==dn)
a[i]=dist[sn][c]+mi;
else
a[i]=spath(sn,i,min);
}
printf("\nNew vector table for %d node:",sn);
for(i=1;i<=n;i++)
printf("\n%d",a[i]);
getch();
}

int spath(int s,int t,int min)


{
struct state
{
int pred;
int len;
enum{permanent,tentative}label;
}state[LIMIT];
int i=1,k;
struct state *p;
for(p=&state[i];p<=&state[n];p++)
{
p->pred=0;
p->len=INFINITY;
p->label=tentative;
}
state[t].len=0;
state[t].label=permanent;
k=t;
do
{
for(i=1;i<=n;i++)
if(dist[k][i]!=0 && state[i].label==tentative)
{
if(state[k].len+dist[k][i]
{
state[i].pred=k;
state[i].len=state[k].len+dist[k][i];
}
}
k=0;
min=INFINITY;
for(i=1;i<=n;i++)
if(state[i].label==tentative && state[i].len
{
min=state[i].len;
k=i;
}
state[k].label=permanent;
} while(k!=s);
return(min);
}

output
Enter no.of nodes::4

Enter distance for 1 node:


0
1
2
0

Enter distance for 2 node:


1
0
0
4

Enter distance for 3 node:


2
0
0
8
distance matrix is:
0120
1004
2008
0480
Enter the source node :3
Enter the distance node:2

Vector table for 1 to all nodes:


0
1
2
5
Vector table for 4 to all nodes:
5
4
7
0
New vector table for 3 node:
2
3
0
7

4.LINK STATE ROUTING ALGORITHM


#include
#include
#define LIMIT 10
#define INFINITY 10000
int m,n,k,i,j,dist[LIMIT][LIMIT],sn,dn,min=0;
struct node
{
int hello[LIMIT],from,adj[LIMIT];
}node[LIMIT];
main()
{
clrscr();
printf("Enter how many nodes do u want::");
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
dist[i][j]=-1;
for(i=1;i<=n;i++)
{
printf("\nEnter distance for %d node:",i);
for(j=1;j<=n;j++)
if(dist[i][j]==-1)
if(i==j)
{
dist[i][j]=0;
printf(" 0");
}
else
{
scanf("%d",&m);
dist[i][j]=dist[j][i]=m;
}
else
printf(" %d",dist[i][j]);
}
printf("\nDistance matrix is:");
for(i=1;i<=n;i++)
{
printf("\n");
node[i].from=0;
for(j=1;j<=n;j++)
printf("\t%d",dist[i][j]);
}
printf("\nSENDING HELLO PACKETS");
for(i=1;i<=n;i++)
{
k=1;
for(j=1;j<=n;j++)
{
if(dist[i][j]>0)
node[i].from++;
if(dist[j][i]>0)
node[i].hello[k++]=j;
}
}
for(i=1;i<=n;i++)
{
printf("\nHello packets to node %d:",i);
for(j=1;j<=node[i].from;j++)
printf("\n%d",node[i].hello[j]);
}
printf("\nSENDING ECHO PACKETS");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
if(dist[i][j]>0)
printf("\nfrom %d to %d, the distance is:%d",i,j,dist[i][j]);
}
printf("\n CONSTRUCTION LINKSTATE PACKETS");
for(i=1;i<=n;i++)
{
printf("\nLINK STATE PACKET FOR %d",i);
printf("\n|-------------|");
printf("\n| %d |",i);
printf("\n|-------------|");
printf("\n| seq |");
printf("\n|-------------|");
printf("\n| Age |");
printf("\n|-------------|");
for(j=1;j<=n;j++)
if(dist[i][j]>0)
{
printf("\n| %d | %d |",j,dist[i][j]);
printf("\n|-------------|");
}

}
printf("\nDISTRIBUTING THE LINK STATE PACKETS");
for(i=1;i<=n;i++)
{
printf("\nNeighbours for %d node are:",i);
for(j=1;j<=n;j++)
{
if(dist[i][j]>0)
printf("%d",j);
}
for(j=1;j<=node[i].from;j++)
{
printf("\n the distance from %d to ",i);
printf("%d is %d",node[i].hello[j],dist[i][node[i].hello[j]]);
}
}
printf("\nCOMPUTING THE SHORTEST PATH");
printf("\nEnter source and destination:");
scanf("%d%d",&sn,&dn);
min=spath(sn,dn,min);
printf("\n minimum distance:%d",min);
getch();
}
int spath(int s,int t,int min)
{
struct state
{
int pred;
int len;
enum{permanent,tentative}label;
}state[LIMIT];
int i=1,k;
struct state *p;
for(p=&state[i];p<=&state[n];p++)
{
p->pred=0;
p->len=INFINITY;
p->label=tentative;
}
state[t].len=0;
state[t].label=permanent;
k=t;
do
{
for(i=1;i<=n;i++)
if(dist[k][i]!=0 && state[i].label==tentative)
{
if(state[k].len+dist[k][i]
{
state[i].pred=k;
state[i].len=state[k].len+dist[k][i];
}
}
k=0;
min=INFINITY;
for(i=1;i<=n;i++)
if(state[i].label==tentative && state[i].len
{
min=state[i].len;
k=i;
}
state[k].label=permanent;
} while(k!=s);
return(min);
}

output
Enter how many nodes do u want::4

Enter distance for 1 node: 0 2 3 4

Enter distance for 2 node: 2 0 7 8

Enter distance for 3 node: 3 7 0 6

Enter distance for 4 node: 4 8 6 0


Distance matrix is:
0234
2078
3706
4860

SENDING HELLO PACKETS


Hello packets to node 1:
234
Hello packets to node 2:
134
Hello packets to node 3:
124
Hello packets to node 4:
123
SENDING ECHO PACKETS
from 1 to 2, the distance is:2
from 1 to 3, the distance is:3
from 1 to 4, the distance is:4
from 2 to 1, the distance is:2
from 2 to 3, the distance is:7
from 2 to 4, the distance is:8
from 3 to 1, the distance is:3
from 3 to 2, the distance is:7
from 3 to 4, the distance is:6
from 4 to 1, the distance is:4
from 4 to 2, the distance is:8
from 4 to 3, the distance is:6

CONSTRUCTION LINKSTATE PACKETS


LINK STATE PACKET FOR 1
|-------------|
|1|
|-------------|
| seq |
|-------------|
| Age |
|-------------|
|2|2|
|-------------|
|3|3|
|-------------|
|4|4|
LINK STATE PACKET FOR 2
|-------------|
| seq |
|-------------|
| Age |
|-------------|
|1|2|
|-------------|
|3|7|
|-------------|
|4|8|
|-------------|
LINK STATE PACKET FOR 3
|-------------|
|3|
|-------------|
| seq |
|-------------|
| Age |
|-------------|
|1|3|
|-------------|
|2|7|
|-------------|
|4|6|
LINK STATE PACKET FOR 4
|-------------|
|4|
|-------------|
| seq |
|-------------|
| Age |
|-------------|
|1|4|
|-------------|
|2|8|
|-------------|
|3|6|
|---------------| | 2 | 8 |
|-------------|
|3|6|
|-------------|
DISTRIBUTING THE LINK STATE PACKETS
Neighbours for 1 node are:234
the distance from 1 to 2 is 2
the distance from 1 to 3 is 3
the distance from 1 to 4 is 4
Neighbours for 2 node are:134
the distance from 2 to 1 is 2
the distance from 2 to 3 is 7
the distance from 2 to 4 is 8
Neighbours for 3 node are:124
the distance from 3 to 1 is 3
the distance from 3 to 2 is 7
the distance from 3 to 4 is 6
Neighbours for 4 node are:123
the distance from 4 to 1 is 4
the distance from 4 to 2 is 8
the distance from 4 to 3 is 6
COMPUTING THE SHORTEST PATH
Enter source and destination:2 3

minimum distance:5

5. PROGRAM TO IMPLEMENT BROADCAST ROUTING ALGORITHM


#include
int a[10][10],n;
main()
{
int i,j,root;
clrscr();
printf("Enter no.of nodes:");
scanf("%d",&n);
printf("Enter adjacent matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("Enter connecting of %d-->%d::",i,j);
scanf("%d",&a[i][j]);
}
printf("Enter root node:");
scanf("%d",&root);
adj(root);
}
adj(int k)
{
int i,j;
printf("Adjacent node of root node::\n");
printf("%d\n\n",k);
for(j=1;j<=n;j++)
{
if(a[k][j]==1 || a[j][k]==1)
printf("%d\t",j);
}
printf("\n");
for(i=1;i<=n;i++)
{
if((a[k][j]==0) && (a[i][k]==0) && (i!=k))
printf("%d",i);
}
}

OUTPUT

Enter no.of nodes:5


Enter adjacent matrix
Enter connecting of 1-->1::0
Enter connecting of 1-->2::1
Enter connecting of 1-->3::1
Enter connecting of 1-->4::0
Enter connecting of 1-->5::0
Enter connecting of 2-->1::1
Enter connecting of 2-->2::0
Enter connecting of 2-->3::1
Enter connecting of 2-->4::1
Enter connecting of 2-->5::0
Enter connecting of 3-->1::1
Enter connecting of 3-->2::1
Enter connecting of 3-->3::0
Enter connecting of 3-->4::0
Enter connecting of 3-->5::0
Enter connecting of 4-->1::0
Enter connecting of 4-->2::1
Enter connecting of 4-->3::0
Enter connecting of 4-->4::0
Enter connecting of 4-->5::1
Enter connecting of 5-->1::0
Enter connecting of 5-->2::0
Enter connecting of 5-->3::0
Enter connecting of 5-->4::1
Enter connecting of 5-->5::0
Enter root node:2
Adjacent node of root node::
2

134
5

6. PROGRAM TO SIMULATE ROUTING USING FLOODING


#include
#include
int a[30][30],i,j,k=0,l=0,b[20],cnt=0,n,seq[30],ch;
char c1='x';
main()
{
clrscr();
b[1]=1;
printf("Enter no.of nodes:");
scanf("%d",&n);
printf("\nConstruct graph");
for(i=1;i<=n;i++)
{
printf("\nEnter edges for %d:",i);
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
}
printf("\n\t\tMENU");
printf("\n\t\t*********************");
printf("\n\t\t1. FLODDING WITH OUT SOLUTION");
printf("\n\t\t1. FLODDING WITH SOLUTION");
printf("\n\t\t**********************");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: for(i=1;i<=n;i++)
{
l=0;
for(j=1;j<=n;j++)
if(a[i][j]!=0)
l++;
if(i
cnt++;
b[i+1]=l*cnt;
}
flood();
break;
case 2: for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][j]!=0 && i
if(seq[j]!=1)
{
printf("\n%d-->%d",i,j);
printf("%c",c1);
seq[j]=1;
}
break;
default: exit(0);
}
getch();
}

flood()
{
printf("\n RECEIVED PACKETS ARE");
for(i=1;i<=n;i++)
{
for(j=2;j<=n;j++)
if(a[i][j]!=0 && i
{
printf("\n%d-->%d",i,j);
for(k=1;k<=b[i];k++)
printf("%c",c1);
}
}
}

OUTPUT

Enter no.of nodes:4

Construct graph
Enter edges for 1:0 1 0 1
Enter edges for 2:1 0 1 0
Enter edges for 3:0 1 0 1
Enter edges for 4:1 0 0 0

MENU
*********************
1. FLODDING WITH OUT SOLUTION
1. FLODDING WITH SOLUTION
**********************
Enter your choice:1

RECEIVED PACKETS ARE


1-->2x
1-->4x
2-->3xx
3-->4xxxx
Enter no.of nodes:4
Construct graph
Enter edges for 1:0 1 0 1
Enter edges for 2:1 0 1 0
Enter edges for 3:0 1 0 1
Enter edges for 4:1 0 0 0

MENU
*********************
1. FLODDING WITH OUT SOLUTION
1. FLODDING WITH SOLUTION
**********************
Enter your choice:2

1-->2x
1-->4x
2-->3x

7. CRC PROGRAM
#include
#include
int g1[]={1,0,0,1,1};
int g2[]={1,1,0,0,0,0,0,0,0,1,1,1,1};
int g3[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};
int g4[]={1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1};
int f[100],g[20],r[2],df[100];
int ch,gl,n,i,j,k;
void main()
{
clrscr();
do{
printf("\n 1.CRC");
printf("\n 2.CRC-12");
printf("\n 3.CRC-16");
printf("\n 4.CRC-CCIT");
printf("\n 5 .DEFAULT");
printf("\n EXIT");
printf("\n ENTER YOUR CHOICE");
scanf("%d",&ch);
switch(ch)
{
case 1 :for(i=0;i<5;i++)
g[i]=g1[i];
gl=5;
break;
case 2 :for(i=0;i<13;i++)
g[i]=g2[i];
gl=13;
break;
case 3 :for(i=0;i<17;i++)
g[i]=g3[i];
gl=17;
break;
case 4 :for(i=0;i<17;i++)
g[i]=g4[i];
gl=17;
break;
case 5 :printf("\n Enter the size of generator");
scanf("%d",&gl);
printf("\n Enter the generator bits");
for(i=o;i
scanf("%d",&g[i]);
}
printf("\n Enter the frame size");
scanf("%d",&n);
if(n>=gl)
{
printf("\n enter the frame bits");
for(i=0;i
{
scanf("%d",&f[i]);
df[i]=f[i];
}
clrscr();
printf("\n\n\t The entered frame is:");
for(i=0;i
printf("%d",f[i]);
printf("\n\n\t The Polynomial is:");
for(i=0;i
printf("%d",g[i]);
for(i=0;i
f[n+i]=0;
printf("\n\n\t\t\t ******STUFF*****");
printf("\n\n\t After adding zeros the frame is:");
for(i=0;i<(n+gl-1);i++)
printf("%d",f[i]);
division(f,g);
for(i=0;i<(n+gl-1);i++)
df[n+i]=r[i+1];
printf("\n\n\t The Transmitted frame is:");
for(i=0; i<(n+gl-1); i++)
printf("%d",df[i]);
printf("\n\n\t\t *****DESTUFF*****");
printf("\n\n\t\t The Receiving frame is :");
for(i=0;i<(n+gl-1);i++)
printf("%d",df[i]);
division(df,g);
printf("\n\n\t There are no errors in the Received frame");
}
else
printf("\n\n\t CRC is not possible");
}while(ch<=5);
getch();
}
division(int f[], int g[])
{
for(i=0;i<=(n-1);i++)
{
if((f[0]==1) && (g[0]==1))
{
for(j=0;j
{
if(f[j]==g[j])
f[j]=r[j]=0;
else
f[j]=r[j]=1;
}
}
else
{
for(k=0;k
r[k]=f[k];
}
for(k=0;k<(n+gl-1);k++)
f[k]=f[k+1];
}
printf("\n\n\t\t The Remainder is:");
for(i=0;i
printf("%d",r[i]);
}

S-ar putea să vă placă și