/* Set the socket to nonblocking first */
opt = fcntl(fd, F_GETFL, NULL);
opt |= O_NONBLOCK;
fcntl(fd, F_SETFL, opt);
for(;;)
{
/* open the connection */
ret = connect(fd, addr, sizeof(struct sockaddr));
if( ret == 0 )
{
/* connection worked */
break;
}
else if( errno != EINPROGRESS )
{
/* no connection yet, and errno is not EINPROGRESS
* this means we have some sort of a connection error
*/
break;
}
opt = 0;
tv.tv_sec = seconds;
tv.tv_usec = 0;
FD_ZERO(&fdset);
FD_SET(fd, &fdset);
/* wait for something to see if the socket becomes
* writeable ( it will become writable on error as well */
ret = select(fd+1, NULL, &fdset, NULL, &tv);
if ( ret == 0)
{
errno = ETIMEDOUT;
ret = -1;
break;
}
else if( ret == -1 )
{
break;
}
}
/* Set socket back to blocking mode */
opt = fcntl(fd, F_GETFL, NULL);
opt &= (~O_NONBLOCK);
fcntl(fd, F_SETFL, opt);
//#if __DEBUG__
/* debug junk for those that want to see how this works */
if( ret == 0 )
{
printf("Returning Success\n");
}
else
{
printf("Returning
Failure(%d),errno=%d,%s\n",ret,errno,strerror(errno));
}
//