Search code examples
socketsnetwork-programmingtcpp2pnat

How long do NAT routers wait before rejecting incoming packets from previously opened connections


How long do NAT routers typically allow incoming packets from previously opened TCP streams before rejecting packets? I'm working on a P2P application and I'd rather not keep an open connection to my central server and relay packets. Instead I was thinking of having the clients poll the central server, save those ip and port numbers and reach back out to each client from server to initiate P2P connection at some later time. If NAT allows packets in for a long time this is feasible. If this is not possible does anyone know how other P2P applications establish NAT Transveral/Connections?


Solution

  • That's unlikely to work. Typical home routers will track the state of TCP connections, and will not forward traffic on connections that have already closed.

    Even for active TCP connections, the amount of time that routers will retain connections that are open, but not sending any data, can vary wildly -- I've seen everything between a minute and a week. You will need to keep the connection open, and send periodic keep-alives, to reliably maintain a connection.

    Some routers also support the NAT Port Mapping Protocol to forward ports to a device behind the router. This is not universally supported, though. In particular, it is almost never supported in business routers.