The public methods mirror the functional API: is_open(), fd(), info(), set_blocking(), bind(), listen(), accept(), connect(), send(), receive(), send_to(), receive_from(), shutdown(), close(), local_name(), peer_name(), poll(), get_option(), set_option(), and set_options(). They delegate to the corresponding documented functions while retaining the handle as object state.

Use Socket$new_auto() to create a connected client with IPv4/IPv6 fallback, or Socket$new_listener() to create a listening server with IPv4/IPv6 fallback.

Value

An R6 Socket object.

Public fields

handle

The underlying socketr_socket external pointer.

Active bindings

address

Local address, when bound.

port

Local port, when bound.

peer_address

Connected peer address, when connected.

peer_port

Connected peer port, when connected.

family

Address family ("inet", "inet6", or "unix").

type

Socket type ("stream" or "dgram").

protocol

Numeric socket protocol.

blocking

Whether the socket is in blocking mode.

open

Whether the underlying socket is open.

Methods


Socket$new()

Create a socket wrapper or wrap an existing handle.

Usage

Socket$new(
  domain = c("inet", "inet6", "unix"),
  type = c("stream", "dgram"),
  protocol = 0L,
  nonblocking = FALSE,
  cloexec = TRUE,
  handle = NULL
)

Arguments

domain

Address family passed to socket_create().

type

Socket type passed to socket_create().

protocol

Numeric protocol passed to socket_create().

nonblocking

Whether the new socket is nonblocking.

cloexec

Whether the new socket is close-on-exec.

handle

An existing socketR handle to wrap.


Socket$new_auto()

Create a connected client using IPv4/IPv6 endpoint fallback.

Usage

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

Arguments

address

Hostname, IP address, or endpoint such as "[::1]:8080".

port

Optional port when it is not embedded in address.

type

Socket type, "stream" (TCP) or "dgram" (UDP).

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.

Returns

A connected Socket R6 object.


Socket$new_listener()

Create a listener using IPv4/IPv6 endpoint fallback.

Usage

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

Arguments

address

Local hostname, IP address, endpoint, or NULL for wildcard.

port

Optional port when it is not embedded in address.

backlog

Maximum pending connection queue length.

reuse_address

Whether to set SO_REUSEADDR.

cloexec

Whether to set close-on-exec.

prefer

Address families to try, in order.

Returns

A listening Socket R6 object.


Socket$is_open()

Test whether the wrapped socket is open.

Usage

Socket$is_open()


Socket$fd()

Return the wrapped socket file descriptor.

Usage

Socket$fd()


Socket$info()

Return metadata for the wrapped socket.

Usage

Socket$info()


Socket$set_blocking()

Set blocking mode.

Usage

Socket$set_blocking(blocking = TRUE)

Arguments

blocking

Whether operations should block.


Socket$bind()

Bind the wrapped socket.

Usage

Socket$bind(address = NULL, port = NULL)

Arguments

address

Local hostname, IP address, or Unix path.

port

Local port, or NULL for Unix sockets.


Socket$listen()

Listen for stream connections.

Usage

Socket$listen(backlog = 128L)

Arguments

backlog

Maximum pending connection queue length.


Socket$accept()

Accept a pending connection.

Usage

Socket$accept(nonblocking = NULL)

Arguments

nonblocking

Whether the accepted socket should be nonblocking.


Socket$connect()

Connect the wrapped socket.

Usage

Socket$connect(address, port = NULL)

Arguments

address

Remote hostname, IP address, or Unix path.

port

Remote port, or NULL for Unix sockets.


Socket$as_connection()

Return the wrapped socket as an R connection.

Usage

Socket$as_connection()


Socket$write()

Write bytes to the wrapped socket.

Usage

Socket$write(object, flags = 0L)

Arguments

object

Raw bytes or a character scalar.

flags

Native send() flags.


Socket$read()

Read bytes from the wrapped socket.

Usage

Socket$read(n = 4096L, flags = 0L)

Arguments

n

Maximum number of bytes.

flags

Native recv() flags.


Socket$send()

Send bytes on the wrapped socket.

Usage

Socket$send(data, flags = 0L)

Arguments

data

Raw bytes or a character scalar.

flags

Native send() flags.


Socket$receive()

Receive bytes from the wrapped socket.

Usage

Socket$receive(n = 4096L, flags = 0L)

Arguments

n

Maximum number of bytes.

flags

Native recv() flags.


Socket$send_to()

Send a datagram.

Usage

Socket$send_to(data, address, port = NULL, flags = 0L)

Arguments

data

Raw bytes or a character scalar.

address

Destination hostname or IP address.

port

Destination port.

flags

Native sendto() flags.


Socket$receive_from()

Receive a datagram.

Usage

Socket$receive_from(n = 4096L, flags = 0L)

Arguments

n

Maximum datagram payload size.

flags

Native recvfrom() flags.


Socket$shutdown()

Shut down part of the connection.

Usage

Socket$shutdown(how = c("both", "read", "write"))

Arguments

how

One of "read", "write", or "both".


Socket$close()

Close the wrapped socket.

Usage

Socket$close()


Socket$local_name()

Return the local socket name.

Usage

Socket$local_name()


Socket$peer_name()

Return the peer socket name.

Usage

Socket$peer_name()


Socket$poll()

Poll the wrapped socket.

Usage

Socket$poll(events = "read", timeout_ms = 60000L)

Arguments

events

Requested readiness events.

timeout_ms

Timeout in milliseconds.


Socket$get_option()

Get a socket option.

Usage

Socket$get_option(
  level = "socket",
  option,
  type = c("int", "logical", "timeval", "linger", "raw"),
  size = 256L
)

Arguments

level

Option level.

option

Option name or numeric constant.

type

Return type.

size

Maximum raw option size.


Socket$set_options()

Set multiple socket options.

Usage

Socket$set_options(options)

Arguments

options

Option specifications.


Socket$set_option()

Set a socket option.

Usage

Socket$set_option(
  level = "socket",
  option,
  value,
  type = c("int", "logical", "timeval", "linger", "raw")
)

Arguments

level

Option level.

option

Option name or numeric constant.

value

Option value.

type

Value type.


Socket$clone()

The objects of this class are cloneable with this method.

Usage

Socket$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.