ACET - CN Lab Exp - Codes-1
ACET - CN Lab Exp - Codes-1
AIM: To write a C program to develop a DNS client server to resolve the given
hostname.
ALGORITHM:
1. Create a new file. Enter the domain name and address in that file.
2. To establish the connection between client and server.
3. Compile and execute the program.
4. Enter the domain name as input.
5. The IP address corresponding to the domain name is display on the screen
6. Enter the IP address on the screen.
7. The domain name corresponding to the IP address is display on the screen.
8. Stop the program.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<netdb.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main(intargc,char *argv[1])
{
structhostent *hen;
if(argc!=2)
{
fprintf(stderr,"Enter the hostname \n");
exit(1);
}
hen=gethostbyname(argv[1]);
if(hen==NULL)
{
fprintf(stderr,"Host not found \n");
}
printf("Hostname is %s \n",hen->h_name);
printf("IP address is %s \n",inet_ntoa(*((structin_addr *)hen->h_addr)));
}
Experiment-3
Aim: To implement on a data set of characters the three CRC polynomials – CRC 12,
CRC 16 and CRC CCITT.
#include<stdio.h>
#include<conio.h>
#include<string.h>
char g[100],d[100],f[100],y[100],t[100];
inti,j,k,ch;
main()
{
int c;
clrscr();
while(1)
{
printf("\nMENU:\n1.CRC\n2.CRC12\n3.CRC16\n4.CRCCCIT\n5.EXIT\nEnter ur choice:");
scanf("%d",&c);
switch(c)
{
case 1:crc();
break;
case 2:crc12();
break;
case 3:crc16();
break;
case 4:crcccit();
break;
case 5:exit(0);
}
}
}
crc()
{
printf("\nEnter the data for transmission:");
scanf("%s",f);
printf("\nEnter the generator:");
scanf("%s",g);
p();
}
crc12()
{
printf("\nEnter the 6 bit data:");
scanf("%s",f);
for(i=0;i<13;i++)
{
if(i==0||i==1||i==9||i==10||i==11||i==12)
g[i]='1';
else
g[i]='0';
}
g[13]='\0';
p();
}
crc16()
{
printf("Enter 8 bit data:");
scanf("%s",f);
for(i=0;i<17;i++)
{
if(i==0||i==1||i==14||i==16)
g[i]='1';
else
g[i]='0';
}
g[17]='\0';
p();
}
crcccit()
{
printf("\nEnter 8 bit data:");
scanf("%s",f);
for(i=0;i<17;i++)
{
if(i==0||i==4||i==11||i==16)
g[i]='1';
else
g[i]='0';
}
g[17]='\0';
p();
}
p()
{
j=strlen(f)+strlen(g)-1;
for(i=strlen(f);i<j;i++)
{
f[i]='0';
}
f[i]='\0';
strcpy(d,f);
while(strlen(d)>=strlen(g))
{
for(i=0;g[i]!='\0';i++)
{
ch=(d[i]-'0')^(g[i]-'0');
d[i]=ch+'0';
}
while(d[0]=='0')
for(j=0;d[j]!='\0';j++)
d[j]=d[j+1];
}
printf("\nCheck sum is: %s",d);
i=strlen(f)-1;
j=strlen(d)-1;
for(k=j;k>=0;k--)
{
if(d[k]!='\0')
f[i]=d[k];
i--;
}
printf("\nFinal frame to be transmitted:%s",f);
getch();
}
Result:
Menu:
1. CRC
2. CRC-12
3. CRC-16
4. CRC CCIT
5. EXIT.
Enter Your Choice: 2
Menu :
1. CRC
2. CRC-12
3. CRC-16
4. CRC CCIT
5. EXIT.
Enter Your Choice: 5
Experiment-4
Aim: To implement Dijkstra‘s algorithm to compute the Shortest path thru a subnet.
#include<stdio.h>
#include<conio.h>
void main()
{
int path[5][5],i,j,min,a[5][5],p,st=1,ed=5,stp,edp,t[5],index;
clrscr();
printf("enter the cost matrix\n");
for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
scanf("%d",&a[i][j]);
printf("enter number of paths\n");
scanf("%d",&p);
printf("enter possible paths\n");
for(i=1;i<=p;i++)
for(j=1;j<=5;j++)
scanf("%d",&path[i][j]);
for(i=1;i<=p;i++)
{
t[i]=0;
stp=st;
for(j=1;j<=5;j++)
{
edp=path[i][j+1];
t[i]=t[i]+a[stp][edp];
if(edp==ed)
break;
else
stp=edp;
}
}
min=t[st];index=st;
for(i=1;i<=p;i++)
{
if(min>t[i])
{
min=t[i];
index=i;
}
printf("minimum cost %d",min);
printf("\n minimum cost path ");
for(i=1;i<=5;i++)
{
printf("--> %d",path[index][i]);
if(path[index][i]==ed)
break;
}
getch();
}
Result:
INPUT:
enter the cost matrix :
01420
00023
00030
00005
00000
enter number of paths : 4
enter possible paths :
12450
12500
14500
13450
OUTPUT:
minimum cost : 4
minimum cost path :
1-->2-->5
Experiment-5
Aim: Take an example subnet graph with weights indicating delay between nodes. Now
obtain Routing table art each node using distance vector routing algorithm.
Code:
#include<ctype.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
intdest,ja,ji,jk,j,jh,min,delay;
chardnode,line;
structnewj
{
int delay;
char line;
}s[12];
int a[12]={0,12,25,40,14,23,18,17,21,9,24,29};
inti[12]={24,36,18,27,7,20,31,20,0,11,22,33};
int h[12]={20,31,19,8,30,19,6,0,14,7,22,9};
int k[12]={21,28,36,24,22,40,31,19,22,10,0,9};
main()
{
ja=8;
ji=10;
jh=12;
jk=6;
clrscr();
printf("\n The source node is j");
printf("\n The adjacent nodes from j are a,i,h,k");
printf("\n Enter the destination node");
scanf("%c",&dnode);
dest=toupper(dnode)-'A';
for(j=0;j<12;j++)
{
min=a[j]+ja;
line='a';
if(min>i[j]+ji)
{
line='i';
min=i[j]+ji;
}
if(min>h[j]+jh)
{
line='h';
min=h[j]+jh;
}
if(min>k[j]+jk)
{
line='k';
min=k[j]+jk;
}
s[j].delay=min;
s[j].line=line;
}
printf("The routing table is ");
printf("\n Dest \t Distance \t via node");
for(j=0;j<12;j++)
if(j==9)
printf("\n j \t 0 \t \t j");
else
printf("\n %c \t %d \t \t %c", j+'a', s[j].delay,
s[j].line);
if(dest==9)
printf("\n Delay from source j to destination j is 0 via j"); else
printf("\n Delay from source j to %c is %d via %c", dnode,s[dest]. delay, s[dest]. line);
getch();
}
Result:
The source node is j
The adjacent nodes from j are a,i,h,k
Enter the destination nodea
The routing table is
Dest Distance via node
a 8 a
b 20 a
c 28 i
d 20 h
e 17 i
f 30 i
g 18 h
h 12 h
i 10 i
j 0 j
k 6 k
l 15 k
Experiment-6
Code:
#include<stdio.h>
main()
{
int a[20][20],i,j,n,source,b[20],k=0,1;
printf("\nEnterno.of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the source node for broadcast:");
scanf("%d",&source);
b[k]=source;
k++;
printf("\nafterbroadcastrouting:\n");
for(i=0;i<n;i++)
{
if(a[source][i]!=0)
{
printf("\n%d---->%d\n",source,i);
b[k]=i;
k++;
}
if(k==n)
break;
}
}
}
Result:
Enter number of nodes : 3
Enter a [0][0] : 0
Enter a [0][1] : 0
Enter a [0][2] : 1
Enter a [1][0] : 0
Enter a [1][1] : 0
Enter a [1][2] : 1
Enter a [2][0] : 0
Enter a [2][1] : 1
Enter a [2][2] : 1
Enter the Source node for broad cast: 2
After broadcast routing:
2---->1
2---->2