在 LAN 之外测试 UDP 代码时遇到问题?
这几天我在学习udp。 这个周末我将参加一场 72 小时的比赛,我希望比赛结束时我的 UDP 代码可以在线运行。 在比赛期间我不会有任何互联网(所以没有打电话给某人并让他们测试)。
我知道有关 UDP 的一些问题,例如数据包进入两次,没有进入几个帧(但我不知道我应该期望多长时间(以毫秒为单位)),建议的字节大小(576)等。 我应该了解 UDP 编程什么?
但是,发生的一些事情是什么?您从 LAN 转移到互联网后吗?
注意:我将尽快运行一些代码并在线测试。 希望我的最终代码是什么样子,但我也可能会错过一些东西。
I am learning udp in the next few days. This weekend I am going to be in one of those 72hour competition and I would like to have my UDP code work online by the end of it. During the competition I wont have any internet (so no calling someone and having them test).
I know of some of the problems about UDP like packets coming in twice, not coming in for several frames (but I don't know how long in milliseconds I should expect), the recommended byte size (576) etc. What should i know about UDP programming?
But what are some of the things that happen to you after moving from LAN to internet?
NOTE: I will be running some code ASAP and testing it online. Hopefully what my end code will look like but I may also miss a few things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的延迟可能会增加,并且您可能会丢失更多数据包。 这取决于发射器和接收器所在的位置。 如果您在美国并且尝试使用 UDP 与澳大利亚进行通信,您将获得相当高的延迟,您将遭受更多的数据包丢失/重新排序的数据包/重复,因为传输距离更长,数据包的路径也更多被路由。
Your delays may increase and you may get more packet loss. This is dependent on where the transmitter and receiver are. If you are in the US and you are trying to communicate with UDP with Australia, you're going to get pretty high latency you will suffer more packet loss/reordered packets/duplicates because there is more distance to travel and more paths for the packets to be routed.
如果您使用 UDP 作为传输协议,则必须在应用程序协议内保护传输的安全。 有多种方法可以做到这一点:
您应该将这些作为应用程序数据包的标头发送,并在接收端检查它们。
PS:如果您确实依赖于协议的所有方面(而不仅仅是部分)的正确数据包顺序和完整性,您应该切换到 TCP。
If you're using UDP as transport protocol, you have to secure your transmission inside your application protocol. There are several ways to do this:
You should send these as a header of your application packets and check them at the receiving end.
PS: If you really rely on correct packet order and completeness in all aspects of your protocol (not just parts of it) you should switch to TCP.
这可能有点极端(!),但如果这很重要,您可以编写一个 UDP 代理来侦听传入的数据报并转发它们。 然后,您可以修改此设置以提供:
,并引导客户端将数据发送到代理,然后代理将数据直接发送到服务器(通过适当的数据包修改)。
就像我说的,也许是极端的。 但是,即使存在 LAN/互联网,您也可以使用它来测试您的应用程序在 UDP 不友好的环境中的行为。
编辑:肯定有人写过这样的东西吗? 如果没有,我感觉到一个开源项目即将到来......
This may be a bit extreme (!) but if this is important, you could write a UDP proxy that listens for incoming datagrams and forwards them on. You could then modify this to provide:
and direct your client to send data to the proxy, and the proxy to direct on (with appropriate packet modifications) to the server.
Like I say, perhaps extreme. However, even in the presence of your LAN/internet, you would be able to use this to test your application for its behaviour in a UDP-unfriendly environment.
EDIT: Surely someone has written something like this ? If not, I sense an open-source project coming on....
简短的回答是:在 LAN 上,您可能会注意到很少有 UDP 的权衡(好的和坏的),因为在 LAN 上很少有事情会出错。
你可能会丢失数据包。 LAN 拥塞或本地系统限制是最可能的原因。
当您离开 LAN 并依赖于路由器时,大多数 UDP 问题都会出现(碎片、重复数据包、延迟数据包、丢弃超过一定大小的数据包、缺少返回的错误消息)。
换句话说,使 UDP 变得复杂的大部分原因是“离开 LAN”。
The short answer is: on a LAN, you will probably notice very few of the tradeoffs (good and bad) of UDP, because on a LAN few things can go wrong.
You could lose packets. LAN congestion or local system limitations are the most likely cause.
When you leave the LAN, and depend on routers, then the majority of UDP problems will appear (fragmentation, duplicate packets, delayed packets, discarding of packets beyond a certain size, lack of error messages being returned).
In other words, MOST of what makes UDP complicated is "leaving the LAN".