SOLVED UDP Hole Punching Help

B

BlockCabin

Guest
I want to create a peer to peer game like risk of rain. I'm pretty new to networking, but I really wanna learn how to do this. I don't know how to hole punch, it's that simple. I just do not. I think I get the general gist, but I have absolutely no idea how I would actually pull it off. From what I understand, it goes a little like this: The peers connect to a master server that takes their packets and sends them along. That doesn't really make sense to me though, since how would the master server be able to send packets through? I probably have it wrong. Even if that was correct though, I don't know how I'd actually be able to write that and make it work.

So basically, please help!
 

Tornado

Member
This is really a complex topic.
Check this link for the start:
https://forum.yoyogames.com/index.php?threads/gmnet-engine-beginners-help.44025/
And search for other topics about networking in the forum.
If you are new to networking you have to start to inhale and understand things slowly...one at the time...

We are planning to make realtime multiplayer version of our existing game via UDP hole punching. We have a professional server which already serves our single player game, so we will of course use it too for multiplayer and UDP hole punching because for hole punching you need a mediator between two peers (clients/players) helping them establish peer to peer connection. After they have a connection established, they can communicate then directly with each other.

Under the link I sent you above, there is some option for free server I think...just check it out.

When we start with multiplayer we will too first have to do a lot of learning and researching...
 
Last edited:

ThraxxMedia

Member
I want to create a peer to peer game like risk of rain.
Funny you mention Risk of Rain, since it had the very same issue of not providing any kind of automatic port forwarding, so you had to do it manually. This, of course, only applies to the person creating a server; so in a game where one guy is the host and 10 people connect as clients, only the host would have to know how to configure their router. You may just go this exact route for your own game, but then again, it's an additional obstacle that may or may not be irritating to players. Not everyone knows how to properly configure a piece of hardware. Some are happy enough they know how to turn on the computer.

From what I understand, it goes a little like this: The peers connect to a master server that takes their packets and sends them along.
You're correct. The "master server" would be a publicly accessible PC with a static, known IP address that can be hard coded into your game client. Once a client establishes a connection to said server, the server may (temporarily) store their information so it knows who's connected at all times. This information can then be passed on to every other connecting client to negotiate direct connections between them. That's really just the very basic gist of it, as you said, so if you don't already know how to set up such a middle-man kind of server (let alone making it publicly available 24/7), I reckon this is not really a viable option for you.

The only way to establish a true peer-to-peer environment, without the need of any third party or service in between, would be port forwarding - basically meaning that whoever opens a session as host will have their ports configured in such a way that they are reachable by everyone else, so they can act as the authoritative server. Again, if you don't want to tell your customers to do this by hand (what if they don't know how?) then the only solution left are probably extensions doing the dirty work for you; after all, if you knew how to do this yourself, you wouldn't be asking for help. :)

Important thing to note: extensions are not a 100% surefire guarantee it'll work, simply because you cannot trust that every user's router is supporting or properly configured for UPnP and/or NAT-PMP, but it's better than nothing and definitely eliminates at least some of the hassle. Also, depending on the type of extension, it may limit your project to certain platforms (for example if it's a DLL, you can only use it for Windows applications).

Currently, there's only one extension on the marketplace (that I know of) that's offering automatic port forwarding; it's called "DLCEXT - Jack Of All Trades" and was written by me. It also comes with a set of other features that you may or may not want/need in your game, but that's besides the point. I created it because not long ago I was in the very same spot you're in right now: I wanted to make an online multiplayer game, but GMS2 doesn't come with native functions for NAT traversal, and quite literally every single post I found on the internet either offered no solution at all, said you would need to rely on third party stuff, or the conversation derailed into other topics over time, still not being of any help.

So, this is me giving back to anyone looking for an answer to this question. :)

Edit: if you're nevertheless adamant on going for the hole punching route, I recommend reading some articles that go into more detail, like this one: https://bford.info/pub/net/p2pnat/

Hopefully, it'll provide you with enough information to consider whether this is the best approach for your project.
 
Last edited:
B

BlockCabin

Guest
Funny you mention Risk of Rain, since it had the very same issue of not providing any kind of automatic port forwarding, so you had to do it manually.
I actually meant ror2, sorry.

For the rest of the post though, thank you! I actually saw DLCEXT on the marketplace, but I didn't really know if I needed it. I currently don't really have money to spend on it, but I will get it when I can! For now, I'll just make it so that you do have to port forward. Also, I might consider that master server thing if I had money, but I do not. Also, may I ask, what does the automatic port forwarding really do? Does it open a port permanently that you could for example edit if you went into your router settings, or does it do something completely else that I would never be able to wrap my brain around? Not really an important question, just a curiosity one.

Seriously, thank you for your post, that was insanely helpful, you would not imagine.
 

ThraxxMedia

Member
Also, may I ask, what does the automatic port forwarding really do? Does it open a port permanently that you could for example edit if you went into your router settings, or does it do something completely else that I would never be able to wrap my brain around? Not really an important question, just a curiosity one.
What it does is basically the same thing you would do by hand if you were to go into your router‘s configuration and add things manually. So, absolutely no black magic there. :p

The way this works is by attempting to communicate with the router over either Universal Plug & Play (UPnP; the more common one), or via NAT-PMP which is an alternative protocol that some devices use. Port mappings set via UPnP are permanent until you remove them (which you could do whenever you close the game, for example), while NAT-PMP has a lease time of one hour by default. After that, the mapping would need to be refreshed.

Just to be clear, I‘d like to point this out again: there is no such thing as a 100% guarantee that it‘ll work for everyone, because of two requirements: the user‘s router has to a) support either one of the above mentioned protocols and b) be enabled for it. Some devices may have it turned off by default, which would then require manual configuration anyways.

As long as that‘s given though, no user input is required at all. :)
 

SirCaliber

Member
Risk of Rain 2 (one of my favorite games btw) Uses steam's servers, so it actually doesn't even holepunch at all (as far as i know). What it does is connects the client to the steam servers, then clients can join others' servers because they are connected to steam. Risk of Rain 1, on the other hand (which was made in GameMaker) requires you to either port forward OR use Hamachi. Hamachi is a program that makes your connection and other connections seem like they're on LAN to programs, but you still need to connect to their Hamachi IPv4, but it eliminates the need to port forward. Hamachi is very handy and I use it daily
 

csaki01

Member
What it does is basically the same thing you would do by hand if you were to go into your router‘s configuration and add things manually. So, absolutely no black magic there. :p

The way this works is by attempting to communicate with the router over either Universal Plug & Play (UPnP; the more common one), or via NAT-PMP which is an alternative protocol that some devices use. Port mappings set via UPnP are permanent until you remove them (which you could do whenever you close the game, for example), while NAT-PMP has a lease time of one hour by default. After that, the mapping would need to be refreshed.

Just to be clear, I‘d like to point this out again: there is no such thing as a 100% guarantee that it‘ll work for everyone, because of two requirements: the user‘s router has to a) support either one of the above mentioned protocols and b) be enabled for it. Some devices may have it turned off by default, which would then require manual configuration anyways.

As long as that‘s given though, no user input is required at all. :)
At the very least your extension does some other helpful things, which is nice, I'll definitely keep it in mind in case I decide to go down this route.
 
Top