前言
首linux系统下,connect函数是阻塞的,阻塞时间的长度与系统相关。而如果把套接字设置成非阻塞,调用connect函数时会报错Operation now in progress,且errno被设置为EINPROGRESS。下面将分析非阻塞时调用connect报错的原因
然后看看man手册
EINPROGRESS
The socket is nonblocking and the connection cannot be completed
immediately. It is possible to select(2) or poll(2) for comple‐
tion by selecting the socket for writing. After select(2) indi‐
cates writability, use getsockopt(2) to read the SO_ERROR option
at level SOL_SOCKET to determine whether connect() completed
successfully (SO_ERROR is zero) or unsuccessfully (SO_ERROR is
one of the usual error codes listed here, explaining the reason
for the failure).
简单来说就是
这段话描述了connect 出错时的一种errno值:EINPROGRESS。这种错误发生在对非阻
塞的socket调用connect,而连接又没有立即建立时。根据man文档的解释,在这种情况下,我们可以调用select、poll等函数来监听这个连接失败的socket上的可写事件。当select、poll等函数返回后,再利用getsockopt来读取错误码并清除该socket上的错误。如果错误码是0,表示连接成功建立,否则连接失败。
代码示例
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<assert.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>
#include<errno.h>
#include<sys/epoll.h>
#include<pthread.h>
#include<sys/ioctl.h>
int setsocknoblock(int fd)
{
int old_option=fcntl(fd,F_GETFL);
int new_option=old_option|O_NONBLOCK;
fcntl(fd,F_SETFL,new_option);
return old_option;
}
/*
parameter meaning
ip 服务器端口信息
port 端口号
time 超时时间
function preformancd:
实现connect非阻塞
*/
int unblock_connect(const char* ip,int port,int time)
{
int ret=0;
struct sockaddr_in address;
bzero(&address,sizeof(address));
address.sin_family=AF_INET;
address.sin_port=htons(port);
inet_pton(AF_INET,ip,&address.sin_addr);
int sockfd=(PF_INET,SOCK_STREAM,0);
int fdopt=setsocknoblock(sockfd);
ret=connect(sockfd,(struct sockaddr*)&address,sizeof(address));
if(ret==0)
{
printf("connect with server immediately\n");
fcntl(sockfd,F_SETFD,fdopt);
return sockfd;
}
else if(errno!=EINPROGRESS)//连接没有立即建立
//errno 是EINPROGRESS时表示连接正在建立
{
printf("unblock connect not support\n");
return -1;
}
fd_set readfds;
fd_set writefds;
struct timeval timeout;
timeout.tv_sec=time;
timeout.tv_usec=0;
FD_ZERO(&readfds);
FD_SET(sockfd,&writefds);
ret=select(sockfd+1,NULL,&writefds,NULL,&timeout);
if(ret<=0)
{
//select 超时或者出错
printf("connect time out\n");
close(sockfd);
return -1;
}
if(!FD_ISSET(sockfd,&writefds))
{
printf("no event no sockfd found");
close(sockfd);
return -1;
}
int error=0;
socklen_t length=sizeof(error);
if(getsockopt(sockfd,SOL_SOCKET,SO_ERROR,&error,&length)<0)
{
printf("get socket option failed\n");
close(sockfd);
return -1;
}
if(error!=0)
{
printf("connect failed after with the error %d\n",error);
close(sockfd);
return -1;
}
printf("connection ready after select with the socket :%d\n",sockfd);
fcntl(sockfd,F_SETFD,fdopt);
return sockfd;
}
int main(int argc,char* argv[])
{
if(argc<=2)
{
printf("Usage %s ip_addresss port_number\n",basename(argv[0]));
return 1;
}
const char* ip=argv[1];
int port=atoi(argv[2]);
int sockfd=unblock_connect(ip,port,10);
assert(sockfd!=-1);
close(sockfd);
return -1;
}
工作流程:
注意:
但遗憾的是,这种方法存在几处移植性问题。首先,非阻塞的socket可能导致connect始终失败。其次,select对处于EINPROGRESS 状态下的socket可能不起作用。最后,对于出错的socket, getsockopt在有些系统(比如Linux)上返回-1(上面代码),而在有些系统(比如源自伯克利的UNIX)上则返回0。这些问题没有一个统一的解决方法,感兴趣的读者可自行参考相关文献。