HP Rp7410 BSD Sockets Interface Programmer's Guide - Page 52
S E R V E R
View all HP Rp7410 manuals
Add to My Manuals
Save this manual to your list of manuals |
Page 52 highlights
Using Internet Stream Sockets Example Using Internet Stream Sockets } /* * S E R V E R * * This is the actual server routine that the daemon forks * to handle each individual connection. Its purpose is * 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; /* keeps count of number of requests */ char buf[10]; /* This example uses 10 byte messages. */ 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, sizeof (struct in_addr), peeraddr_in.sin_family); if (hp == NULL) { /* The info is unavailable for the remote host. * Just format its internet address to be * printed in the logging information. The * address will be shown in internet dot format. */ hostname = inet_ntoa(peeraddr_in.sin_addr); } else { hostname = hp->h_name; /* point to host's 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, sizeof(linger)) == -1) { errout: printf("Connectionwith%saborterror\n",hostname); 52 Chapter 2