views:

44

answers:

2

In TCP/IP, the port number is specified by a 16-bit field, yielding a total of 65536 port numbers. However, the lower range (don't really know how far it goes) is reserved for the system and cannot be utilized by the application. Assuming that 60,000 port numbers are available, it should be more than plenty for most nework application. However, MMORPG games often have tens of thousands of concurrently connected users at a time.

This got me wondering: Are there situations where a network application can run out of ports? How can this limitation be worked around?

+1  A: 

The canonical starter resource for this problem is Dan Kegels C10K page from 1999.

The lower range you refer to is probably the range below 1024 on most Unix like systems. This range is reserved for privileged applications. An application running as a normal user can not start listening to ports below 1024.

An upper range is often used by the OS for return ports and NAT when creating connections.

In short, because of how TCP works, ports can run out if a lot of connections are made and then closed. The limitation can be mitigated to some extent by using long-lived connections, one for each client.

In HTTP, this means using HTTP 1.1 and keep-alive.

Amigable Clark Kant
+2  A: 

You don't need one port per connection.

A connection is uniquely identified by a tuple of (host address, host port, remote address, remote port). It's likely your host IP address is the same for each connection, but you can still service 100,000 clients on a single machine with just one port. (In theory: you'll run into problems, unrelated to ports, before that.)

Roger Pate
Ah, kind of suspected this, but just wanted to make sure. Thanks!
gablin