Search code examples
c++iosudpnatenet

NAT Hole Punching With ENet


I have been searching for a simple UDP C++ library that provides reliability for a while now. I have just come across ENet, which looks perfect, except that I can't find any support for NAT hole punching in the documentation. 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.

If it is possible, can someone give some pseudocode/steps describing the process?


Solution

  • 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