Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit/AppGameKit Studio Showcase / NAT Punchthrough - AGKClassic/Studio-Tier1

Years of Service
User Offline
Joined: 9th Oct 2002
Posted: 11th Feb 2023 22:37
As I had need of this capability in my own projects, I spent a long time exploring ways to implement an effective punchthrough system for AGK/S and managed to come up with the attached system. After successful tests and using it in a variety of connection configurations, I wanted to post a simplified example in case others might find it useful. Here are some notes on the concept and how it works (instructions are included with the client code example):

// This NAT-Punchthrough system works on the principle of a 'STUN' (Session Traversal of User Datagram Protocol [UDP] Through Network Address Translators [NAT]) server.
// An exchange server (the 'STUN') is set up on a system that has a required port open and forwarded so that other systems outside of the network can connect to it to exchange IP information.
// If the port is available on the exchange server, players can connect to it to report they either want to host a session or join an existing session.
// The exchange server then retrieves the IP information of the player and logs it for later broadcast to other players. For the host, it logs the IP and stores the details
// so that they can be provided to other players (clients) who want to join. For clients, the exchange server simply informs the host when a player wants to join and
// then exchanges IP addresses between the two players (host and joining client) so that they can then attempt to connect to each other directly at the same time.
// If the two players broadcast to each other on preset ports at the same time, the router will allow return traffic from the same system outbound traffic is sent to.
// As a result, the two players can then communicate directly with each other without having to manually open/forward ports in their routers.
// This approach also depends on routers retaining port paths along the way, so it won't work with symmetric NAT's or other uncommon NAT configurations.
// For most users with typical routers, this approach will generally work very well and is a common technique used in games and chat software.
// This is only for internet connections through routers. It is not needed for LAN multiplayer connections since those ports are open.
// This system only completes the process up to connection. Any further data exchange and player management is up to the developer.


Login to view attachments
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 13th Feb 2023 16:42
Oh this is great, I wanted to do this for years now.
We just need a non AppGameKit language version for the server side as nobody should run Interpreted AppGameKit code on a dedicated server ^^
Couldn't test as of yet but i will.
Thank you very much for sharing.
Years of Service
User Offline
Joined: 9th Oct 2002
Posted: 13th Feb 2023 19:26 Edited at: 13th Feb 2023 20:30
I'd imagine a variety of webapp/compiled language formats could be used, so long as they follow the same UDP format AppGameKit uses so they can communicate with each other. The sample AppGameKit code could be used to write something in a different language.

However, I have run the example server app on a dedicated AWS server and it keeps the CPU utilization low enough to not drop credit on the lowest t2.micro instance type. Generally hovers at 0% most of the time and still runs fast enough to keep up with requests and exchanges at around 10 cycles per second. But it can be further adjusted via the 'cpulevel' variable (line 43) as may be needed.
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 20th Feb 2023 07:38
Nice work can't wait to test it out

Login to post a reply

Server time is: 2023-10-04 00:39:57
Your offset time is: 2023-10-04 00:39:57