HP Rp7410 BSD Sockets Interface Programmer's Guide - Page 53
len += len1
View all HP Rp7410 manuals
Add to My Manuals
Save this manual to your list of manuals |
Page 53 highlights
Using Internet Stream Sockets Example Using Internet Stream Sockets exit(1); } /* Go into a loop, receiving requests from the * remote client. After the client has sent the * last request, it will do a shutdown for sending, * which causes an end-of-file condition to appear * on this end of the connection. After all of the * 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 */ /* 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. */ while (len < 10) { len1 = recv(s, &buf[len], 10-len, 0); if (len1 == -1) goto errout; len += len1; } /* Increment the request count. */ reqcnt++; /* This sleep simulates the processing of * the request that a real server might do. */ sleep(1); /* Send a response back to the client. */ 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 53