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

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

Advertisement

Programming Hints
Adding a Server Process to the Internet Daemon
*
*
*
This is the actual server routine that the /etc/inetd forks to
*
handle each individual connection.
*
the request packets from the remote client, process them,
*
and return the results to the client.
*
*/
main()
{
char buf[10];
int len, lenl;
/* Go into a loop, receiving requests from the remote
* client.
* it will do a shutdown for sending, which will cause
* an end-of-file condition to appear on this end of the
* connection.
* been received, the next recv call will return zero
* bytes, signaling an end-of-file condition.
* how the server will know that no more requests will
* follow, and the loop will be exited.
*/
while (len = recv(0, buf, 10, 0)) {
if (len == -1) {
}
/* The reason this while loop exists is that there
* is a remote possibility of the above recv returning
* less than 10 bytes.
* as soon as there is some data, and will not wait for
* all of the requested data to arrive.
* is relatively small compared to the allowed TCP
* packet sizes, a partial receive is unlikely.
* this example had used 2048 bytes requests instead,
* a partial receive would be far more likely.
* This loop will keep receiving until all 10 bytes
* have been received, thus guaranteeing that the
* next recv at the top of the loop will start at the
* beginning of the next request.
*/
while (len < 10) {
len1 = recv(0, &buf[len], 10-len, 0);
if (len1 == -1) {
}
sleep(1);
if (send(0, buf, 10, 0) != 10) {
}
}
}
/* The loop has terminated, because there are no
* more requests to be serviced.
170
M A I N
/* This example uses 10 byte messages. */
After the client has sent the last request,
After all the client's requests have
exit (1); /* error from recv */
This is because a recv returns
exit (1);
}
len += len1;
/* This sleep simulates the processing of
* the request that a real server may do.
*/
/* Send a response back to the client. */
exit (1);
Its purpose is to receive
This is
Since 10 bytes
If
Chapter 8

Advertisement

Table of Contents
loading

Table of Contents