Resolves address for both requested address families, tries endpoints in prefer order, and creates a socket matching the endpoint that succeeds. This is the address-aware alternative to an "auto" socket domain. It supports IPv4 and IPv6 Internet sockets only; Unix-domain filesystem paths are not resolved by this helper. Use type = "dgram" for UDP or the default type = "stream" for TCP.

socket_connect_auto(
  address,
  port = NULL,
  type = c("stream", "dgram"),
  protocol = 0L,
  nonblocking = FALSE,
  cloexec = TRUE,
  prefer = c("inet6", "inet")
)

Arguments

address

A hostname or numeric IP address.

port

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

type

Socket type: "stream" or "dgram".

protocol

Numeric protocol, usually zero.

nonblocking

Whether the socket should be nonblocking.

cloexec

Whether to set close-on-exec.

prefer

Address families to try, in order.

Value

A connected socketR handle.

Examples

if (interactive()) {
  client <- socket_connect_auto("localhost", 80L)
  socket_close(client)
}