linux 非阻塞connect errno什么情况下不为einprogress
的有关信息介绍如下:当connect在非阻塞模式下,会出现返回-1值,错误码是EINPROGRESS,但如何判断connect是联通的呢?stevens书中说明要在connect后,继续判断该socket是否可写?若可写,则证明链接成功。如何判断可写,有2种方案,一种是select判断是否可写,二用poll模型。select:int CheckConnect(int iSocket){fd_set rset;FD_ZERO(&rset);FD_SET(iSocket, &rset);timeval tm;tm. tv_sec = 0;tm.tv_usec = 0;if ( select(iSocket + 1, NULL, &rset, NULL, &tval) <= 0){ close(iSocket); return -1;}if (FD_ISSET(iSocket, &rset)){ int err = -1; socklen_t len = sizeof(int);if ( getsockopt(iSocket, SOL_SOCKET, SO_ERROR ,&err, &len) < 0 ){ close(iSocket); printf("errno:%d %s\n", errno, strerror(errno)); return -2;}if (err){ errno = err; close(iSocket); return -3;}}return 0;}poll:int CheckConnect(int iSocket) { struct pollfd fd; int ret = 0; socklen_t len = 0; fd.fd = iSocket; fd.events = POLLOUT; while ( poll (&fd, 1, -1) == -1 ) { if( errno != EINTR ){ perror("poll"); return -1; } } len = sizeof(ret); if ( getsockopt (iSocket, SOL_SOCKET, SO_ERROR, &ret, &len) == -1 ) { perror("getsockopt"); return -1; } if(ret != 0) { fprintf (stderr, "socket %d connect failed: %s\n", iSocket, strerror (ret)); return -1; } return 0;}