UDP服务器到客户端的通信 - UDP是无状态的,如何绕过路由器?
在最近的一系列问题中,我问了很多关于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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
toserver:port_B
, then the response will come back fromserver:port_B
toclient: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.