random_port function

Find a random available TCP port

Find a random available TCP port

Test a series of random TCP ports from 3000 to 8000 (excluding a few that are considered unsafe by Chrome) and return the first available one. A web server can be later started on this port.

random_port( port = 4321L, host = getOption("servr.host", "127.0.0.1"), n = 20, exclude = NULL )

Arguments

  • port: The preferred port(s).
  • host: A string that is a valid IPv4 address that is owned by this server, or "0.0.0.0" to listen on all IP addresses.
  • n: The maximum number of random ports to be tested.
  • exclude: A vector of port numbers not to be considered.

Returns

A port number, or an error if no ports are available.

  • Maintainer: Yihui Xie
  • License: GPL
  • Last published: 2024-10-04