RFC6335 by IANA recommends the port range 49152-65535 as dynamic or ephemeral ports 1. Ephemeral ports as the name suggests are to be used for temporay connections. Most softwares rely on the operating system to create a socket for an outgoing connection and the operating system picks one of the dynamic port for that socket connection.

Note

The dynamic port range might differ based on the OS.

For example, when you open a website in google chrome, chrome has to initiate a tcp connection with the server which requires local port. Chrome relies on the underlying socket implementation of the OS to create a socket connection. The OS will pick a port for this connection based on what dynamic ports are available. Look at net/socket/tcp_socket_posix.cc and net/socket/tcp_socket_win.cc.

Java NIO selectors also rely on a similar mechanism. Explored this during Jetty establishing multiple TCP connections.

Footnotes

  1. https://datatracker.ietf.org/doc/html/rfc6335#section-6