Search code examples
socketsnetworkingp2pnat

How can i send and get string through nat, simply as you can?


My given state is two computers on the network that know the extarnal ip and port of both computers and the inthernal ip and port of both. (NO SERVER INCLUDE)

How can I create a TCP socket in between them ?

Simply as you can

Thanks in advance


Solution

  • Since you say about external ip and internal ip, I think you mean that both of the computers are behind a router or other NAT devices.

    So, if you want to connect them by TCP, here are some methods.

    1. If you can use a server in WLAN as relay, it's quite simple. The computers connect to the server, the server change messages for them.

    2. If you don't want to use a server, then here is a problem: NAT devices may drop those packets which hasn't established a connection according to their type. Here are four types: full cone NAT, restricted NAT, port restricted NAT and symmetric NAT. And here are some methods for this circumstance

      2.1 Use NAT traversal algorithm, but they may not work well in symmetric NAT

      2.2 Use STUN/TURN/ICE to realise, it's quite reliable but need to learn how to use them.