Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does recv() always return a complete data packet which has the expected length?

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?

like image 763
user1884325 Avatar asked Nov 07 '25 12:11

user1884325


1 Answers

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.

like image 195
DoxyLover Avatar answered Nov 09 '25 07:11

DoxyLover



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!