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) torecv().In the 2nd example, you are passing the value of
blockSizetorecv(), type-casted as a pointer. SinceblockSizeis 0, the pointer is effectivelynullptr. You need to pass the address ofblockSizeinstead. Also,size_tis not guaranteed to be 4 bytes, so use(u)int32_tinstead, eg: