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:
Expand|Select|Wrap|Line Numbers
- 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;
- }
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.