我编写了以下函数,并且运行良好:

bool NetworkSocket::isSocketReady()
{

    /// Got here because iSelectReturn > 0 thus data available on at least one descriptor
    // Is our socket in the return list of readable sockets
    bool             res;
    fd_set          sready;
    struct timeval  nowait;

    FD_ZERO(&sready);
    FD_SET((unsigned int)this->socketFD,&sready);
    //bzero((char *)&nowait,sizeof(nowait));
    memset((char *)&nowait,0,sizeof(nowait));

    res = select(this->socketFD+1,&sready,NULL,NULL,&nowait);
    if( FD_ISSET(this->socketFD,&sready) )
        res = true;
    else
        res = false;


    return res;

}

当套接字准备工作时,上述功能返回true,您是否知道我测试套接字是否有数据进行测试?

最佳答案



否。当可以在不获取EWOULDBLOCK / EAGAIN的情况下执行read()时,即如果可以立即读取某些内容而不会阻塞,则返回true。



您已经找到了。

08-16 01:49