PersianGulf
PersianGulf

Reputation: 2885

how to find out socket has data in C++

I wrote the following function and it's working fine:

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;

}

Above function when socket is ready for work return true, Do you have any idea if I test socket has data how I test it?

Upvotes: 1

Views: 5487

Answers (1)

user207421
user207421

Reputation: 311054

when socket is ready for work return true

No. It returns true when a read() can be executed without getting EWOULDBLOCK/EAGAIN, i.e. if there is something that can be read immediately without blocking.

Do you have any idea if i test socket has data

You've already found it.

Upvotes: 4

Related Questions