当前位置:问百问>百科问答>linux 非阻塞connect errno什么情况下不为einprogress

linux 非阻塞connect errno什么情况下不为einprogress

2024-08-04 09:50:32 编辑:zane 浏览量:575

linux 非阻塞connect errno什么情况下不为einprogress

的有关信息介绍如下:

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;}

版权声明:文章由 问百问 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.wenbwen.com/answer/128528.html
热门文章