3

I have a question regarding recv (C, Android).

Is recv guaranteed to return a complete UDP datagram when it returns?

In my case I am using recv to read RTP packets from a socket. The expected length of each RTP packet is 172 (160 bytes for payload and 12 for the header). However, I'm not sure whether I have a guarantee that I'll get the complete 172 bytes when recv returns with data.

Can anybody confirm/comment?

alk
  • 69,737
  • 10
  • 105
  • 255
user1884325
  • 2,530
  • 1
  • 30
  • 49
  • You will probably find this question [Does UDP allow repacketization?](http://stackoverflow.com/questions/15939796/does-udp-allow-repacketization) interesting also. – Cacho Santa May 16 '14 at 23:13

2 Answers2

2

Per POSIX, recv returns the entire UDP packet, unless and error occurs or the buffer is too small for the entire packet. You can detect this by setting the MSG_TRUNC flag, which causes recv to return the frame's actual data length, which you can compare to the buffer size.

DoxyLover
  • 3,366
  • 1
  • 15
  • 19
0

Yes. A UDP datagram is received intact and entire in a single recv() call, or not at all.

user207421
  • 305,947
  • 44
  • 307
  • 483