Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

randomPort

Find an open TCP port


Description

Finds a random available TCP port for listening on, within a specified range of ports. The default range of ports to check is 1024 to 49151, which is the set of TCP User Ports. This function automatically excludes some ports which are considered unsafe by web browsers.

Usage

randomPort(min = 1024L, max = 49151L, host = "127.0.0.1", n = 20)

Arguments

min

Minimum port number.

max

Maximum port number.

host

A string that is a valid IPv4 or IPv6 address that is owned by this server, which the application will listen on. "0.0.0.0" represents all IPv4 addresses and "::/0" represents all IPv6 addresses.

n

Number of ports to try before giving up.

Value

A port that is available to listen on.

Examples

## Not run: 
s <- startServer("127.0.0.1", randomPort(), list())
browseURL(paste0("http://127.0.0.1:", s$getPort()))

s$stop()

## End(Not run)

httpuv

HTTP and WebSocket Server Library

v1.6.1
GPL (>= 2) | file LICENSE
Authors
Joe Cheng [aut], Winston Chang [aut, cre], RStudio, PBC [cph], Hector Corrada Bravo [ctb], Jeroen Ooms [ctb], Andrzej Krzemienski [cph] (optional.hpp)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.