UDP 客户端 - 打开端口?

发布于 2024-11-19 03:18:26 字数 144 浏览 2 评论 0原文

所以现在我只为我的客户端使用 TCP - 它们连接到服务器,打开套接字并自由获取数据包。 但是,如果我决定在游戏中也使用 UDP,该怎么办?他们必须开放港口吗?例如,如果他们使用常规 WiFi,我可以将 UDP 发送到客户端而不会出现打开端口问题吗?

谢谢。

So right now I'm using only TCP for my clients - they connect to the server, open socket and freely getting packets.
But what if I will decide to use also UDP in my game? Will they gonna have to open ports? For example, if they are using a regular WiFi, can I send UDP to the client without having opening ports problem?

Thanks.

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

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

发布评论

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

评论(1

彼岸花ソ最美的依靠 2024-11-26 03:18:26

TCP 和UDP 只是传输层实现的两个示例。它们都使用术语“端口”来确定哪个应用程序应该接收传入数据包,但它们可以通过路由器/交换机/防火墙/等进行不同的路由/过滤。

所以答案是否定的。打开端口时您也会遇到类似的问题。除了“TCP端口xxx应该打开”之外,您还必须要求“UDP端口xxx应该打开”。

在大多数家庭网络中,防火墙规则允许将传出数据包(请求)发送到任何远程端口(例如,在您的服务器上,应打开此端口)。当这样的数据包通过路由器时,它会创建临时规则以允许答案返回到请求数据包的本地端口。

因此,正常情况是这样的:

  1. 数据包源自 IP 5.5.5.5 的家庭计算机。假设它有源 UDP 端口 55555、源 IP 地址 5.5.5.5 和目标端口 8888
  2. 数据包到达家庭路由器。由于它是从内部发出的 - 路由器允许它通过并创建 2 分钟的规则,以允许目标为 5.5.5.5 的数据包到达 UDP 端口 55555
  3. 数据包在您的服务器之前到达公司路由器。它有规则传递数据包到端口 8888,因此数据包被允许通过。
  4. 您的服务器接收数据包并处理它。作为响应,它会为 IP 5.5.5.5 和 UDP 端口 55555 创建数据包。
  5. 公司路由器允许响应进行。
  6. 家庭路由器允许响应按照临时规则进行。
  7. 您的计算机收到响应。

为了确保安全,公司计算机和路由器通常会受到更多限制,因此如果您的用户(IP 5.5.5.5)位于公司网络中,第二点可能会限制数据包。

它非常简单,因为实际上几乎总是有像 NAT 这样的东西,并且规则更复杂......但总的来说,它给出了它内部如何工作的想法。

TCP and UDP are just two examples of transport layer implementations. Both of them are using term 'port' to determine which app should receive incoming packet, but they could be routed/filtered differently by routers/switches/firewalls/etc.

So the answer is no. You will have similar problems with opening ports. Just except 'TCP port xxx should be opened' you have to demand 'UDP port xxx should be opened'.

In most home networks firewall rules allow outgoing packets (requests) to any remote port (on your server for example, where this port should be opened). And when such a packet goes through a router - it creates temporary rule to allow answers come back to the local port from which request packet.

So, normal scenario is like that:

  1. Packet originated from home computer with IP 5.5.5.5. Lets say it has source UDP port 55555, source IP address 5.5.5.5 and destination port 8888.
  2. Packet reaches home router. As it is going from inside - router allows it to pass through and creates rule say for 2 minutes to allow packets targeted to 5.5.5.5 to UDP port 55555.
  3. Packet reaches corporate router before your server. It has rule to pass packets for port 8888 so packet is allowed to go.
  4. Your server receives the packet and processes it. In response it creates packet for IP 5.5.5.5 and UDP port 55555.
  5. Corporate router allows response to go.
  6. Home router allows response to go according to temporary rule.
  7. Your computer receives the response.

Corporate computers and routers often more restrictive to ensure security, so second point could restrict packet if your user (IP 5.5.5.5) is in corporate network.

It is very simplified as in reality there's almost always things like NAT and rules are more complex... But in general it gives the idea how it works internally.

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