I'm trying to figure out how to avoid recv() calls that hang forever by using using a non-blocking socket along with the select() function. This seems to be the standard way to avoid recv() calls that hang forever.
I'm having difficulty getting this to work however. It seems to work sometimes, but occasionally the recv() call hangs. Additionally, I think the loop I'm using is not doing something right, because it slows down the system indicating it is using a lot of CPU cycles.
Here is the recv() function I used:
bool recv_non_block(char* buf)
{
int status;
do {
if ((status = recv(m_sock, buf, RECV_BUF_SIZE, 0)) == -1) {
if (errno == EWOULDBLOCK || errno == EAGAIN) {
if (Select()) continue;
}
else return false;
}
} while (status != 0);
return true;
}
Note: The Select() function is simply a convenient wrapper for the select() function call.
Is the above loop a correct way to receive on a non-blocking socket? Note also that since this can be used on an Internet website, I do not necessarily know the size of the data I am receiving, and therefore I need to rely on recv() returning 0 in order to end the loop.
Start Free Trial