UDP Hole Punching Java Example

后端 未结 3 1964
梦如初夏
梦如初夏 2020-12-14 10:53

I want to do UDP Hole Punching with two clients with the help of a server with a static IP. The server waits for the two clients on port 7070 and 7071. After that it sends t

相关标签:
3条回答
  • 2020-12-14 11:28

    For symmetric NAT (3G network connecting to a different mobile network), you need to do Multi-UDP hole punching.

    See:

    1. https://drive.google.com/file/d/0B1IimJ20gG0SY2NvaE4wRVVMbG8/view?usp=sharing

    2. http://tools.ietf.org/id/draft-takeda-symmetric-nat-traversal-00.txt

    3. https://www.goto.info.waseda.ac.jp/~wei/file/wei-apan-v10.pdf

      1. http://journals.sfu.ca/apan/index.php/apan/article/view/75/pdf_31

    Either that or relay all the data through a TURN server.

    0 讨论(0)
  • 2020-12-14 11:29

    You rightly use a rendezvous server to inform each node of the others IP / port based on the UDP connection. However using the public IP and port which is the combination which will is obtained by the connection as you have, means that in scenarios where both hosts exist on the same private network hairpin translation is required by the NAT which is sometimes not supported.

    To remedy this you can send the IP and port your node believes itself to have in the message to the server (private ip / port) and include this in the information each node receives on the other. Then attempt a connection on both the public combination (the one you are using) and the one I just mentioned and just use the first one which is successfully established.

    0 讨论(0)
  • 2020-12-14 11:43

    UDP hole punching can't be achieved with all types of NAT. There is no universal or reliable way defined for all types of NAT. It is even very difficult for symmetric NAT.

    Depending on the NAT behaviour, the port mapping could be different for different devices sending the UDP packets. Like, If A sends a UDP packet to B, it may get some port like 50000. But if A sends a UDP packet to C, then it may get a different mapping like 50002. So, in your case sending a packet to server may give a client some port but sending a packet to other client may give some other port.

    You shall read more about NAT behaviour here:

    http://tools.ietf.org/html/rfc4787

    https://tools.ietf.org/html/rfc5128

    UDP hole punching not going through on 3G

    0 讨论(0)
提交回复
热议问题