I want to establish a peer to peer connection (in C#) without the need for network configurations from any of the clients (port forwarding and such).
I saw many people say NAT punch is the solution with a middle server which works good for my needs but left a question open, why is port forward required on TCP but not on UDP?
And is port forwarding obsolete when RUDP (reliable udp) exists as it holds TCP's advantages but without the need for port forward?
I rarely see anybody talks about this advantage of UDP over TCP. (It might be a disadvantage, but works as an advantage for my needs).
Would love an explanation because I couldn't find anything online
CodePudding user response:
TCP establish a connection between the two hosts, UDP on the other hand is fire and forget.
A NAT will rewrite the IP-address of outgoing packets (and possibly ports), and keep a table of all the TCP connections, so an incoming packet on a specific port can be translated to the correct IP/Packet. This is made easier by the TCP connections, since the NAT knows when the connection is established and disconnected.
So explicit port forwarding would only be needed for incoming connections.
NAT will work more or less the same when using UDP, but since it does have any concept of connections, the NAT will probably use a timeout mechanism, so the hosts need to send packets every now and then to reset the timeout and keep the connection open. UDP hole punch work due to the lack of connection, i.e. once the NAT-mapping has been created the host can receive UDP packets from any host, not just the one it send the original packet to.
UDP hole punching may however fail with some types of NAT, so claiming that port forwarding is obsolete is probably an exaggeration. It will depend on the specific purpose.