If we want to
examine the data but still leave it on the receive queue for some
other part of our process to read, we can use the MSG_PEEK
flag (Figure 14.6). If we
want to do this, but we are not sure that something is ready to be
read, we can use this flag with a nonblocking socket or combine
this flag with the MSG_DONTWAIT flag.
Be aware that the amount of data on the receive
queue can change between two successive calls to recv for
a stream socket. For example, assume we call recv for a
TCP socket specifying a buffer length of 1,024 along with the
MSG_PEEK flag, and the return value is 100. If we then
call recv again, it is possible for more than 100 bytes to
be returned (assuming we specify a buffer length greater than 100),
because more data can be received by TCP between our two calls.
In the case of a UDP socket with a datagram on
the receive queue, if we call recvfrom specifying
MSG_PEEK, followed by another call without specifying
MSG_PEEK, the return values from both calls (the datagram
size, its contents, and the sender's address) will be the same,
even if more datagrams are added to the socket receive buffer
between the two calls. (We are assuming, of course, that some other
process is not sharing the same descriptor and reading from this
socket at the same time.)