简体   繁体   English

使用ENet进行NAT打孔

[英]NAT Hole Punching With ENet

I have been searching for a simple UDP C++ library that provides reliability for a while now. 我一直在寻找一个提供了可靠性的简单UDP C ++库。 I have just come across ENet, which looks perfect, except that I can't find any support for NAT hole punching in the documentation. 我刚刚看过ENet,它看起来很完美,但是我在文档中找不到对NAT打孔的任何支持。 The internet seems to have a few people discussing this, but I have yet to find a definite answer to whether it is possible. 互联网上似乎有一些人在讨论这个问题,但是我还没有找到是否可行的明确答案。

I already have a third party server that is configured to hand out external ips & ports to clients. 我已经有一个配置为向客户端分发外部ips和端口的第三方服务器。

If it is possible, can someone give some pseudocode/steps describing the process? 如果可能,有人可以提供一些伪代码/步骤来描述该过程吗?

I figured this out. 我想通了。 Here's what I did: 这是我所做的:

  1. Create Host for current client 为当前客户端创建主机
  2. Connect to 3rd party server 连接到第三方服务器
  3. When another client connects to the 3rd party server, it sends out the other clients info to each client 当另一个客户端连接到第三方服务器时,它将其他客户端的信息发送给每个客户端
  4. Upon receiving the peers address, each peer attempts a connection to the other peer on the same socket that was used to connect to the server, and begins sending packets to that address. 接收到对等方地址后,每个对等方都尝试在用于连接服务器的同一套接字上与另一对等方建立连接,然后开始向该地址发送数据包。 This was initially my problem. 这最初是我的问题。 I had been sending only a couple of packets, and they could easily be lost. 我只发送了两个数据包,它们很容易丢失。 The connection would only work half of the time. 该连接将只在一半时间内工作。 I ended up spawning a thread that sent packets until either the connection succeeded or failed. 我最终产生了一个发送数据包的线程,直到连接成功或失败。
  5. Connection should be established after a few packets being sent 发送一些数据包后应建立连接

Use STUN for Hole Punching and then use ENet . 使用STUN进行打孔,然后使用ENet
Communicate with STUN-Server and retrieve public endpoint and NAT type . 与STUN-Server通信并检索公共端点NAT类型 Make sure the NAT type is open and then continue => use getsockname on socket that used for communicate with STUN-Server, to get local endpoint and then close that socket. 确保NAT类型已打开,然后继续=>在用于与STUN-Server通信的套接字上使用getsockname ,以获取本地端点 ,然后关闭该套接字。
Now create ENet server and initialize it with local endpoint . 现在创建ENet服务器并使用本地端点对其进行初始化。 Now your ENet clients from other computers can connect to server using public endpoint . 现在,来自其他计算机的ENet客户端可以使用公共端点连接到服务器。

UDP may drop packets and it may send packets out of order. UDP可能会丢弃数据包,并且可能会乱序发送数据包。 Ordering the packets is trivial. 订购数据包很简单。 But if a packet has been dropped, there can be no way of recovering it. 但是,如果数据包被丢弃,将无法恢复。 However, many systems that do UDP will provide you with another stream with duplicate data. 但是,许多执行UDP的系统将为您提供另一个具有重复数据的流。 In which case you can simply create a back up with the secondary stream and then update check if the packet dropped in the first stream is available in the second. 在这种情况下,您可以简单地使用辅助流创建备份,然后更新检查在第一个流中丢弃的数据包在第二个流中是否可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM