Search code examples
c#.netudphole-punchingpeer

C# UDP punchthrough without rendevous server


I am writing a program that requires two clients (peer-to-peer) to connect to each other without the use of any server whatsoever, even just in the matchmaking process. Both clients already know each others' public and private IPs. One or both clients may be behind a NAT. I cannot rely on IPv6.

The problem I am running into is that one client (let's call them A) must send the packet to the other (B) that punches a hole in A's NAT and passes though B's NAT which has already been punched. However, this requires that A knows the external port on B's NAT that B has punched through to, as B's local port behind the NAT cannot be used. In addition, B cannot discern this port from behind its NAT. Visual representation of my problem Is there any way to discern this port, or even just let B tell the NAT what port to use?

Thanks in advance.

P.S. I am using the version of .NET Unity uses, approximately 2.0.

P.P.S. If you have a solution that works with TCP, I'll take that too!

P.P.P.S. Or pretty much any protocol other than Http!


Solution

  • You must have a well known public server to negotiate between the 2 endpoints.

    Here's a good implementation of this concept in c#: https://github.com/7wingfly/P2Pchat