I am trying to peek the number of received bytes in a socket using the recv() function with the MSG_PEEK flag.
So I wrote a line:
maxToread = recv(source_fd, tmpBuffer, maxToRead, MSG_PEEK | MSG_DONTWAIT);
and then if maxToRead > 0 (there is something to be read) I do:
readn = recv(source_fd, buffer, msgLen, MSG_DONTWAIT);
According to the Man pages, the expected behavior when setting the MSG_PEEK flag is that the data is not removed from the reception buffer so that the next read returns the same data.
In my case I am seeing that, after the first 'recv()' call (with MSG_PEEK), the second reads 0 bytes instead of reading the same data the first call read.
This is quite annoying because I need to be able to check if there is data in the buffer without consuming it. Am I missing something here? Did I misunderstand the MSG_PEEK behavior?
Any help appreciated!!