如何建立P2P连接时,两个客户端都在NAT后面都在、客户端、后面、两个

2023-09-05 03:47:55 作者:嘘!听屁在歌唱!

是否有可能建立一个直接 2的客户,都是不同的NAT后面之间的连接? 当然,我知道,在一定程度上与公共服务器IP地址将是至关重要的,但我不希望它是一个代理。看看下面的情况:

Is it possible to establish a direct connection between 2 clients that are both behind different NATs? Of course I realize that to some extent a server with a public IP will be essential, but I don't want it to be a proxy. Take a look at the following scenario:

我不希望有anythig做配置网络设备。我只是想做到这一点在code。 我不希望服务器采取文件传输过程的一部分(因为性能原因) 我们有:客户A,B,客户和服务器S和它看起来有点像这样的:

A - [路由器1] - S - [路由器2] - B

A--[Router1]--S--[Router2]--B A连接至S并授权 B连接至S并授权 A想要将文件发送到B 系统要求S代表到B的连接 S [它的魔力在这里]和A现在已经到B的连接 A开始发送文件 S端变为下降(或至少是文件传输绕过的) 还是有A和B之间的连接 A继续将文件发送到B

A connects to S and authorizes B connects to S and authorizes A wants to send a file to B A asks S for a connection to B S [does it's magic here] and A now has a connection to B A starts to send a file S goes down (or at least the file transfer bypasses is) there's still the connection between A and B A continues sending the file to B

我的问题:

这可能吗? 如何做到这一点? 遇到了示例项目,能够做到这一点?

我发现 WCF / WPF聊天应用程序 ,但它竟然是一个代理。

I found WCF / WPF Chat Application, but it turned out to be a proxy.

我也发现了一些帖子使用UPnP和NAT穿越暗示,但没有直接回答我的第一个问题,所以我没有深入挖掘进去。

I also found some posts suggesting using UPnP and NAT Traversal, but none answered my 1st question directly so I didn't dig deeply into it.

推荐答案

您正在寻找的神奇的一部分的术语叫做的 NAT打孔的。不幸的话题有点太宽泛地解释完全这里就如何完成它,但现在知道正确的说法应该至少能够让你开始寻找正确的教程。

The term you are looking for the "magic part" is called NAT Hole Punching. Unfortunatly the topic is a bit too broad to be explained fully here on how to accomplish it, but now knowing the correct term should at least be able to get you started on looking for the correct tutorials.

下面是algorithom从打洞页。

Here is the summary of the algorithom from the UDP Hole Punching page.

让A和B是两个主机,每个都在它自己的专用网络; N1和   N 2与全局可达IP地址P1和两个NAT设备   P2分别; S是一个公共服务器用一个公知的全球   访问的IP地址。

Let A and B be the two hosts, each in its own private network; N1 and N2 are the two NAT devices with globally reachable IP addresses P1 and P2 respectively; S is a public server with a well-known globally reachable IP address.   在A和B每开始一个UDP谈话S;在NAT设备N1和N2创建UDP转换状态并分配临时的外部   端口号X和Y   取值​​检查UDP数据包来获得使用N1和N2(外部NAT端口X和Y)的源端口   取值​​通过P1:X为B,而P2:Y为A    A发送一个数据包P2:Y和B发送一个数据包到P1:使用相同的源端口带有S这样冲的对话在锁孔x   对其他主机的NAT   如果任一主机接收到一个数据包时,打孔成功,两台主机之间的互通。    A and B each begin a UDP conversation with S; the NAT devices N1 and N2 create UDP translation states and assign temporary external port numbers X and Y S examines the UDP packets to get the source port used by N1 and N2 (the external NAT ports X and Y) S passes P1:X to B and P2:Y to A A sends a packet to P2:Y and B sends a packet to P1:X using the same source port as the conversation with S thus "punching" a hole in the NAT towards the other host If either host receives a packet, the hole punching is successful and both hosts can communicate.

如果两台主机都受限锥形NAT之后或的对称NAT的时,外部的NAT的端口将不同于那些用于与S在某些   路由器,外部端口依次拾取使得有可能   经过猜附近的港口建立一个对​​话。

If both hosts have Restricted cone NATs or Symmetric NATs, the external NAT ports will differ from those used with S. On some routers, the external ports are picked sequentially making it possible to establish a conversation through guessing nearby ports.

它是否将工作高度依赖于两个端点的NAT路由器的行为,这是非常可能的是你的使用一个显著部分将被配对并且两者都具有路由器不是打孔友好。

Whether or not it will work is highly dependent on how both endpoint's NAT routers behave, it is very likely that a significant portion of your uses will be paired up and both have routers that are not "hole punch friendly".

在你情我愿有我的软件尝试顺序执行这些步骤。

In your situation I would have my software try these steps in order.

检查,看看我们是否可以只连接(用户做手工端口转发) 使用UPnP和打开一个端口 在使用公共服务器之间的在旅途中使用某种形式的冲孔的 使用另外一个节点确实有端口开放作为数据代理(一个超节点)。 使用一台服务器主机我作为代理转发数据。 Check and see if we can just connect (The user did manual port forwarding) Use UPnP and open a port Use some form of hole punching using a public server as the go between Use another peer that does have ports open as a proxy for the data (a Supernode). Use a server I host as a proxy to forward the data.