UDP服务器到客户端的通信 - UDP是无状态的,如何绕过路由器?

发布于 2024-10-15 17:50:22 字数 351 浏览 8 评论 0原文

在最近的一系列问题中,我问了很多关于 UDP、boost::asio 和 c++ 的问题。

我最新的问题(在 Stackoverflow 上似乎没有答案)是这样的:

在客户端/服务器应用程序中,要求服务器在任何防火墙中打开端口,以便允许消息进入是完全可以的。然而,为客户做同样的事情绝对不是一个很好的用户体验。

TCP 连接通常可以实现此目的,因为大多数路由器支持状态数据包检查,如果原始请求源自本地主机,则允许响应数据包通过。

我不太清楚这如何与 UDP 一起工作,因为 UDP 是无状态的,并且不存在“响应数据包”之类的东西(据我所知)。我应该如何在我的客户端应用程序中解释这一点?

感谢您的任何答复!

In a recent series of question I have asked alot about UDP, boost::asio and c++ in general.

My latest question, which doesn't seem to have an answer here at Stackoverflow, is this:

In a client/server application, it is quite okay to require that the server open a port in any firewall, so that messages are allowed in. However, doing the same for clients is definately not a great user experience.

TCP-connections typically achieve this due to the fact that most routers support stateful packet inspection, allowing response packets through if the original request originated from the local host.

It is not quite clear to me how this would work with UDP, since UDP is stateless, and there is no such thing as "response packets" (to my knowledge). How should I account for this in my client application?

Thanks for any answers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2024-10-22 17:50:22

UDP 本身是无状态的,但防火墙通常不是。 UDP 的约定是,如果请求从 client:port_A 发送到 server:port_B,则响应将从 server:port_B 返回> 到客户端:port_A

防火墙可以利用这一点。如果它看到客户端发出 UDP 请求,它会在其状态表中添加一个条目,使其能够识别响应,以允许它们进入。因为 UDP 是无状态的并且没有连接终止的指示,所以防火墙将通常会实现超时 - 如果在一定时间内该 UDP 地址对之间没有发生流量,则删除防火墙状态表中的关联。

因此,要在客户端应用程序中利用这一点,只需确保服务器从用于接收请求的同一端口发送回响应即可。

UDP itself is stateless, but the firewall typically is not. The convention on UDP is that if a request goes out from client:port_A to server:port_B, then the response will come back from server:port_B to client:port_A.

The firewall can take advantage of this. If it sees a UDP request go out from the client, it adds an entry to its state table that lets it recognise the response(s), to allow them in. Because UDP is stateless and has no indication of connection termination, the firewall will typically implement a timeout - if no traffic occurs between that UDP address pair for a certain amount of time, the association in the firewall's state table is removed.

So - to take advantage of this in your client application, simply ensure that your server sends responses back from the same port that it uses to receive the requests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文