ptg10805159
606 Network IPC: Sockets Chapter 16
The address we specify withconnectis the address of the server with which we wish
to communicate. Ifsockfdis not bound to an address,connectwill bind a default
address for the caller.
When we try to connect to a server,the connect request might fail for several
reasons. Foraconnect request to succeed, the machine to which we aretrying to
connect must be up and running, the server must be bound to the address we aretrying
to contact, and theremust be room in the server’s pending connect queue (we’ll learn
moreabout this shortly). Thus, applications must be able to handleconnecterror
returns that might be caused by transient conditions.
Example
Figure16.10 shows one way to handle transientconnecterrors. These errors arelikely
with a server that is running on a heavily loaded system.
#include "apue.h"
#include <sys/socket.h>
#define MAXSLEEP 128
int
connect_retry(int sockfd, const struct sockaddr *addr, socklen_t alen)
{
int numsec;
/*
*Try to connect with exponential backoff.
*/
for (numsec = 1; numsec <= MAXSLEEP; numsec <<= 1) {
if (connect(sockfd, addr, alen) == 0) {
/*
*Connection accepted.
*/
return(0);
}
/*
*Delay before trying again.
*/
if (numsec <= MAXSLEEP/2)
sleep(numsec);
}
return(-1);
}
Figure 16.10 Connect with retry
This function shows what is known as anexponential backoffalgorithm. If the call to
connectfails, the process goes to sleep for a short time and then tries again, increasing
the delay each time through the loop, up to a maximum delay of about 2 minutes.