P2P 应用程序中的直接 TCP/IP 连接 [英] Direct TCP/IP connections in P2P apps

查看:22
本文介绍了P2P 应用程序中的直接 TCP/IP 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘自 Joel 在 Copilot 上的帖子:

直连!我们一直做我们竭尽所能确保Fog Creek Copilot 可以连接任何网络情况,不管怎样防火墙或 NAT 已就位.到使这发生,双方使到我们服务器的出站连接,代表他们中继流量.嗯,在很多情况下,这不是必要的.所以 2.0 版确实如此一些相当聪明的东西:它设置通过我们的初始连接服务器,因此您可以正确连接100% 的可靠性.但是之后一旦你们都连接上了,它就会悄悄地,在后台,寻找一种方法建立直接联系.如果不能,没什么大不了的:你只是继续中继通过我们的服务器.如果你可以做一个直接点对点连接,它默默地将您的数据转移到直接连接.你不会注意到除了,可能,快得多沟通.

Direct Connect! We’ve always done everything we can to make sure that Fog Creek Copilot can connect in any networking situation, no matter what firewalls or NATs are in place. To make this happen, both parties make outbound connections to our server, which relays traffic on their behalf. Well, in many cases, this isn’t necessary. So version 2.0 does something rather clever: it sets up the initial connection through our servers, so you get connected right away with 100% reliability. But then once you’re all connected, it quietly, in the background, looks for a way to make a direct connection. If it can’t, no big deal: you just keep relaying through our server. If you can make a direct peer-to-peer connection, it silently shifts your data onto the direct connection. You won’t notice anything except, probably, much faster communication.

他们如何将服务器连接更改为 P2P 连接?

How do they change the server connection to a P2P connection?

推荐答案

这非常棘手和有趣.我确定我有一些细节错误,但概述是这样的:

It's pretty tricky and interesting. I'm sure I have some details wrong, but the overview is this:

程序之间已经可以通过 Joel 的服务器相互通信,因此它们可以相互交换信息,也可以与 Joel 的服务器交换信息.此外,Joel 有他们的外部 IP 地址,他们向 joel 提供有关其内部 IP 地址的信息.

The programs can already talk to each other through Joel's server, so they can exchange information with each other and Joel's server. Further, Joel has their external IP addresses, and they give joel information about their internal IP addresses.

他们决定尝试这种打孔技术.计算机 A 使用 B 的外部 IP 地址启动与计算机 B 的 TCP 连接.它不会通过,但它所做的是告诉 A 的路由器它需要允许在给定端口上来自 B 的传入数据包.

They decide to try this hole punch technique. Computer A initiates a TCP connection with Computer B using B's external IP address. It won't go through, but what it does is tell's A's router that it needs to allow incoming packets from B on a given port.

计算机 B 做同样的事情,但它的消息通过 A,因为 A 的路由器打开了一个与 B 发送的内容匹配的端口/IP 组合(这里发生了一些端口魔法 - 这很重要,但可行).

Computer B does the same thing, but its message gets through to A since A's router opened a port/ip combination that matches what B sent (there's some port magic that happens here - this is non trivial, but doable).

B 的路由器记住 B 在给定的端口和 IP 上发起了与 A 的连接,因此 A 的数据包现在也正确地通过其路由器流入 B.

B's router remembers that B initiated a connection with A on a given port and IP, and so A's packets now flow into B past their router correctly as well.

所以它实际上非常简单,但是实现有细节,特别是关于如何将端口分配给新的 TCP 连接,以及 NAT 路由器如何通常处理 TCP 请求以及它们如何映射到外部端口.这些细节很有趣,也很困难.

So it's actually pretty straight forward, but the implementation has details, especially regarding how ports are given to new TCP connections, and how NAT routers typically deal with TCP requests and how they map to external ports. These details are the interesting, and difficult, bit.

-亚当

这篇关于P2P 应用程序中的直接 TCP/IP 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆