Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.
Can't get socket peek to work
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!!