A server is a machine or program that provides services. For example, a web server provides the service of “serving webpages”.
A client is a machine or program that uses services. For example, a web browser is a client that uses the services of a web server.
A server provides a service by listening on a port for incoming connections. For example, a web server generally listens on port 80.
A port is a number, generally in the range 1 to 65535.
Caveats: Two programs cannot listen on the same port at the same time.
A client that wants to use a service needs to connect to the listening port for the service on the server. For example, web browsers are configured to connect to port 80 of the website that you want to view.
A server, upon receiving a connection from a client, will accept the connection.
Now, instead of the server’s listening port being monopolized by that client for the duration of the connection, the server actually allocates a new port for the server to use for the duration of that connection.
Such a port is called an ephemeral port (i.e. a temporary port).
From that point onward, the communication between the client and server takes place using the new server port.
That way, the original listening port is immediately available to receive new connections from other clients, thus allowing a server to provide the same service to multiple clients at the same time.
(Note that the client’s port doesn’t change. Only the server’s port changes.)
Caveats: Ephemeral port numbers fall within the same range as normal port numbers. Thus, when setting up a new service on a server, it’s generally wise not to use an ephemeral port number as the listening port for the service, otherwise if the server provides multiple services on different ports, there would be a problem if one listening port accepts a connection, which creates a connection on an ephemeral port which happens to be the same port as one that another service wants to (but has not yet started to) listen on.
Can Multiple Programs Listen on the Same Port Number at the Same Time?
No. Only one program/process/thread can listen on a port at any point in time. Any attempt to listen on a port that is already being listened to will fail with an error.
What is the Ephemeral Port Range of My Machine?
The Internet Assigned Numbers Authority (IANA) suggests using the port range 49152-65535.
Many Linux kernels use the port range 32768-61000. The actual configured range for a machine can be seen in the pseudo file /proc/sys/net/ipv4/ip_local_port_range.
FreeBSD has used the IANA port range since release 4.6. Previous FreeBSD versions, including the Berkeley Software Distribution (BSD), use ports 1024-5000.
Microsoft Windows operating systems through to XP use the port range 1025-5000 by default.
Windows Vista, Windows 7, and Server 2008 use the IANA port range by default.
Windows Server 2003 uses the port range 1025-5000 by default, until Microsoft security update MS08-037 from 2008 is installed, after which it uses the IANA port range by default.
Windows Server 2008 with Exchange Server 2007 installed uses the port range 1025-60000 by default.
In addition to the default range, all versions of Windows since Windows 2000 have the option of specifying a custom port range anywhere within 1025-65535.
On Linux you can see the ephemeral port range by viewing a pseudo file: