Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Dns Application

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

DNS APPLICATION AIM: To write a program to develop an application for DNS. ALGORITHM: Server: 1. Include required header files.

2. Create a socket between server and client. 3. Bind server to socket. 4. Listen to the message from the client. 5. Accept the message from the client. 6. Display the host name. 7. resolve the host name. 8. Display the output and send it to client. Client: 1. Include required header files. 2. Define the port family. 3. Create socket using SOCKET(). 4. Connect the client to the socket using CONNECT(). 5. Enter the host name and send it to server. 6. The result is received and displayed .

PROGRAM: Server: #include<stdio.h> #include<sys/socket.h> #include<sys/types.h> #include<arpa/inet.h> #include<unistd.h> #include<netdb.h> #include<string.h> #include<stdlib.h> #define SER_PORT 8765 int main() { int a,c; FILE *f1,*f2; int sersock,newsock,n; char str[25],str2[25],host[25]; void *buf1,*buf2; struct sockaddr_in servaddr,clinfo; struct hostent *hp; socklen_t size=sizeof(clinfo); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(SER_PORT); servaddr.sin_addr.s_addr=htonl(INADDR_ANY); if((sersock=socket(AF_INET,SOCK_STREAM,0))<0) { error("Socket!\n"); exit(0); } if(bind(sersock,(struct sockaddr*)&servaddr,sizeof(servaddr))<0) { error("Bind!\n"); exit(0); } if(listen(sersock,1)<0) { error("Listen!\n"); exit(0); } size=sizeof(clinfo);

if((newsock=accept(sersock,(struct sockaddr*)&clinfo,&size))<0) { error("Accept!\n"); exit(0); } else printf("Connected to %s\n",inet_ntoa(clinfo.sin_addr)); read(newsock,host,sizeof(host)); printf("%s",host); hp=gethostbyname(host); inet_ntop(AF_INET,hp->h_addr,str2,sizeof(str2)); printf("%s",str2); write(newsock,str2,strlen(str2)+1); close(newsock); close(sersock); return 0; }

Client: #include<stdio.h> #include<sys/socket.h> #include<sys/types.h> #include<arpa/inet.h> #include<netdb.h> #include<netinet/in.h> #include<unistd.h> #include<stdlib.h> #include<string.h> #define SER_PORT 8765 int main(int count,char *argv[]) { int a; int clisock,x,b,c; char str[25],host[25]; char str2[25]; struct sockaddr_in cliaddr; cliaddr.sin_port=htons(SER_PORT); cliaddr.sin_family=AF_INET; cliaddr.sin_addr.s_addr=inet_addr(argv[1]); clisock=socket(AF_INET,SOCK_STREAM,0); if(clisock<0) { perror("No Socket!\n"); exit(0); } if((x=connect(clisock,(struct sockaddr*)&cliaddr,sizeof(cliaddr)))<0) { perror("No connect!\n"); exit(0); } printf("Enter Domain Name: "); scanf("%s",host); write(clisock,host,strlen(host)+1); read(clisock,str2,sizeof(str2)); printf("%s\n",str2); close(clisock); return 0; }

OUTPUT: Server: [test2@localhost ~]$ vi dnsserv.c [test2@localhost ~]$ cc dnsserv.c [test2@localhost ~]$ ./a.out Client: [test2@localhost ~]$ vi dnscli.c [test2@localhost ~]$ cc dnscli.c [test2@localhost ~]$ ./a.out 192.160.10.2 Enter Domain Name: localhost 127.0.0.1 Server: Connected to 193.167.1.225 localhost127.0.0.1

RESULT: Thus the above program DNS application was completed successfully.

You might also like