The len
argument to recv
specifies the maximum number of bytes to read. There is no guarantee that recv
will block until it reads exactly this amount; it may return after reading any smaller number of bytes. The actual amount of data read is noted in the return value.
You need to call recv
repeatedly yourself if you want to read a set number of bytes
int read_exact(SOCKET s, char* buf, int len)
{
int remaining = len;
do {
int ret = recv(s, buf, remaining, 0);
if (ret == 0) {
break;
}
if (ret != SOCKET_ERROR ) {
remaining -= ret;
buf += ret;
}
} while (ret != SOCKET_ERROR && remaining > 0);
return (ret==SOCKET_ERROR? SOCKET_ERROR : len - remaining);
}