|
|
发表于 2006-6-7 22:28:53
|
显示全部楼层
我自己都是这么写的, 不过也许是我的 connect 一般没有发生过失败, 所以可能会有潜在的问题我没有发现
这里附上 APUE 2ed 中的一段例子
- #include "apue.h"
- #include <sys/socket.h>
- #define MAXSLEEP 128
- int
- connect_retry(int sockfd, const struct sockaddr *addr, socklen_t alen)
- {
- int nsec;
-
- /*
- * Try to connect with exponential backoff.
- */
- for (nsec = 1; nsec <= MAXSLEEP; nsec <<= 1) {
- if (connect(sockfd, addr, alen) == 0) {
- /*
- * Connection accepted.
- */
- return(0);
- }
- /*
- * Delay before trying again.
- */
- if (nsec <= MAXSLEEP/2)
- sleep(nsec);
- }
- return(-1);
- }
复制代码 |
|