Dealing with sendto failure for UDP socket

回眸只為那壹抹淺笑 提交于 2021-02-10 14:16:28

问题


If sendto fails according to the manpage

"On success, these calls return the number of characters sent. On error, -1 is returned, and errno is set appropriately."

I know that with TCP that is definately the case and you should really attempt to send the remaining data as pointed out in Beej's guide to network programming.

However, partially sending a UDP packet makes no sense to me, and this comment seems to imply it.

If the message is too long to pass atomically through the underlying protocol, the error EMSGSIZE is returned, and the message is not transmitted.

Can someone confirm for me that if I call sendto (or send) with a UDP packet that if it actually doesn't fit in the outbound buffer then I'll get -1 returned with errno set to EMSGSIZE and no partial send as with a stream (TCP) socket?


回答1:


There is no hidden meaning, the function just returns the count of bytes sent. It is a standard pattern for Unix APIs. Datagrams are all or nothing delivery, receipt is more complicated if the network caused fragmentation to occur but generally the stack hides all the details and presents each complete packet as it is reconstructed.




回答2:


EMSGSIZE indicates that "the socket requires that the message be sent atomically, but the size of the message to be sent makes this impossible" (see man sendto).

However, the outbound buffer being full isn't necessarily the reason - Linux (for instance) apparently won't fragment UDP packets by default (see man udp).



来源:https://stackoverflow.com/questions/3931570/dealing-with-sendto-failure-for-udp-socket

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