h->connect_status = 0;
h->connect_starttime = time(NULL);
if(connect(h->fd, (struct sockaddr *)&h->server, sizeof(struct sockaddr_in)) < 0)
{
perror("connect failed:");
if (errno != EALREADY && errno != EINPROGRESS)
{
perror("Connect Server failed:");
close(h->fd);
h->connect_status = 0;
h->fd = -1;
return -1;
}
//usleep(1000000);
//printf("call usleep(1000000)\n");
FD_ZERO(&writeset);
FD_SET(h->fd, &writeset);
tv.tv_sec = 0;
tv.tv_usec = 500000;
if(select(h->fd + 1, NULL, &writeset, NULL, &tv) > 0)
{
opt_len = sizeof(connect_status);
ret = getsockopt(h->fd, SOL_SOCKET, SO_ERROR, (char *)&connect_status, &opt_len);
if(ret == 0)
{
if(connect_status == 0)
{
printf("connect ok.\n");
}
else
{
printf("socket status:%d\n", strerror(connect_status));
h->connect_status = -1; //connect_status;
return -1;
}
}
}
else
{
perror("select failed:");
h->connect_status = -1;
return -1;
}
}
评论