How can I forward the port programmatically? To connect two computers directly (even with dynamic ip)? Tell me where to dig?

1 answer 1

The connection of two computers directly consists of two phases (subtasks), which are usually solved separately.

Phase 1 - discovery

To connect - you need to know where to connect. In other words, you need an ip-address. When a client connects to a server, a domain name is usually used — but with a peer-to-peer connection, as a rule, no domain names are assigned to computers. And even if appointed - they also need to somehow learn.

There are several ways to do this.

  1. Enter the IP address (or domain name) by the user directly. If in your case it is assumed that this option will be the main one, then you can not read further about detection.
  2. Network scan. Of course, the ip-addresses and ports should not be iterated: there is an easier way for cooperative discovery — broadcast packets. You can send a UDP packet to a broadcast address on one side and receive it on the other side to the known port on the other. In the old network games it was the main method of detection, it shows itself well within one segment of the local network.
    • A more modern option is to send a packet to a group address. Google by the word IGMP .
  3. Using a shared directory. You need to create a server that would be engaged in the dissemination of customer knowledge about each other. Examples: in online games from Blizzard, Battle.Net servers do this. In the bittorrent network this is done by torrent trackers.
  4. Using distributed data warehouses. Google by word dht

Phase 2 - Connection

And so, we have the ip-address of the other party, and maybe even an intermediary through whom you can contact and agree on the technical parameters of the connection.

I note in particular that it does not matter to us at all whether the address is static or dynamic. It is only important that he is assigned to the right computer - and we know him! It remains to establish a connection.

There are options again.

  1. If one of the computers "sees" the other - then he can connect to it directly. I note that it makes sense to try both options, since the possibility of establishing a connection is not necessarily symmetrical. For example, torrent clients can work even with ports completely closed for incoming connections - connecting with those for whom they are open. Or, another example, the FTP protocol - in it the data connection can be opened both in active (the server connects to the client) and in passive (the client connects to the server) modes.
  2. The user can be asked to open the port manually.
  3. UPnP allows, among other things, opening a port on a local router - if it supports it.
  4. If the computer is closed by NAT - but this NAT is not symmetrical, then you can use the "buggy" NAT. The fact is that a UDP packet sent "out" opens a random port for receiving information "inside". And in some types of NAT, this same incoming port is available not only for the address and port to which the first packet was sent. Thus, for the UDP protocol for some types of NAT, you can open the incoming port. The STUN protocol is responsible for this.
  5. Some providers or organizations still provide Internet access through a proxy server. HTTP (S) or SOCKS4 proxy are useless for us, but SOCKS5 proxy allows us to open a port for receiving connections.
  6. If everything is really bad, VPN or tunneling can help. Usually such questions are left to the user - but Skype, for example, is obliged by its popularity, including the ability to tunnel traffic through a so-called. super-nodes (a super-node can (could ?!) suddenly become any computer with allowed incoming connections, a thick channel and running Skype).
  • Loved the answer. but unfortunately I have already chosen another. hurried. =) Thanks, I learned a lot of interesting things. - Anonymous
  • 2
    @bigman tick, as it were, can be rearranged - Pavel Mayorov
  • 2
    @bigman and in front of me, then convenient? :) - Pavel Mayorov
  • 2
    @bigman tick you put not so much for the respondent, but for those who will read this question with the answers after. - Pavel Mayorov
  • one
    @PavelMayorov thanks, and sorry if it touched in the comments. - rdorn 7:38 pm