建立P2P TCP/UDP连接(NAT穿越)

huangapple go评论109阅读模式
英文:

Establishing P2P TCP/UDP connections (NAT traversal)

问题

有没有办法在没有中介服务器的情况下通过TCP或UDP建立真正的P2P通信?我知道如果两台计算机只有公共IP地址,这是完全可行的,但如果它们在任何类型的NAT路由器后面或者在私有网络上,情况就开始变得棘手了...事实上,我越深入研究,似乎越明显根本不可能。我发现了STUN、TURN和ICE协议以及打洞技术,但据我了解,它们都需要某种服务器来至少建立连接。我很惊讶,一个看起来如此简单的事情竟然如此难以实现,我是不是漏掉了什么?
理想情况下,我正在寻找使用Golang的解决方案,任何C++的帮助或示例也会很有用,谢谢。

英文:

Is there any way to establish true P2P communication through TCP or UDP without an intermediary server? I understand that this is quite doable if the two computers have only public IP addresses but if they are behind any sort of NAT router or on a private network it starts to get tricky.. in fact the more I look into it the more it seems plain not possible. I have discovered STUN, TURN and ICE protocols and hole-punching but as far as I understand they all require some sort of server to at least establish the connection. I am amazed something seemingly so simple seems so hard to do, am I missing something?
Ideally I am looking for solutions using Golang any help or examples in C++ would also be useful, thanks.

答案1

得分: 2

这不是一个严格的Go语言问题,而是一个关于网络的一般性问题。

之所以这个问题如此困难,是因为互联网从未设计用于支持NAT,NAT只是偶然发生并破坏了很多功能。解决您的问题的清洁方法是切换到IPv6,其中没有NAT,但这可能不太可能。

如果您的NAT设备支持NAT-PMP或uPNP,您可以使用它。最简单的方法是创建一个与libminiupnpc绑定的cgo,或者您可以寻找一个原生的Go库。

如果您的NAT不支持这些协议,那么您可能没有办法了,因为打洞是一种脆弱且难以实现的技术,您可能会发现很难实现。

英文:

This is not strictly a Go question — it's a general networking question.

The reason this is so difficult is that the Internet was never designed to support NAT, NAT just happened and broke a lot of functionality. The clean solution to your problem is to switch to IPv6, where there is no NAT, but that might not be possible

If your NAT box supports NAT-PMP or uPNP, you could use that. The simplest way would be to create a cgo binding to libminiupnpc, or you could go fishing for a native Go library.

If your NAT doesn't support either of these protocols, then you're probably out of luck, as hole punching is a fragile and difficult technique that you will probably find difficult to implement.

huangapple
  • 本文由 发表于 2016年11月11日 21:56:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/40549502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定