• 13
• 18
• 19
• 27
• 10

# UDP hole punching

This topic is 2429 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm new here:)
I've created a simple app using VoIP client-server.
Now i see i need to use
http://en.wikipedia.org/wiki/UDP_hole_punching , because the server can't handle many clients.
I've no idea how I am supposed to implement that in my code.Anyone could help?

##### Share on other sites
What part are you having trouble with? The algorithm is pretty straightforward.

Do you not know how to send UDP packets? How to set up the central host? Which specific bit is problematic?

##### Share on other sites
Yes I know how to send UDP Packet.
According to the alghoritm

Let A and B be the two hosts, each in its own private network; N1 and N2 are the two NAT devices; S is a public server with a well-known globally reachable IP address.

1. A and B each begin a UDP conversation with S; the NAT devices N1 and N2 create UDP translation states and assign temporary external port numbers
2. S relays these port numbers back to A and B
3. A and B contact each others' NAT devices directly on the translated ports; the NAT devices use the previously created translation states and send the packets to A and B

[/quote]
Okay let's say i have a global server S . Now i run clients on A and B who send UDP packet saying 'hello i want to connect to another pc'. Server gets the IPs of A and B routers.

the NAT devices N1 and N2 create UDP translation states and assign temporary external port numbers
[/quote]
i don't understand this, how to do that.. ?

##### Share on other sites

the NAT devices N1 and N2 create UDP translation states and assign temporary external port numbers

i don't understand this, how to do that.. ?
[/quote]

That's what your router is doing -- you don't do anything.

Btw: the forum FAQ contains several links to this topic, and I wrote an article in Game Programming Gems 5 about this that you might want to find at the library.

##### Share on other sites
Firstly I'd like to thank for the helpful responses
Is it possible to test it inside 1 network? e.g. im 192.168.2.2,second pc is 192.168.2.3 and the main server is 192.168.2.4, the nat is 192.168.2.1
Guess not?

##### Share on other sites
You could set up a virtual machine. I believe one of the options when creating a VMware image is to treat the machine as if it were behind a NAT.

##### Share on other sites

Firstly I'd like to thank for the helpful responses
Is it possible to test it inside 1 network? e.g. im 192.168.2.2,second pc is 192.168.2.3 and the main server is 192.168.2.4, the nat is 192.168.2.1
Guess not?

You need a network that looks something like:

box 1 -> NAT -> Network -> Introducer box 2 -> NAT -> 

I have to admit, when I tested this, I used a server on the Internet, and my home and then-work computers as the two boxes behind NAT, so I did it "live."

However, with a proper VMWare "team" set-up, you may be able to do this with only two machines. Specifically, your host machine running the two behind-NAT virtual machines, and your regular desktop, would be the "box1," "box2" and "NAT" parts. It probably can't be the "introducer" part unless the VMWare NAT virtual network is very, very good (and my experience has been that it isn't) -- I'd plan on having another, physical, box for that.

Also, they need to be on different networks! box1 and box2 may be on 192.168.1.x nodes; the "introducer" needs to be on a different class of network -- 10.x, or 172.x, or with a proper IP address. (Theoretically, if you use net masks properly, you could also put the insides on 192.168.2.1, and put the introducer on 192.168.1.1)

Or, if you're going guerilla, you could just put up a second router -- they can be had for \$40 mail order these days. You would need two network interfaces on your host computer. Hook it up something like:
VM1 -> bridged interface 2 -> Router2 -> Introducer -> bridged interface 1 -> Router1 -> Internet VM2 -> bridged interface 2 -> Router2 -> 

"introducer" would run on the host machine, and only on interface 1, so it's logically on a different network.

Btw: if you don't have VMWare, then downloading Virtual PC is free for Windows, and Linux comes with kvm-qemu. You could also download VirtualBox for either platform. Their network configuraitons may not be quite as slick, but should be possible to bend to the functions you need, assuming you really understand how networks, interfaces, and addresses interact.