Simulation DNS Server in Network LAP C Language Source Code Programming

Develop a client contacts a DNS server to resolve a Give host name.
Step by step algorithm:DNS server: include the required header files
Create a socket between server and client.
Bind server to the socket.
Listen to the message from the client
Accept the message from the client
Display the host name
Resolve the host name
Display the output and also sent it to client.
See->Syntax of commands used DNS server Client gethostbyname() htonl htons in Linux
Source code DNS server programming in C Language
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>
#include<unistd.h>
#define SER_PORT 8013
int main()
{
int a,c;
FILE *f1,*f2;
int sersock,newsock,n;
char str[25],str2[25],host[25];
void *buf,*buf2;
struct sockaddr_in seraddr;
struct sockaddr_in clinfo;
struct hostent *hp;
socklen_t size=sizeof(clinfo);
seraddr.sin_family=AF_INET;
seraddr.sin_port=htons(SER_PORT);
seraddr.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*)&seraddr,sizeof(seraddr))<0)
{
error(" bind\n");
exit(0);
} if(listen(sersock,1)<0)
{
error(" listen\n");
exit(0);
} if((newsock=accept(sersock,(struct sockaddr*)&clinfo,&size))<0)
{ error(" accept \n");
exit(0);
}
else
printf("connected to m%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\n",str2);
write(newsock,str2,strlen(str2)+1);
close(newsock);
close(sersock);
return 0;
}
Output DNS server
[Linux28@localhost network]$cc dnsserver.c
[Linux28@localhost network]$./a.out
Connected to 192.168.1.111
Local host 127.0.0.1

Related post