BSD Sockets Interface Programmer's Guide

Chapter 8 171
Programming Hints
Adding a Server Process to the Internet Daemon
exit (0);
}
/*
* S E R V E R . U D P
*
* This is a variation of the example program called serv.udp.
* This one performs the same function, except that it is
* designed to be called from /etc/inetd. This version does
* not contain a daemon loop, and does not wait for requests
* to arrive on a socket. /etc/inetd does these functions. The
* server simply assumes the socket to receive the message
* from and send the response to is file descriptor 0 when
* the program is started. It also assumes that the client's
* request is already ready to be received from the socket.
*
*/
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
#define BUFFERSIZE 1024 /* max size of packets to be received */
int cc; /* contains the number of bytes read */
char buffer[BUFFERSIZE]; /* buffer for packets to be read into */
struct hostent *hp; /* pointer to info for requested host */
struct sockaddr_in clientaddr_in;/* for client's socket address */
struct in_addr reqaddr; /* for requested host's address */
#define ADDRNOTFOUND 0xffffffff /* return address for unfound
host */
/*
* M A I N
*
* This routine receives the request and returns an answer.
* Each request consists of a host name for which the
* requester desires to know the internet address. The
* server will look up the name in its /etc/hosts file,
* and return the internet address to the client. An
* a internet address value of all ones will be returned
* if the host name is not found.
*
*/
main()
{
int addrlen;
/* clear out address structure */
memset ((char *)&clientaddr_in, 0, sizeof(struct sockaddr_in));
/* Note that addrlen is passed as a pointer
* so that the recvfrom call can return the
* size of the returned address.
*/
addrlen = sizeof(struct sockaddr_in);
/* This call will
* return the address of the client,
* and a buffer containing its request.