在Linux网络连接中connect服务器时,下面哪种算法更好些,为什么?
int connect_retry(int sockfd,const struct sockaddr_in *addr,socklen_t alen)
{
int nsec;
for (nsec = 1; nsec <= MAXSLEEP; nsec <<= 1)
{
if (connect(sockfd,(struct sockaddr *)addr,alen) == 0)
return 0;
if (nsec <= MAXSLEEP)
sleep(nsec);
}
return -1;
}
for (;;)
{
if (connect(sockfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0)
sleep(1);
else
break;
}
------解决方案--------------------