Hey I'am working on a project to create a p2p 开发者_StackOverflownetwork in java. I have built the n/w using UDP for the local lan and I am working on sending the UDP datagram across the internet. I came across this concept called UDP hole punching but cannot seem to find any relevant topic or libraries. Can anyone please help?
I think this is what is often called NAT punch-through. Maybe you'll have more luck searching with that title.
Here is some highly recommended technical material for UDP (or TCP) hole punching:
http://www.usenix.org/events/usenix05/tech/general/full_papers/ford/ford_html/
Or, of course, wikipedia:
http://en.wikipedia.org/wiki/UDP_hole_punching
Essentially, since most computers are behind what are called Network Address Translators (NAT), you will need to use the computers public IP and public port as someone on the internet sees them. Also, you will need the private IP and private port (or as the application itself sees its sockets... this is just in case you are behind the same NAT as the other application/computer/node you are trying to connect to). Then BOTH the two nodes try and establish a connection by trying the public and private addresses. When these two applications establish contact, they have successfully traversed the NAT--they have successfully hole-punched.
A popular way of doing this is to have both clients connect to a server which helps coordinate the two clients. It coordinates the sending of the public and private IP's and ports to the other client, which they then use to perform the "hole punch."
Also, apparently UDP protocol is used because it is more (empirically) successful than TCP. Note: I tried to find the statistics on that but was unsuccessful.
You cannot do UDP broadcast/multi-cast over the inetrnet. You can however pass TCP over the internet.
What I suggest you try is to have a gateway on your network listen to UDP packets of your choice, pass the data over TCP to another gateway on the other network which can turn this information back into UDP.
You're probably looking for uPnP's NAT traversal facility. Many consumer routers support programmatically opening up the entire firewall and forwarding ports for any kind application that asks. Or worm. Either way. :)
Check out this other question on NAT-to-NAT traversal. Erik has a nice description of UDP hole punching.
精彩评论