In a non blocking socket connect, select() always returns 1

女生的网名这么多〃 提交于 2019-11-30 13:49:23

One problem I see is that you stick s in the fdset before you have created the socket. You need to do the

FD_SET(s, &fdset);

after you've created the socket because s is just an integer and so will not be the right value until after the call to socket().

EDIT

Like this:

.
.
.
SOCKET s;
errno=1;
struct timeval tv;
tv.tv_sec = 15;
set_address( hname, sname, &peer, "tcp" );
s = socket( AF_INET, SOCK_STREAM, 0 );

int n = 1;
fcntl(s, F_SETFL, O_NONBLOCK);

if ( !isvalidsock( s ) )
{
    printf("Socket Call Failed: %s\n", strerror(errno));
    return(0);
}    

FD_ZERO(&fdset);
FD_SET(s, &fdset); // don't put socket in set until it is actually created 

You need to check whether the socket is ready for writing (the second fd_set * argument to select), not reading (the first one).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!