Computer Networks Algorithms in C Language
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
1110000011111011111001
DATA AFTER UNSTUFFING
11100000111111111101
********************:
1.BIT STUFFING:
2.CHARACTER STUFFING:
3.EXIT:
*********************
Enter your choice:2
Enter data to send at end put '}':
getch();
}
OUTPUT
From to 2 4
Short path2-->3-->4
Minimum distance is 15
output
Enter no.of nodes::4
}
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
minimum distance:5
OUTPUT
134
5
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
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
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]);
}