recv: how many bytes will I get

20 views
Skip to first unread message

AJ ONeal

unread,
Jan 18, 2011, 11:07:30 PM1/18/11
to uta...@googlegroups.com
According to the man page of recv, it looks like this should work:

length = recv(socket_fd, NULL, 0, MSG_TRUNC);

However, it always returns -1.

char c;
length = recv(socket_fd, &c, 1, MSG_TRUNC | MSG_PEEK);

Always returns 1



How can I tell how many bytes I will be receiving before I receive them?

AJ ONeal

AJ ONeal

unread,
Jan 19, 2011, 5:56:55 PM1/19/11
to uta...@googlegroups.com
[No] Solution:

There is no stardand-c way to tell how many bytes are waiting to be received on a **stream** socket.
Try as you might, it cannot be done without first copying the memory out.

MSG_TRUNC should work for **packet** sockets (datagram)


And the reason I kept getting 1 byte was because the socket was in non-blocking mode and I was getting the byte as soon as it was ready, before the next bytes had filled the buffer.

AJ ONeal
Reply all
Reply to author
Forward
0 new messages