[英]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: 这是我所做的:
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.