UDP sockets and MSG_PEEK

Viewed 699

When we use recvfrom() to read a packet from UDP socket, we cannot read it partially. Because if we read a small part of UDP packet first(by passing a small buffer), the reminder of packet is dropped as mentioned here:

All receive operations return only one packet. When the packet is smaller than the passed buffer, only that much data is returned; when it is bigger, the packet is truncated and the MSG_TRUNC flag is set.

But I wonder if the same thing happens if we just inspect packet using MSG_PEEK flag. Will reminder of packet is dropped if I just peek the UDP message?

1 Answers

Will reminder of packet is dropped if I just peek the UDP message?

Nothing will be dropped, since with the MSG_PEEK flag set, the state of the socket's incoming-data-buffer is not modified; the entire packet will remain in the socket's buffer.

Only the first part of the packet's data will be copied into your too-small destination-data-buffer, of course.

Related