在线游戏/其他服务的端口转发?
我最近注意到我不必为我玩的大型多人在线角色扮演游戏转发端口。
我正在考虑开发一款人们可以在线玩的游戏,并有一个问题。
鉴于其双向套接字连接不断来回发送数据,为什么会出现这种情况?他们的服务器不需要通过我的防火墙才能连接到我吗?
I've noticed recently that I don't have to forward ports for mmorpg's that I play.
I'm thinking about working on a game that people can play online and had a question.
Why is this the case given its a two way socket connection that is constantly sending data back and forth? Doesn't their server need to get through my firewall in order to connect to me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
TCP速成课:TCP是一种双向协议。挑战在于至少需要一台主机来启动。由于在 MMORPG 中,您自己的计算机永远不会充当服务器,因此没有人需要连接到它。有关游戏状态的所有信息都通过公司面向公众的服务器传递,这些服务器具有面向公众的 IP 地址(嘿,也许他们实际上在那里使用端口转发,只是为了混淆我的解释......但你永远不必看到他们漂亮的网络内部结构、代理和其他魔法。)。
无论如何,当您连接到 Stackoverflow 时,您将建立一个出站连接,从服务器请求数据,然后通过同一连接接收返回的数据。同样的场景,只是使用网络服务器而不是游戏。
TCP crash lesson: TCP is a two-way protocol. The challenge is that at least one host needs to initiate. Since within an MMORPG, your own computer is never acting as a server, nobody has to connect to it. All the information about game state is passed through the company's public facing servers that have public facing IP addresses (and hey, maybe they actually use port forwarding there, just to confuse my explanation... but you never have to see their pretty network internals, proxies, and other wizardry.).
Anyway, when you connect to Stackoverflow, you're making one outbound connection that requests data from the server, and then over that same connection you're receiving it back. Same exact scenario, only with a webserver instead of a game.
UPnP 允许您处理许多路由器。如果您有权访问第三方,还可以使用 NAT 穿透不在路由器后面。
无论哪种方式,仅当您希望充当服务器(或 P2P 关系中的发送者)时才需要端口转发。客户端不需要转发端口。
UPnP allows you to tackle many routers. There's also NAT Punch-Through if you have access to a third party that isn't behind a router.
Either way, port forwarding is only necessary if you wish to act as a server (or the sender in a P2P relationship). A client does not need to forward ports.
尽管数据也会传入,但您也不需要转发端口来访问网络。
当您建立传出 TCP 连接时,NAT 路由器会将连接放入表中,以便当数据传入时,它知道将数据包发送到 LAN 中的哪台计算机。
You don't need to forward ports to access the web either, despite data coming in as well.
When you make an outgoing TCP connection, your NAT router puts the connection in a table, so that when data comes in, it knows what machine in your LAN to send the packet to.
每个人都提到 TCP,但 NAT 也适用于 UDP:第一个传出 UDP 数据包将该源端口与内部 IP 地址相关联,并且您的 NAT 设备会将传入该端口的流量转发到内部网络上的正确主机。
Everyone mentions TCP, but NAT works for UDP as well: The first outgoing UDP packet associates that source port with the internal IP address, and your NAT device will forward incoming traffic to that port to the correct host on the internal network.
换句话说,如果您的计算机首先请求连接(出站),路由器会自动打开该端口,假设您需要返回数据。但是,如果您希望远程用户在您的计算机没有请求的情况下连接到您的计算机,则路由器通常会丢弃数据包,因为它不知道将它们发送到哪里(它们是未经请求的)。因此,您需要告诉路由器将端口 N 上的任何未经请求的数据包传送到您的计算机。
抱歉这么晚才添加另一个答案,我知道一个答案已经被接受,但我个人发现其他答案比这个简单的解释更令人困惑。
In other words, if your computer requests the connection (outbound) first, the router opens up the port automatically, on the assumption that you're going to want data back. But if you want remote users to connect to your computer without your computer requesting it, the router would normally drop the packets since it wouldn't know where to send them (they were unsolicited). So instead, you need to tell the router to deliver any unsolicited packets at port N to your computer.
Sorry to add another answer so late, and I know one was already accepted, but I personally found the other answers to be more confusing than this simple explanation.