Search code examples
network-programmingtcpconnectionlan

How do IM applications manage to let users transfer files between differrent LANs


I am working on a program related to network, and there's a situation that the client has to connect to a server which is inside a LAN.

As I know, when establishing a TCP connection, the port the server is listening on has to be accessable to the client. If the server is inside a LAN, port accesses are blocked by the router. One solution I know is to use UPnP to perform a port mapping on the router. However, in some cases, the router does not support UPnP, are there other solutions?

IM applications came up into my mind. Many IM applications have the functionality that users can send files to each other, whatever the network environment is, as long as you can access to the internet. I don't think a public server is used as a file data exchanger between the two, the connection has to be a direct one. How do they actually do to enable the client to connect to a "hidden" server?


Solution

  • Typically such programs try a series of steps:

    1. A connect directly to B
    2. B connect directly to A
    3. A tries to connect to a firewall (uPnP) forwarded port to B
    4. B tries to connect to a firewall (uPnP) forwarded port to A
    5. A and B both connect to a central server and exchange data through that

    The last step is obviously the least preferred because the provider has to have sufficient resources to manage all simultaneous transfers. Rate-limiting is common.

    Since IM has central management anyway, it's not too difficult to coordinate all this.