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

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

问题描述

来自乔尔在副驾驶上的帖子:

直接连接!我们一直都在做 我们能确保的一切 雾溪副驾驶可以连接任何 联网情况,无论如何 防火墙或NAT已到位.到 做到这一点,双方都做到 与我们服务器的出站连接, 代表他们中继流量. 好吧,在很多情况下,这不是 必要的.所以2.0版可以 相当聪明的东西:它建立了 通过我们的初步联系 服务器,这样您就可以正确连接 拥有100%的可靠性.但是之后 所有人建立连接后,它就会安静地 在后台,寻找一种方法 直接建立联系.如果不能的话 没什么大不了的:您只要继续中继 通过我们的服务器.如果可以 直接对等连接 默默地将您的数据转移到 直接连接.你不会注意到 除了,可能更快 交流.

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

解决方案

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

这些程序已经可以通过Joel的服务器相互通信,因此它们可以与Joel的服务器相互交换信息.此外,Joel有自己的外部IP地址,他们还提供Joel有关其内部IP地址的信息.

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

计算机B做同样的事情,但是由于A的路由器打开了一个与B发送的端口匹配的端口/IP组合,因此它的消息传递到了A(这里发生了一些端口魔术,这很简单,但是可行). /p>

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

因此,这实际上非常简单,但是该实现具有详细信息,尤其是有关如何将端口分配给新的TCP连接以及NAT路由器通常如何处理TCP请求以及它们如何映射到外部端口的细节. .这些细节是有趣且困难的.

-亚当

From a Joel's post on Copilot:

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.

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:

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.

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.

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'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.

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.

-Adam

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

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