locked
wm5中将socket设成non-blocking,然后调用connect,怎么还是block了啊? RRS feed

  • 问题

  • 是不是在wm5中,non-blocking IO不支持connect调用啊?

    我的代码如下:
    WSADATA wsadata;
    SOCKET sock;
    char hostname[MAX_PATH];


    if( 0 != WSAStartup(MAKEWORD(2,2), &wsadata) )
    {
    return FALSE;
    }

    sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if( INVALID_SOCKET == sock )
    {
    WSACleanup();
    return FALSE;
    }

    // set socket as an non-blocking connection
    unsigned long ul = 1;
    int ret = ioctlsocket(sock, FIONBIO, (unsigned long*)&ul);
    if(ret==SOCKET_ERROR)
    {
    closesocket( sock );
    WSACleanup();
    return FALSE;
    }


    SOCKADDR_IN skaddr;
    skaddr.sin_family = AF_INET;
    skaddr.sin_port = htons( port );

    struct hostent *hp;
    hp = gethostbyname( hostname );
    if( 0 == hp )
    skaddr.sin_addr.S_un.S_addr = inet_addr( hostname );
    else
    memcpy((char*)&skaddr.sin_addr, (char*)hp->h_addr, hp->h_length);

    ret =connect( sock, (SOCKADDR*)&skaddr, sizeof(SOCKADDR_IN) );  // 调用connect,sock还是被阻断了
    if( 0 != ret)
    {
    closesocket( sock );
    WSACleanup();
    return FALSE;
    }

    2007年11月12日 4:00