Is this the proper way to iterate over a read on a socket? I am having a hard time getting this to work properly. data.size is an unsigned int that is populated from the socket as well. It is correct. data.data is an unsigned char *.
if ( data.size > 0 ) { data.data = (unsigned char*)malloc(data.size); memset(&data.data, 0, data.size); int remainingSize = data.size; unsigned char *iter = data.data; int count = 0; do { count = read(connect_fd, iter, remainingSize); iter += count; remainingSize -= count; } while (count > 0 && remainingSize > 0); } else { data.data = 0; }
Thanks in advance.
You need to check the return value from read before you start adding it to other values.
You’ll get a zero when the socket reports EOF, and -1 on error. Keep in mind that for a socket EOF is not the same as closed.