pbdZMQ (version 0.2-5)

random_port: Random Port

Description

Generate a valid, random TCP port.

Usage

random_port(min_port = 49152, max_port = 65536)
random_open_port(min_port = 49152, max_port = 65536, max_tries = 100)

Arguments

min_port, max_port
The minimum/maximum value to be generated. The minimum should not be below 49152 and the maximum should not exceed 65536 (see details).
max_tries
The maximum number of times a random port will be searched for.

Details

By definition, a TCP port is an unsigned short, and so it can not exceed 65535. Additionally, ports in the range 1024 to 49151 are (possibly) registered by ICANN for specific uses.

random_port() will simply generate a valid, non-registered tcp port. random_unused_port() will generate a port that is available for socket connections.

random_open_port() finds a random port not already bound to an endpoint.

References

"The Ephemeral Port Range" by Mike Gleason. http://www.ncftp.com/ncftpd/doc/misc/ephemeral_ports.html

Examples

Run this code
random_port()

Run the code above in your browser using DataCamp Workspace