This does work:
char blockSize[4];
int r = recv(socket, blockSize, 4, 0);
This does not work. It always returns zero, why?
size_t blockSize = 0;
int r = recv(socket, (char*)blockSize, 4, 0);
It's not a major problem, I know how to convert the char array back to a size_t. But I just want to know why it's not working.
In the 1st example, you are passing the address of blockSize
(via array decay) to recv()
.
In the 2nd example, you are passing the value of blockSize
to recv()
, type-casted as a pointer. Since blockSize
is 0, the pointer is effectively nullptr
. You need to pass the address of blockSize
instead. Also, size_t
is not guaranteed to be 4 bytes, so use (u)int32_t
instead, eg:
uint32_t blockSize = 0;
int r = recv(socket, (char*)&blockSize, 4, 0);