通过 NAT 连接 P2P? [英] Connecting P2P over NAT?

查看:31
本文介绍了通过 NAT 连接 P2P?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始探索使用 p2p 连接与其他人连接的选项,所以我在 JAVA 中为 android 设备编写了一个简单的套接字程序,用户可以在其中共享简单的消息 p2p(当时我对 NAT 一无所知).我了解了 NAT,所以我现在需要与另一个用户建立 TCP 连接,该用户使用服务器进行发现,但有效负载是 p2p 传输的.我也看过 XMPP(关于协议如何工作的一个非常好的和详细的解释是 此处) 和 UPnP,但我不知道如何实现它们.

I started to explore the option of connecting with other using a p2p connection, so I coded a simple socket program in JAVA for android devices in which the users can share simple messages p2p (I didn't have any idea about NAT then). I got to know about NAT, so I now need to establish a TCP connection with another user which uses a server for discovery but payload is transferred p2p. I have also looked at XMPP(a very good and detailed explanation of how protocol works is here) and UPnP but I dont know how to implement them.

另一个有趣的问题是 BitTorrent,因为它们可以在任何设备上运行,甚至可以在 NAT 之后运行.我无法解释 BitTorrent 的工作原理.

Another interesting question that arises is of BitTorrent because they can work on any device and even behind a NAT. I am not able to get any explanation of how BitTorrent works.

我已经研究了很多,但我被卡住了.

I have researched a lot but I am stuck.

我的问题是:

  1. BitTorrent 的详细解释(如此处,而不是种子文件的工作原理)以及它如何绕过 NAT 工作?
  2. 有没有办法以编程方式创建 NAT 条目?
  3. 套接字编程对于 p2p 是否足够?
  4. 创建自己的协议有多难,我该如何构建?
  5. 如果两个设备 D1 和 D2 想要进行 p2p 通信并且它们知道彼此的 IP.D1 向 D2 发送请求,该请求无法通过 D2 的 NAT,但应该在 D1 的 NAT 中创建了一个条目.因此,当 D2 尝试发送某些内容时,D1 的 NAT 应该会发现具有 D2 的 IP 的条目.那为什么数据包不允许呢?
  1. A detailed explanation of BitTorrent(like here, not how torrents work) and how is it able to work around NAT ?
  2. Is there a way to make a NAT entry programmatically ?
  3. Is socket programming sufficient for p2p ?
  4. How difficult is it to create your own protocol and how can I build one ?
  5. If two devices D1 and D2 want to communicate p2p and they know each other's IP. D1 sends a request to D2 and that can't get through the D2's NAT, but there should be an entry created in D1's NAT. So when D2 tries to send something D1's NAT should discover an entry with D2's IP. Then why is the packet not allowed by it ?

推荐答案

另一个有趣的问题是 BitTorrent,因为它们可以在任何设备上运行,甚至可以在 NAT 之后运行.我无法解释 BitTorrent 的工作原理.

Another interesting question that arises is of BitTorrent because they can work on any device and even behind a NAT. I am not able to get any explanation of how BitTorrent works.

这个声明看起来像是您假设 bittorrent 需要完全连接才能运行.

This statement looks like you assume that bittorrent needs full connectivity to operate.

这是错误的.

在 NAT 设备后面,您仍然可以建立传出 TCP 连接.只要有其他非 NATed(或 NATed 但正确 port-forwarded) 网络中可以接受传入连接的客户端.

Behind a NAT device you will still be able to establish outgoing TCP connections. Which generally is sufficient for bittorrent as long as there are other, non-NATed (or NATed but properly port-forwarded) clients in the network that can accept incoming connnections.

NAT 对数据的流向没有影响,因为连接一旦建立就是双向的.只有初始连接设置有问题.

NAT has no impact on the flow direction of the data because connections are bi-directional once they are established. It only is problematic for the initial connection setup.

这对于 bittorrent 非常有效,因为 bittorent 并不关心您从哪个特定节点获取数据.虽然更好的连接通常确实会提高性能.

This works perfectly fine for bittorrent because bittorent does not care from which specific node you get your data. Although better connectivity generally does improve performance.

如果节点的身份很重要或者一对一传输是一个重要的用例,那么其他 p2p 协议通常会尝试 NAT 遍历,如果失败,则依赖第 3 方节点在无法直接相互连接的节点之间中继流量.

If the identity of the node matters or one-on-one transfers are an important use-case then other p2p protocols usually attempt NAT traversal first and if that fails rely on 3rd party nodes relaying traffic between those nodes who cannot connect to each other directly.

此外,由于越来越多的 ISP 开始推出 运营商级 NAT 用于 IPv4,而 IPv6 将保持非 NAT

Additionally, IPv6 support will become essential in the future to maintain end-to-end connectivity because more and more ISPs are starting to roll out carrier-grade NAT for IPv4 while IPv6 will remain non-NATed

这篇关于通过 NAT 连接 P2P?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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