Sockets in C: read() returns > 0 bytes read, but buffer is zero -
i'm trying read data socket in c. there 2 messages.
i trying send size of message socket read() command know when message ends , can stop reading @ point. otherwise, read both messages 1 blob. problem read command sets buffer 0 though read > 0 bytes.
here code:
n = read(newsockfd, buffer, sizeof(int)); int bytes_to_read = atoi(buffer); fprintf(stdout, "here size of data for: %d", bytes_to_read); /*read message*/ fprintf(stdout, "reading first message.\n"); int bytesread = 0; int chunk = bytes_to_read > 255 ? 255 : bytes_to_read; { bzero(buffer,256); n = read(newsockfd,buffer,chunk); bytes_read += n; fprintf(stdout, "the value of n %d , here new buffer after first read: %s , new buffer length : %d\n", n, buffer, strlen(buffer)); strcat(plaintext, buffer); size += 256; plaintext = realloc(plaintext, size); } while (bytes_read < bytes_to_read);
the output shows n = 36 (as expected) buffer 0 , length of buffer 1.
why read() command setting buffer 0? there easier way tell read() stop reading?
edit: based on feedback got, drastically re-wrote this. note know client send 1 packet containing number of bytes in data unit32_t, send data.
char * readtext(int newsockfd) { char * text = malloc(256); char buffer[256]; int n; int size = 256; /*find out how many bytes in data*/ uint32_t bytes_to_read; n = read(newsockfd, &bytes_to_read, sizeof(uint32_t)); bytes_to_read = ntohl(bytes_to_read); /*read data client*/ int bytes_read = 0; int bytes_left = bytes_to_read; /*the maximum can read @ 1 time 255 bytes since size of buffer. * buffer have been larger we'd still have check data size , keep calling * read() until of data. * if data read smaller buffer, read in 1 go. if data larger, * read in 255 byte-sized chunks until have read of it*/ int chunk = (bytes_to_read > 255) ? 255 : bytes_to_read; { bzero(buffer,256); n = read(newsockfd,buffer,chunk); /*copy on data have read far text buffer offset amount have read in not overwrite data in array*/ memcpy(text + bytes_read, buffer, n); bytes_read += n; bytes_left -= n; /*if have less 255 bytes left, read amount. prevents read() pulling in data , messing next read() call*/ chunk = (bytes_left > 255) ? 255 : bytes_left; size += n; /*grow text variable as read in. makes room next chunk of data. next chunk large 255 bytes*/ text = realloc(text, size); } while (bytes_read < bytes_to_read); text[bytes_read] = '\0'; //null terminate our string can use string functions on it. if (n < 0) error("error reading socket"); return text; }`
there's no guarantee received entire length message.
you're reading
sizeof int
bytes, suggests binary data, you're callingatoi()
, suggests null-terminated ascii data in form of decimal string. it?if you're getting return value of 36 that's how many bytes received, , if buffer contains
{0, 1}
that's received too.it isn't valid use
strlen()
orstrcat()
on arbitrary array of bytes. these functions null-terminated strings. there nothing tcp says every buffer receive null-terminated string, , nothing says receive printable.
what you're doing not valid.
Comments
Post a Comment