Networking – How does peer-to-peer work over the internet

bittorrentinternetnetworkingpeer-to-peer

From what I understand, there is no way to send a packet to a computer in a local network from outside the network, unless we know the routing mechanism employed by the router.

Assuming we have a set-up that looks like this:

  1. Computer-A, IP 192.168.1.2 (default gateway 192.168.1.1)

  2. Computer-B, IP 192.168.1.3 (default gateway 192.168.1.1)

  3. Router-C, IP 192.168.1.1 (external IP 1.1.1.1)

  4. Router-D (external IP 2.2.2.2)

Computer-A, Computer-B, and Router-C belongs to the same local network. Router-D wants to send data to Computer-A, but it can't do this without going through Router-C.

Now Router-C will forward packets to Computer-A if the destination port is 1000, and will forward packets to Computer-B if the destination port is 2000. But surely, the only device that knows this routing mechanism is Router-C itself! Not even Computer-A nor Computer-B will know about it, right?

So Router-D can send a packet to Computer-A if it sends a packet to Router-C through port 1000, but how is Router-D to know to send packets through port 1000, and not say port 1001?

How do peer-to-peer programs like Bittorrent get pass this problem? The only solution I can think of is for Router-D to send the packet to Router-C through all ports, such that it gets forwarded to Computer-A, but is there a better solution?

Best Answer

Your confusion stems from some incorrect assumptions.

But surely, the only device that knows this routing mechanism is Router-C itself! Not even Computer-A nor Computer-B will know about it, right?

What, why‽ Then why was the router configured to forward those ports to those IPs? You have to set up the P2P client to use a specific port and then set up the router to correspond.

but how is Router-D to know to send packets through port 1000, and not say port 1001?

Because you configure the P2P client to use a specific port (standard or non-standard for that protocol).

The only solution I can think of is for Router-D to send the packet to Router-C through all ports, such that it gets forwarded to Computer-A, but is there a better solution?

It is much simpler than that. When the client makes a connection to a peer, it specifies which port it wants to use, so the peer sends the data on that port.

Hmm, but Bittorrent doesn't change the router's behavior right? Since some routing mechanism could have been dynamic as demonstrated in superuser.com/a/187190/78897, how is Computer-A able to know about it?

The client doesn’t directly affect the router, but the router can be configured/intelligent enough to adapt to the client’s behavior. You can enable UPnP in both the router and client to automatically configure the connection and most routers have stateful inspection abilities as part of their port-forwarding mechanism.

Take together, what it means is that a connection can be dynamically made on a random port, and then the router can keep track of what is happening instead of viewing everything as random, meaningless connections. That way, it can forward a connection as necessary because for example, it is a response to this other connection that just happened.

The problem comes when you have multiple systems using the same program. Having multiple systems connected to the same router, sharing the same IP and using dynamic ports quickly becomes unmanageable and even with stateful inspection, it is difficult if not impossible to get it to work correctly. In that case, static ports (default or otherwise) will need to be used.


If you use a program like SmartSniff or TCPView to monitor your connections, you will notice that the P2P connections will usually have the port you configured (or the default for the client) as the destination for incoming connections and either the default or a custom/random port for the source, and vice versa for outgoing connections.

Related Question