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

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

Advertisement

exit(1);
}
/* Go into a loop, receiving requests from the
* remote client.
* last request, it will do a shutdown for sending,
* which causes an end-of-file condition to appear
* on this end of the connection.
* client's requests have been received, the next
* recv call will return zero bytes, signalling an
* end-of-file condition. This is is how the server
* will know that no more requests will follow
* and the loop will be exited.*/
while (len = recv(s, buf, 10, 0)) {
if (len == -1) goto errout; /* error from recv */
while (len < 10) {
}
reqcnt++;
sleep(1);
if (send(s, buf, 10, 0) != 10) goto errout;
}
/* The loop has terminated, because there are no
* more requests to be serviced. As above, this
* close will block until all of the sent replies
* have been received by the remote host. Lingering
* on the close is so the server will have a better
* idea when the remote has picked up all the data.
* This allows the start and finish times printed
* in the log file to more accurately reflect
* the length of time this connection was used.
*/
close(s);
/* Log a finishing message. */
time (&timevar);
Chapter 2
Example Using Internet Stream Sockets
After the client has sent the
/* The reason this while loop exists is that
* there is a remote possibility of the above
* recv returning less than 10 bytes. This is
* because a recv returns as soon as there is
* some data, and will not wait for all of the
* requested data to arrive. Since 10 bytes is
* relatively small compared to the allowed TCP
* packet sizes, a partial receive is unlikely.
* If 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.
*/
len1 = recv(s, &buf[len], 10-len, 0);
if (len1 == -1) goto errout;
len += len1;
/* Increment the request count. */
/* This sleep simulates the processing of
* the request that a real server might do.
*/
/* Send a response back to the client. */
Using Internet Stream Sockets
After all of the
53

Advertisement

Table of Contents
loading

Table of Contents