Page 1 of 2
Peer-to-Peer Networking Question
Posted: Sat Feb 25, 2017 1:16 am
by thebigredforest
I want to add peer to peer multiplayer to a game i'm working on, but I do not want to have people port forward their router, just to play my game. I am a complete noob when it comes to networking.
How should I accomplish this?
Thanks everyone!
Edit:
To further explain what I am looking for, the client will start server on their computer and then others can connect to it. Kinda like Call od Duty where there is one host, and many other clients.
Re: Networking question
Posted: Sat Feb 25, 2017 1:27 am
by zorg
Use hamachi and enjoy the added lag.
Re: Networking question
Posted: Sat Feb 25, 2017 1:41 am
by thebigredforest
Nice. I hate hamachi.
Re: Peer-to-Peer Networking Question
Posted: Sat Feb 25, 2017 1:57 am
by raidho36
There is a technique called "NAT punchthrough" but it requires an accessible server on the internet.
Re: Peer-to-Peer Networking Question
Posted: Sat Feb 25, 2017 3:22 am
by airstruck
You should be able to do holepunching without an intermediary server if you have players exchange their ip addresses outside of the game. I thought someone around here had a holepunching library?
Re: Peer-to-Peer Networking Question
Posted: Sat Feb 25, 2017 8:31 am
by zorg
I only found
one relevant thread here on the forums.
Re: Peer-to-Peer Networking Question
Posted: Sat Feb 25, 2017 8:44 am
by Positive07
If someone gets this right that person should MUST make a library out of it!!
Re: Peer-to-Peer Networking Question
Posted: Sat Feb 25, 2017 11:03 am
by bartbes
airstruck wrote: ↑Sat Feb 25, 2017 3:22 am
You should be able to do holepunching without an intermediary server if you have players exchange their ip addresses outside of the game.
Which kind of requires that server. For most NAT types the following (udp!) method works:
- Magically obtain a server list (usually by having a master server where the game can say "Hi, I exist at <port>!")
- Send a message to some master server saying "I want to connect to <targetip>:<targetport> from <myport>"
- The master server forwards this via an established connection to the server, saying "Hey, <clientip>:<clientport> wants to connect"
- The client just starts sending traffic to the server, and vice versa
- At some point the intermediary NATs see bidirectional traffic, so "connect" the two ends, and they'll see each other's traffic.
- Now you've done the holepunching, connect as normal.
Note that a peer behind a NAT can't determine their own ip
nor their port. Most NATs will map the same internal source port to the same external source port for different destinations, though, so port 1234 will map to, for example, 2345 when sending traffic to 8.8.6.6, and to also to 2345 when sending traffic to 8.8.8.8 (these are examples, of course). This is why you want to register to the master server using the same socket that will accept the connections (which is perfectly possible with udp). You can use an additional socket for further notifications, if you want to, but again, using the same socket is pretty much the only way to figure out the ip and port. The same goes for the client, of course, it needs to use the same socket for both the master server and the other peer.
For NATs that do not re-use port mappings.. there's no practical way to holepunch. Also note that you might want to add rate limiting on your master server, or something of the sort, so people can't just send a bunch of connection requests via your server.
Lastly, if you're a fan of XML and complicated standards, you might be able to figure out UPnP and establish temporary port forwarding that way. I'm not sure how many routers you'll find that actually accept UPnP though, it may be (way) more effort than it's worth.
Re: Peer-to-Peer Networking Question
Posted: Sat Feb 25, 2017 5:10 pm
by airstruck
I'm sure I've seen it done without a master server, but I can't remember where. I may have been thinking of this:
http://samy.pl/pwnat/
Of course if two players actually
exchanged IPs (host has client's IP as well), you shouldn't need a master server either, and wouldn't need fancy tricks like pwnat. By "outside the game" I just meant by text message, phone call, shouting across the street, etc. Unclear whether that suits the OP's use case (looks like it from the title and first paragraph, but not the part after the "edit").
Re: Peer-to-Peer Networking Question
Posted: Sat Feb 25, 2017 5:37 pm
by bartbes
airstruck wrote: ↑Sat Feb 25, 2017 5:10 pm
I'm sure I've seen it done without a master server, but I can't remember where. I may have been thinking of this:
http://samy.pl/pwnat/
Though it does presume the client knows the server's public ip (and port?), which seems a bit weird.
airstruck wrote: ↑Sat Feb 25, 2017 5:10 pm
Of course if two players actually
exchanged IPs (host has client's IP as well), you shouldn't need a master server either, and wouldn't need fancy tricks like pwnat. By "outside the game" I just meant by text message, phone call, shouting across the street, etc. Unclear whether that suits the OP's use case (looks like it from the title and first paragraph, but not the part after the "edit").
Right, as long as both sides know each other's details (accurately!) the hole punching works.
In practice, though, you'll want some server so you can have a server list anyway, so using that same server to help with holepunching isn't a problem.