UDP数据传输可能出现哪些错误?
我们将开发一款支持互联网多人游戏的游戏。因为它是一个交互式游戏,我知道我必须使用 UDP 来减少连接延迟,但我想知道使用 UDP 连接传递的包中可能会出现哪些错误?我到处都看到他们说 UDP 提供“尽力而为”,但没有人给出完整的解释这是什么意思。阅读一些文章后,我仍然有两个问题:
- 是否可以发送包裹并在连接的另一端接收其中的一部分?
- 如果您对第一个问题的回答是正确的,那么下一个包裹会发生什么?我应该等待包的其余部分,还是可以假设下一个包从我的下一个
recv
调用开始?
对于我们的游戏,我认为我们需要每秒发送 4 个大约 20 字节的包。
we're going to develop a game with internet multiplayer support. since it's an interactive game I know I have to use UDP to reduce connection latency, but I'm wondering what are the possible errors that may occur in a package delivered using UDP connection? everywhere I looked they say UDP provides "Best effort delivery", but no one does give a complete explanation what does it mean. after reading some article there are two questions that I still have:
- Is it possible to send a package and receive part of it at the other end of connection?
- if your answer to first quesiton is true what would happen to the next packages? should I wait for the rest of package or can I assume next package start with my next
recv
call?
for our game I think we will need to send 4 packages of around 20 byte each second.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最常见的情况是:一侧发送消息,另一侧什么也没收到。
事实并非如此,即使消息很大并且变得支离破碎。与
TCP
不同,在UDP
中,每条消息都是独立的。你要么完全得到它,要么什么也得不到。所以你应该做的只是在循环中
recvfrom
事物并处理它们。显然,您应该使您的应用程序不受消息丢失的影响,这样丢失的消息就不会使其崩溃。The most common thing that can happen is: one side sends a message, the other receives nothing.
Not really, not even when the message is huge and it gets fragmented. Unlike in
TCP
, inUDP
every message is independent. You either get it entirely or nothing at all.So what you should do it just
recvfrom
things in a loop and process them. Obviously you should make your application impervious to message loss such that a missing message doesn't crash it.