处理 UDP 套接字的发送失败 [英] Dealing with sendto failure for UDP socket

查看:104
本文介绍了处理 UDP 套接字的发送失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果根据手册页发送失败

If sendto fails according to the manpage

成功时,这些调用返回发送的字符数.错误时,返回-1,并正确设置 errno."

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

我知道 TCP 肯定是这种情况,您应该真正尝试发送 Beej 网络编程指南中指出的剩余数据.

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.

然而,部分发送 UDP 数据包对我来说没有意义,而这个评论似乎暗示了这一点.

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

如果消息太长无法通过底层协议原子传递,则返回错误EMSGSIZE,消息不传输.

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

有人可以向我确认,如果我使用 UDP 数据包调用 sendto(或发送),如果它实际上不适合出站缓冲区,那么我将返回 -1,并将 errno 设置为 EMSGSIZE 并且没有部分发送与流 (TCP) 套接字一样?

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?

推荐答案

没有隐藏含义,函数只是返回发送的字节数.它是 Unix API 的标准模式.数据报要么全有要么全无,如果网络导致碎片发生,接收会更复杂,但通常堆栈会隐藏所有细节,并在重建时呈现每个完整的数据包.

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.

这篇关于处理 UDP 套接字的发送失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆