UDP non-blocking socket on a real-time OS: sendto() and recvfrom() can return with partial message?

百般思念 提交于 2021-02-08 04:38:47

问题


This is my first message here. I'm working with a non-blocking UDP socket on a real-time OS (OnTime and VxWorks). I have read the documentation and some forums but I have some doubts about 'atomicity' of sendto() and recvfrom() functions:

  • sendto() returns the number of bytes enqueued or error. Is it possible that it's less then the input buffer length? Maybe the output buffer has not enough free space and just few bytes are enqueued...

  • recvfrom() returns the number of byte received or error. Is it possible that it's less then the size of message the source has sent? I mean partial message reading...

I hopes reading and writing functions are atomic (full message or no message read/write).

Thanks. Emanuele.


I asked to OnTime support and they told me that it's possible that sendto() will enqueue a partial message if the output buffer has not enough free space. I don't know if also recvfrom() could return a partial message in some cases. I suppose that there's no standard behavior on socket implementations among different OS.


回答1:


I'm not really familiar with these systems, but I would be very surprised if they break normal UDP socket semantics, which is always to enqueue a full datagram on "send" and to dequeue a full single datagram on a "receive".




回答2:


sendto() returns the number of bytes enqueued or error. Is it possible that it's less then the input buffer length?

No. It's sent wholly, or not at all for UDP.

recvfrom() returns the number of byte received or error. Is it possible that it's less then the size of message the source has sent? I mean partial message reading...

If the buffers of the OS stack can't hold an entire UDP packet, it is dropped. If your application buffers can't hold the entire packet, you get the initial content of the packet.

i.e. you can read a partial message in just one case, that is if the data cannot fit in the buffer you pass to sendto(). In that case the rest of the packet is discarded. With recvmsg() you can detect if the packet was truncated, but this is normally resolved by either using a max sized buffer (UDP must fit in an IP packet which MTU is 2^16-1), or by designing the protocol you use inside UDP where you set your own reasonable max size.



来源:https://stackoverflow.com/questions/14320622/udp-non-blocking-socket-on-a-real-time-os-sendto-and-recvfrom-can-return-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!