TCP NAT-Traversal /- Punching with .NET
Asked Answered
W

1

2

I write an application where it is essential to connect clients directly with each other and keep them connected. After doing some research using superior techniques (google) I found out that what I need can be achieved by doing NAT-Punching or NAT-Traversal.

Most people seem to use NAT-Punching via UDP, but in my case I need to do this with TCP (it is a fact, before someone is asking like "why not UDP?") and I couldn't find any sample code for this or at least a proper tutorial. All I could find where the theoretical appendages but nothing with Sockets or TCPClients (I prefer sockets.).

Can someone please link me some sample code with which I am able to connect two clients which are behind a NAT or link me to a full tutorial with sample-code? I know this goal is hard to do in .NET (I am using VB.NET but I'm able to read and understand C# as well), but it should be possible.

UPnP is not an option, because some NATs got this deactivated due to security-reasons.

Witmer answered 6/8, 2012 at 13:44 Comment(3)
There might be something you can do with raw sockets, but I wouldn't want to write it.Bat
so i guess i have to figure out some routing.... k thx!Witmer
Hi, I know this is a very old question, but I made another post answering this question in quite a bit of detail recently. And wanted to share it in case you're still looking for answers. #72167341Kizzee
S
1

NAT punching is not accomplishable via TCP unless you have access to Raw Sockets. You need to do some trickary and manipulation with sequence numbers and TTL values to make it work. However in Windows Raw Sockets is not allowed on non server versions of the operating systems on all versions of windows after XP SP2 due to the shear volume of malware that was abusing raw sockets to either propagate or perform a DDoS.

You do one of the following:

  1. Use UDP, put in code to re-request missing packets or refactor your code to handle lost packets if that is acceptable.
  2. Use a server as a middle man to stream through
  3. One of the ends must be capable of crafting raw sockets.
Spandau answered 6/8, 2012 at 15:20 Comment(1)
Hole punching technique doesn't require raw socket. I've done it successfully with winSock. Of course, you need a middle-man server which is connectable by both 2 peers.Miracle

© 2022 - 2024 — McMap. All rights reserved.