对 UDP 数据包进行校验和的正确方法是什么
我正在用 C# 构建 UDP 服务器应用程序。我遇到了数据包校验和问题。
您可能知道,每个数据包都应该携带一些简单的方法来告诉接收者数据包数据是否完整。
现在,UDP 已经将 2 字节校验和作为标头的一部分,这是可选的,至少在 IPv4 世界中是这样。替代方法是将自定义校验和作为每个数据包中数据部分的一部分,并在接收器上进行验证。
我的问题归结为:依靠 UDP 数据包标头中的(可选)校验和更好,还是将自定义校验和实现作为数据包数据部分的一部分更好?
也许正确的答案取决于具体情况(像往常一样),所以这里的一种情况是,即使代码是在 Windows 上用 .NET 编写和开发的,它可能必须在独立于平台的 Mono.NET 下运行,因此最终的解决方案应该是兼容的与其他平台。我相信自定义校验和算法很容易移植,但我对第一个不太确定。
有什么想法吗?
另外,欢迎对数据包校验和进行大喊大叫。
I'm building UDP server application in C#. I've come across a packet checksum problem.
As you probably know, each packet should carry some simple way of telling receiver if packet data is intact.
Now, UDP already has 2-byte checksum as part of header, which is optional, at least in IPv4 world. Alternative method is to have custom checksum as part of data section in each packet, and to verify it on receiver.
My question boils down to: is it better to rely on (optional) checksum in UDP packet header, or to make a custom checksum implementation as part of packet data section?
Perhaps the right answer depends on circumstances (as usual), so one circumstance here is that, even though code is written and developed in .NET on Windows, it might have to run under platform-independent Mono.NET, so eventual solution should be compatible with other platforms. I believe that custom checksum algorithm would be easily portable, but I'm not so sure about the first one.
Any thoughts?
Also, shouts about packet checksumming in general are welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
需要统计错误吗?如果 UDP 校验和错误,那么任何路由器都可能会丢弃数据包,而您将永远不会得到任何东西。
当然,如果标头中存在错误(例如目标 IP 地址),您也不会得到它。
此外,UDP 校验和算法是完全标准化的,尽管打开或关闭校验和的函数调用确实因平台而异,尽管它通常涉及setsockopt。
Do you need to count errors? If the UDP checksum is wrong then any router could just throw the packet away and you'd never get anything.
Of course, if there's an error in the header (such as destination IP address) you also wouldn't get it.
Also, the UDP checksum algorithm is completely standardized, although the function call to turn checksums on or off does vary by platform, although it usually involves setsockopt.