Creates a stream listener, tries bind candidates in prefer order, and returns the first listener that binds and listens successfully. When address is NULL, the family wildcard ("::" or "0.0.0.0") is used. This helper supports IPv4 and IPv6 stream sockets only. Unix-domain sockets require an explicit filesystem path with socket_create() and socket_bind().

socket_listen_auto(
  address = NULL,
  port = NULL,
  backlog = 128L,
  reuse_address = TRUE,
  cloexec = TRUE,
  prefer = c("inet6", "inet")
)

Arguments

address

A local hostname or IP address, or NULL for a wildcard.

port

A port between 0 and 65535, or NULL when embedded in a bracketed endpoint such as "[::1]:12345".

backlog

Maximum pending connection queue length.

reuse_address

Whether to set SO_REUSEADDR before binding.

cloexec

Whether to set close-on-exec.

prefer

Address families to try, in order.

Value

A listening socketR handle.

Examples

listener <- socket_listen_auto(port = 0L, prefer = "inet")
socket_local_name(listener)
#> $family
#> [1] "inet"
#> 
#> $address
#> [1] "0.0.0.0"
#> 
#> $port
#> [1] 42557
#> 
socket_close(listener)