什么时候需要IPPROTO_UDP?
什么时候需要IPPROTO_UDP?
是否存在 UDP不是 SOCK_DGRAM 的默认协议的情况? (真实案例,而不是假设的“可能是”,请“)
即,在什么情况下以下两行不会产生相同的行为?
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
if ((s=socket(AF_INET, SOCK_DGRAM, 0))==-1)
When is IPPROTO_UDP required?
Is there ever a case where UDP is not the default protocol for SOCK_DGRAM? (real cases, not hypothetical "it might be", please")
i.e., what are the situations where the following two lines would not produce identical behavior?
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
if ((s=socket(AF_INET, SOCK_DGRAM, 0))==-1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
某些操作系统(例如 2.6.20 之后的 Linux 内核)支持 SOCK_DGRAM 的第二种协议,称为 UDP-Lite。如果您的系统支持,则可以通过提供 IPPROTO_UDPLITE 作为 socket() 调用的第三个参数来启用它。
它与普通 UDP 的区别在于允许仅对数据报的一部分应用校验和。 (通常,UDP 校验和是一种全有或全无的努力。)这样,如果校验和区域之外的某些片段可能在传输过程中丢失,协议可以更好地抵抗由于分段传输而导致的校验和失败。只要覆盖校验和部分的片段被成功接收,尽可能多的数据报仍将被传递到应用程序。
为了与现有代码向后兼容,我怀疑(但我不能保证)调用套接字(AF_INET,SOCK_DGRAM,0)将继续默认为普通UDP,即使在另外支持UDP-Lite的系统中也是如此。
Some operating systems (eg. Linux kernel after 2.6.20) support a second protocol for SOCK_DGRAM, called UDP-Lite. If supported by your system, it would be enabled by providing IPPROTO_UDPLITE as the third argument to the socket() call.
It is differentiated from normal UDP by allowing checksumming to be applied to only a portion of the datagram. (Normally, UDP checksumming is an all-or-nothing effort.) That way, the protocol can be more resistant to checksum failures due to fragmented transmission, in the event that some fragments outside the checksummed area may have been lost in transit. As long as the fragments covering the checksummed portion were successfully received, as much of the datagram as possible will still be delivered to the application.
For backwards compatibility with existing code, I suspect (but I cannot guarantee) that the call socket(AF_INET,SOCK_DGRAM,0) will continue to default to normal UDP, even in systems that additionally support UDP-Lite.
鉴于这些声明:
Linux 中的 ip(7) 手册页显示:
你的问题中的这两行总是会产生相同的结果。
Given these declarations:
the ip(7) manual page in linux says:
Those two lines in your questions will always produce the same result.