Communication between two computers without opening ports, using a third computer to set up the connection

后端 未结 5 1885
南笙
南笙 2021-02-03 16:19

Let\'s say I have a server, and two clients connected to it. (via TCP, but it doesn\'t matter)

My goal is to allow a direct connection between those two clients. This is

相关标签:
5条回答
  • 2021-02-03 16:39

    Ephemeral ports won't magically eliminate the need to relay through the server, because they are only valid during the life of the session opened through a well known service port. Basically ephemeral ports depend on a server session.

    You will need to use the server to relay communications between both clients, that is act as a proxy server. One option would be to setup a SSH tunnel through a SSH proxy server, with the added benefit of security.

    Still this doesn't guarantee that the firewall won't block the connection. That depends on the firewall type and configuration. Most residential routers that act as firewalls, by default block all incoming connections. This is normally fine because most of the time the computers behind the firewall act only as clients, which initiate the connections to the outside. And this setup varies, because some restrict initiating connections only to well known service ports like HTTP, HTTPS, FTP, SFTP, SSH, etc., and if your proxy server uses a non-well-known-service port then the connection will be blocked.

    But firewalls can be setup to block outgoing traffic also, this is most common in corporate networks, which don't even allow direct connections to web servers and route everything through proxy servers, in order to control resource usage.

    You can also research on the use of UPnP to open ports dynamically.

    0 讨论(0)
  • 2021-02-03 16:40

    Punching TCP holes in NAT is sometimes/often possible (it depends of the NAT behavior). This is not a simple subject to learn, but read the corresponding chapter about NAT traversal from Practical JXTA II (available online on Scribd) to understand the nature of the issues to solve.

    Then, read this. It comes from the guy who wrote that: http://nutss.gforge.cis.cornell.edu/stunt.php (one of the links in your question).

    I am not a C/C++ specialist, but the issues to solve are not language specific. As long as you have access to TCP from your code base, that's enough. Keep in mind that implementing UDP traversal is easier than TCP.

    Hope these tips help.

    P.S.: I am not aware of a C/C++ implementation of the solution. The code mentioned in Cornell's link is NOT operational as confirmed by the author. I tried to resuscitate it myself, but he let me know it was completely tweaked for research purposes and far from production ready.

    0 讨论(0)
  • 2021-02-03 16:56

    A few links to projects that might be of interest or helpful:

    • http://sourceforge.net/projects/stun/
    • http://udt.sourceforge.net/
    • http://www.telehash.org/
    0 讨论(0)
  • 2021-02-03 17:00

    I'm not aware of any way to reliably punch through firewalls for TCP, but there's a similar method for UDP traffic that's pretty well documented:

    • http://en.wikipedia.org/wiki/STUN

    • http://en.wikipedia.org/wiki/UDP_hole_punching

    0 讨论(0)
  • 2021-02-03 17:00

    You're looking for rendezvous server for NAT hole punching: the server that is publicly accessible (not behind NAT/firewall or they are properly configured) to help computers behind NAT/firewall to establish peer-to-peer connection.

    UDP is more popular in NAT punching because provides much better results than TCP. Clear and informative description of UDP NAT hole punching can be found here.

    If you need reliable communication, you can use reliable protocols over UDP:

    1. SCTP (libraries) - standardized one, or
    2. one of many custom protocols, e.g. RakNet (I used this library, it's quite mature and feature-rich and has NAT punching implementation), Enet or many others (Q8)
    0 讨论(0)
提交回复
热议问题