HP Rp3440-4 - 9000 - 0 MB RAM Programmer's Manual page 52

Bsd sockets interface programmer’s guide
Hide thumbs Also See for Rp3440-4 - 9000 - 0 MB RAM:
Table of Contents

Advertisement

Using Internet Stream Sockets
Example Using Internet Stream Sockets
}
/*
*
*
*
This is the actual server routine that the daemon forks
*
to handle each individual connection.
*
to receive the request packets from the remote client,
*
process them, and return the results to the client.
*
It will also write some logging information to stdout.
*
*/
server()
{
int reqcnt = 0;
char buf[10];
char *inet_ntoa();
char *hostname; /* points to remote host's name string */
int len, len1;
/* Close the listen socket inherited from the daemon. */
close(ls);
/* Look up the host information for the remote host
* we have connected with. Its internet address
* was returned by the accept call, in the main
* daemon loop above.
*/
hp = gethostbyaddr ((char *) &peeraddr_in.sin_addr,
if (hp == NULL) {
/* The info is unavailable for the remote host.
* Just format its internet address to be
* printed in the logging information.
* address will be shown in internet dot format.
hostname = inet_ntoa(peeraddr_in.sin_addr);
} else {
hostname = hp->h_name;
}
/* Log a startup message. */
time (&timevar);
/* The port number must be converted first to
* host byte order before printing. On most hosts,
* this is not necessary, but the ntohs() call is
* included here so that this program could easily
* be ported to a host that does require it.
*/
printf("Startup from %s port %u at %s",
hostname, ntohs(peeraddr_in.sin_port), ctime(&timevar));
/* Set the socket for a lingering, graceful close.
* Since linger was set to 1 above, this will
* cause a final close of this socket to wait
* until all of the data sent on it has been
* received by the remote host.
*/
if (setsockopt(s, SOL_SOCKET, SO_LINGER, &linger,
errout:
printf("Connectionwith%saborterror\n",hostname);
52
S E R V E R
/* keeps count of number of requests */
/* This example uses 10 byte messages. */
sizeof (struct in_addr),
peeraddr_in.sin_family);
*/
/* point to host's name */
sizeof(linger)) == -1) {
Its purpose is
The
Chapter 2

Advertisement

Table of Contents
loading

Table of Contents