BSD Sockets Interface Programmer's Guide

172 Chapter 8
Programming Hints
Adding a Server Process to the Internet Daemon
* BUFFERSIZE - 1 bytes are read so that
* room is left at the end of the buffer
* for a null character.
*/
cc = recvfrom(0, buffer, BUFFERSIZE - 1, 0 &clientaddr_in,
&addrlen);
if ( cc == -1) exit(1);
/* Make sure the message received in
* null terminated.
*/
buffer[cc]='\0';
/* Treat the message as a string containing
* a hostname. Search for the name
* in /etc/hosts.
*/
hp = gethostbyname (buffer);
if (hp == NULL) {
/* Name was not found. Return a
* special value signifying the error.
/*
reqaddr.s_addr = ADDRNOTFOUND;
} else {
/* Copy address of host into the
* return buffer.
/*
reqaddr.s_addr =
((struct in_addr *)(hp->h_addr))->s_addr;
}
/* send the response back to the requesting client. The
* address is sent in network byte order. Note that
* all errors are ignored. The client
* will retry if it does not receive
* the response.
*/
sendto (0, &reqaddr, sizeof(struct in_addr), 0,
&clientaddr_in, addrlen);
exit(0);
}