首页 > linux socket connect [error:EINPROGRESS]一般会在什么情况下产生?

linux socket connect [error:EINPROGRESS]一般会在什么情况下产生?

ret = connect(fd, (struct sockaddr*)&server, sizeof(server)));

调用connect然后返回-1,查了下文档是这么说的

EINPROGRESS


----------


The socket is nonblocking and the connection cannot be completed immediately. It is possible to select(2) or poll(2) for completion by selecting the socket for writing. After select(2) indicates 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).

现在疑惑的是在哪些情况下connetc有可能会返回EINPROGRESS,以及对于这种情况我做如下的处理是否得当?

int err = -1;
socklen_t len = sizeof(int);
if (getsockopt(fd,  SOL_SOCKET, SO_ERROR ,&err, &len) < 0 ){
        close(fd);
        logError("errno:%d %s\n", errno, strerror(errno));
}

if (err){
        errno = err;
        close(fd);
        logError("%d :Get socketopt err: %d", __LINE__, err);
}

非阻塞的socket,connect调用后立即返回,连接过程还在执行

【热门文章】
【热门文章】