AIM:
To write a program to display the host address for the given domain name.
ALGORITHM:
SERVER:
STEP 1: Start
STEP 2: Declare the variables for the socket
STEP 3: Specify the family, protocol, IP address and port number
STEP 4: Create a socket using socket() function
STEP 5: Bind the IP address and Port number
STEP 6: Listen and accept the client’s request for the connection
STEP 7: Read the domain name and display if it is binded
STEP 8: Display the error message if it is not binded
STEP 9: Stop
CLIENT:
STEP 1: Start
STEP 2: Declare the variables for the socket
STEP 3: Specify the family, protocol, IP address and port number
STEP 4: Create a socket using socket() function
STEP 5: Check whether the connection is established or not
STEP 6: Get the domain name
STEP 7: Display the corresponding IP address for the domain name
STEP 8: Stop
SOURCE CODE:
SERVER:
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<stdlib.h>
#include<netdb.h>
#define SA struct sockaddr
int main()
{
int listenfd,connfd;
socklen_t len;
struct sockaddr_in servaddr,cliaddr;
struct hostent*hp,tp;
char host[50],buff[100];
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(5035);
servaddr.sin_addr.s_addr=htons(INADDR_ANY);
printf("\nBinded");
bind(listenfd,(SA*)&servaddr,sizeof(servaddr));
listen(listenfd,10);
for(;;)
{
len=sizeof(cliaddr);
connfd=accept(listenfd,(SA*)&cliaddr,&len);
read(connfd,host,sizeof(host));
printf("\n\t%s",host);
if((hp=gethostbyname(host))==NULL)
printf("\nCan't get address");
if(inet_ntop(AF_INET,hp->h_addr,buff,sizeof(buff))<=0)
printf("\nHost address is not available");
printf("\n\t%s",buff);
write(connfd,buff,strlen(buff)+1);
close(connfd);
}
return 0;
}
CLIENT:
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<netdb.h>
#include<stdlib.h>
#define SA struct sockaddr
int main(int argc,char **argv)
{
socklen_t len;
struct sockaddr_in servaddr;
struct hostent tp;
char buff[100],host[50];
int sockfd;
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(5035);
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
if(connect(sockfd,(SA*)&servaddr,sizeof(servaddr))<0)
printf("Connect Error");
printf("\nEnter the Domain Name : ");
scanf("%s",host);
write(sockfd,host,strlen(host)+1);
if(read(sockfd,buff,sizeof(buff))<0)
printf("\nRead Error");
printf("\nThe host address : %s\n",buff);
return 0;
}
OUTPUT:
SERVER:
CLIENT:
RESULT:
Thus the program for Domain Name System was executed and the output was
verified.
EmoticonEmoticon