Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UDP Hole Punching (Java)

I've been trying to create a socket chat/file transfer application for several weeks now. I've spent hours searching on the internet for a piece of code on UDP hole punching to work with, but I haven't found anything that works or is simple enough for me to use.

I'm trying to get two clients behind routers (NATs) to connect to each other. I've managed to set up a server that shares the two clients IPs and ports, but I haven't managed to actually initiate a connection. Is there any source code anywhere that I can use?

I've found several STUN/ICE/TURN libraries like JStun, but there's no documentation on how to use them. Do I need a STUN server to start the connection? Can I use a public one like numb.viagenie.ca or http://www.stunserver.org/ ?

Any answers would be greatly appreciated. Thanks!

EDIT: I've had a bit more poking around, and I've discovered that STUN is fairly easy to make even through PHP. I've set up my own PHP stun that just shares the two clients data. The hard part is connecting the two clients, but that uses something called ICE. Thanks to selbie who posted this in the comments. That got me on the right track with ICE. I'll leave this question open for a bit longer in case somebody comes up with anything else. I'll start coding my own ICE code, or try and look for something online. Thanks!

like image 214
Marcus Cemes Avatar asked Aug 23 '14 18:08

Marcus Cemes


People also ask

How does UDP hole punching work?

UDP hole punching is one of the most common techniques used to establish UDP connections with systems behind NAT. It is called UDP hole punching because it punches a hole in the firewall of the network which allows a packet from an outside system to successfully reach the desired client on a network using NAT.

What is TCP UDP hole punching?

TCP hole punching is an experimentally used NAT traversal technique for establishing a TCP connection between two peers on the Internet behind NAT devices. NAT traversal is a general term for techniques that establish and maintain TCP/IP network and/or TCP connections traversing NAT gateways.

Is UDP hole punching reliable?

UDP hole punching can be used in a 'known' environment. In an ad-hoc scenario it's a matter of luck and far from reliable. With older routers it might mostly work but no so much with newer ones with stricter security. Chances are, things get worse for you over time.

What is port punching?

To punch a hole, each client connects to an unrestricted third-party server that temporarily stores external and internal address and port information for each client.


1 Answers

An example of UDP Hole Punching in pure Java can be found here: https://github.com/lklacar/java-hole-punching

Not longer maintained but here is a framework for STUN process etc.: https://github.com/htwg/UCE

I ve tried to get UDP Hole Punching working with Java Clients and PHP Server as well but could not get it working yet. If you have, u might share it with us and answer the following question: Java-Client PHP-Server UDP Hole Punching example code

like image 64
Hatzen Avatar answered Sep 20 '22 05:09

Hatzen