| Title: | POSIX Socket Programming for R |
|---|---|
| Description: | Provides a small POSIX sockets interface to R, enabling direct network communication from R for applications that need low-level socket control or lightweight client and server connections. This package provides IPv4 and IPv6 support with 'TCP/UDP' protocols. It functional API with 'socket_create' and an R6 interface through 'Socket' object. |
| Authors: | Eduardo Hellas [aut, cre], SAS [cph, fnd] |
| Maintainer: | Eduardo Hellas <[email protected]> |
| License: | Apache License (>= 2) |
| Version: | 1.0.5 |
| Built: | 2026-10-14 11:37:27 UTC |
| Source: | https://github.com/sassoftware/socketr |
External-pointer sockets implemented with base R's C API for POSIX TCP, UDP, IPv4, and IPv6 and Unix-domain socket operations, including bind, listen, accept, connect, send, receive, polling, shutdown, close, socket names, and typed options.
Socket handles are RAII-managed external pointers and should still be
closed explicitly with socket_close() when possible. Most system-call
failures include the POSIX errno name and number.
Maintainer: Eduardo Hellas [email protected]
Authors:
Eduardo Hellas [email protected]
Other contributors:
SAS [email protected] [copyright holder, funder]
Useful links:
Close an R6 Socket with the standard R connection API.
## S3 method for class 'Socket' close(con, ...)## S3 method for class 'Socket' close(con, ...)
con |
A |
... |
Ignored. |
Close a socketR handle with the standard R connection API.
## S3 method for class 'socketr_socket' close(con, ...)## S3 method for class 'socketr_socket' close(con, ...)
con |
A socketR socket handle. |
... |
Ignored. |
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.
An R6 Socket object.
handleThe underlying socketr_socket external pointer.
addressLocal address, when bound.
portLocal port, when bound.
peer_addressConnected peer address, when connected.
peer_portConnected peer port, when connected.
familyAddress family ("inet", "inet6", or "unix").
typeSocket type ("stream" or "dgram").
protocolNumeric socket protocol.
blockingWhether the socket is in blocking mode.
openWhether the underlying socket is open.
Socket$new()Create a socket wrapper or wrap an existing handle.
Socket$new(
domain = c("inet", "inet6", "unix"),
type = c("stream", "dgram"),
protocol = 0L,
nonblocking = FALSE,
cloexec = TRUE,
handle = NULL
)
domainAddress family passed to socket_create().
typeSocket type passed to socket_create().
protocolNumeric protocol passed to socket_create().
nonblockingWhether the new socket is nonblocking.
cloexecWhether the new socket is close-on-exec.
handleAn existing socketR handle to wrap.
Socket$new_auto()Create a connected client using IPv4/IPv6 endpoint fallback.
Socket$new_auto(
address,
port = NULL,
type = c("stream", "dgram"),
protocol = 0L,
nonblocking = FALSE,
cloexec = TRUE,
prefer = c("inet6", "inet")
)
addressHostname, IP address, or endpoint such as "[::1]:8080".
portOptional port when it is not embedded in address.
typeSocket type, "stream" (TCP) or "dgram" (UDP).
protocolNumeric protocol, usually zero.
nonblockingWhether the socket should be nonblocking.
cloexecWhether to set close-on-exec.
preferAddress families to try, in order.
A connected Socket R6 object.
Socket$new_listener()Create a listener using IPv4/IPv6 endpoint fallback.
Socket$new_listener(
address = NULL,
port = NULL,
backlog = 128L,
reuse_address = TRUE,
cloexec = TRUE,
prefer = c("inet6", "inet")
)
addressLocal hostname, IP address, endpoint, or NULL for wildcard.
portOptional port when it is not embedded in address.
backlogMaximum pending connection queue length.
reuse_addressWhether to set SO_REUSEADDR.
cloexecWhether to set close-on-exec.
preferAddress families to try, in order.
A listening Socket R6 object.
Socket$is_open()Test whether the wrapped socket is open.
Socket$is_open()
Socket$fd()Return the wrapped socket file descriptor.
Socket$fd()
Socket$info()Return metadata for the wrapped socket.
Socket$info()
Socket$set_blocking()Set blocking mode.
Socket$set_blocking(blocking = TRUE)
blockingWhether operations should block.
Socket$bind()Bind the wrapped socket.
Socket$bind(address = NULL, port = NULL)
addressLocal hostname, IP address, or Unix path.
portLocal port, or NULL for Unix sockets.
Socket$listen()Listen for stream connections.
Socket$listen(backlog = 128L)
backlogMaximum pending connection queue length.
Socket$accept()Accept a pending connection.
Socket$accept(nonblocking = NULL)
nonblockingWhether the accepted socket should be nonblocking.
Socket$connect()Connect the wrapped socket.
Socket$connect(address, port = NULL)
addressRemote hostname, IP address, or Unix path.
portRemote port, or NULL for Unix sockets.
Socket$as_connection()Return the wrapped socket as an R connection.
Socket$as_connection()
Socket$write()Write bytes to the wrapped socket.
Socket$write(object, flags = 0L)
objectRaw bytes or a character scalar.
flagsNative send() flags.
Socket$read()Read bytes from the wrapped socket.
Socket$read(n = 4096L, flags = 0L)
nMaximum number of bytes.
flagsNative recv() flags.
Socket$send()Send bytes on the wrapped socket.
Socket$send(data, flags = 0L)
dataRaw bytes or a character scalar.
flagsNative send() flags.
Socket$receive()Receive bytes from the wrapped socket.
Socket$receive(n = 4096L, flags = 0L)
nMaximum number of bytes.
flagsNative recv() flags.
Socket$send_to()Send a datagram.
Socket$send_to(data, address, port = NULL, flags = 0L)
dataRaw bytes or a character scalar.
addressDestination hostname or IP address.
portDestination port.
flagsNative sendto() flags.
Socket$receive_from()Receive a datagram.
Socket$receive_from(n = 4096L, flags = 0L)
nMaximum datagram payload size.
flagsNative recvfrom() flags.
Socket$shutdown()Shut down part of the connection.
Socket$shutdown(how = c("both", "read", "write"))
howOne of "read", "write", or "both".
Socket$close()Close the wrapped socket.
Socket$close()
Socket$local_name()Return the local socket name.
Socket$local_name()
Socket$peer_name()Return the peer socket name.
Socket$peer_name()
Socket$poll()Poll the wrapped socket.
Socket$poll(events = "read", timeout_ms = 60000L)
eventsRequested readiness events.
timeout_msTimeout in milliseconds.
Socket$get_option()Get a socket option.
Socket$get_option(
level = "socket",
option,
type = c("int", "logical", "timeval", "linger", "raw"),
size = 256L
)
levelOption level.
optionOption name or numeric constant.
typeReturn type.
sizeMaximum raw option size.
Socket$set_options()Set multiple socket options.
Socket$set_options(options)
optionsOption specifications.
Socket$set_option()Set a socket option.
Socket$set_option(
level = "socket",
option,
value,
type = c("int", "logical", "timeval", "linger", "raw")
)
levelOption level.
optionOption name or numeric constant.
valueOption value.
typeValue type.
Socket$clone()The objects of this class are cloneable with this method.
Socket$clone(deep = FALSE)
deepWhether to make a deep clone.
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") )socket_connect_auto( address, port = NULL, type = c("stream", "dgram"), protocol = 0L, nonblocking = FALSE, cloexec = TRUE, prefer = c("inet6", "inet") )
address |
A hostname or numeric IP address. |
port |
A port between 0 and 65535, or |
type |
Socket type: |
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. |
A connected socketR handle.
if (interactive()) { client <- socket_connect_auto("localhost", 80L) socket_close(client) }if (interactive()) { client <- socket_connect_auto("localhost", 80L) socket_close(client) }
Adapt a socket handle to an R connection.
The returned connection can be passed to base R functions such as
readBin(), writeBin(), readLines(), and writeLines(). Closing the
connection does not close the underlying socket handle; call socket_close()
explicitly when the handle is no longer needed.
Uses R's connection dispatch, so this works with socket_connection() and
other readable R connections.
Uses R's connection dispatch, so this works with socket_connection() and
other writable R connections.
socket_connection(socket, close_socket = FALSE) socket_connection_read(connection, n = 4096L) socket_connection_write(connection, data)socket_connection(socket, close_socket = FALSE) socket_connection_read(connection, n = 4096L) socket_connection_write(connection, data)
socket |
A socketR socket handle. |
close_socket |
Whether closing the adapter should also close the
underlying socket. The default |
connection |
An open R connection. |
n |
Maximum number of bytes to read. |
data |
A raw vector. |
An R connection object.
A raw vector, possibly shorter than n at end-of-file.
Number of bytes written.
s <- socket_create() con <- socket_connection(s) close(con) socket_close(s)s <- socket_create() con <- socket_connection(s) close(con) socket_close(s)
Send bytes to a datagram destination.
socket_send_to(socket, data, address, port = NULL, flags = 0L) socket_receive_from(socket, n = 4096L, flags = 0L)socket_send_to(socket, data, address, port = NULL, flags = 0L) socket_receive_from(socket, n = 4096L, flags = 0L)
socket |
A datagram socket handle. |
data |
A raw vector or character scalar. |
address |
Destination hostname, IP address, or Unix-domain path. |
port |
Destination port; |
flags |
Native |
n |
Maximum datagram payload size. |
Number of bytes sent, or NA_integer_ if I/O would block.
A list with data and address, or NULL if I/O would block.
if (interactive()) { receiver <- socket_create(type = "dgram") sender <- socket_create(type = "dgram") socket_bind(receiver, "127.0.0.1", 0L) socket_send_to(sender, "hello", "127.0.0.1", socket_local_name(receiver)$port) socket_receive_from(receiver) socket_close(sender); socket_close(receiver) }if (interactive()) { receiver <- socket_create(type = "dgram") sender <- socket_create(type = "dgram") socket_bind(receiver, "127.0.0.1", 0L) socket_send_to(sender, "hello", "127.0.0.1", socket_local_name(receiver)$port) socket_receive_from(receiver) socket_close(sender); socket_close(receiver) }
Return the local socket name.
socket_fd(socket) socket_info(socket) socket_local_name(socket) socket_peer_name(socket)socket_fd(socket) socket_info(socket) socket_local_name(socket) socket_peer_name(socket)
socket |
A connected socket handle. |
An integer descriptor or NA_integer_.
A list containing socket metadata.
Address metadata as a list.
Address metadata as a list.
s <- socket_create() socket_local_name(s) socket_close(s)s <- socket_create() socket_local_name(s) socket_close(s)
Get a socket option.
socket_get_option( socket, level = "socket", option, type = c("int", "logical", "timeval", "linger", "raw"), size = 256L )socket_get_option( socket, level = "socket", option, type = c("int", "logical", "timeval", "linger", "raw"), size = 256L )
socket |
A socketR socket handle. |
level |
Option level such as |
option |
Option name or native numeric constant. |
type |
Return type: |
size |
Maximum raw option size. |
The option value.
Write bytes to a connected socket.
This is a connection-style alias for socket_send(). It returns the number
of bytes written, which may be less than the input length for a nonblocking
socket or a large payload.
This is a connection-style alias for socket_receive(). Blocking sockets
wait until at least one byte is available or the peer closes the connection.
socket_write(socket, object, flags = 0L) socket_send(socket, data, flags = 0L) socket_read(socket, n = 4096L, flags = 0L) socket_receive(socket, n = 4096L, flags = 0L)socket_write(socket, object, flags = 0L) socket_send(socket, data, flags = 0L) socket_read(socket, n = 4096L, flags = 0L) socket_receive(socket, n = 4096L, flags = 0L)
socket |
A connected socket handle. |
object |
A raw vector or character scalar. |
flags |
Native |
data |
A raw vector or character scalar. |
n |
Maximum number of bytes to receive. |
Number of bytes written, or NA_integer_ if I/O would block.
Number of bytes sent, or NA_integer_ if nonblocking I/O would block.
A raw vector, or NULL if a nonblocking read would block.
A raw vector, or NULL if nonblocking I/O would block.
if (interactive()) { server <- socket_create() client <- socket_create() socket_bind(server, "127.0.0.1", 0L) socket_listen(server) socket_connect(client, "127.0.0.1", socket_local_name(server)$port) peer <- socket_accept(server) socket_write(client, "hello") socket_read(peer, 5L) socket_close(peer); socket_close(client); socket_close(server) }if (interactive()) { server <- socket_create() client <- socket_create() socket_bind(server, "127.0.0.1", 0L) socket_listen(server) socket_connect(client, "127.0.0.1", socket_local_name(server)$port) peer <- socket_accept(server) socket_write(client, "hello") socket_read(peer, 5L) socket_close(peer); socket_close(client); socket_close(server) }
Manage a socket handle lifecycle.
Test whether a socket handle is open.
Shut down part of a full-duplex socket.
socket_close(socket) socket_is_open(socket) socket_shutdown(socket, how = c("both", "read", "write"))socket_close(socket) socket_is_open(socket) socket_shutdown(socket, how = c("both", "read", "write"))
socket |
A socketR socket handle. |
how |
One of |
Invisibly, TRUE.
A logical scalar.
Invisibly, the socket handle.
s <- socket_create() socket_close(s)s <- socket_create() socket_close(s)
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") )socket_listen_auto( address = NULL, port = NULL, backlog = 128L, reuse_address = TRUE, cloexec = TRUE, prefer = c("inet6", "inet") )
address |
A local hostname or IP address, or |
port |
A port between 0 and 65535, or |
backlog |
Maximum pending connection queue length. |
reuse_address |
Whether to set |
cloexec |
Whether to set close-on-exec. |
prefer |
Address families to try, in order. |
A listening socketR handle.
listener <- socket_listen_auto(port = 0L, prefer = "inet") socket_local_name(listener) socket_close(listener)listener <- socket_listen_auto(port = 0L, prefer = "inet") socket_local_name(listener) socket_close(listener)
These helpers read or set frequently used socket options. With the value
argument omitted, the current option value is returned; otherwise the
option is updated and the socket handle is returned invisibly. The
socket_linger() helper uses on and seconds to configure SO_LINGER.
socket_reuse_address(socket, value) socket_keep_alive(socket, value) socket_broadcast(socket, value) socket_no_delay(socket, value) socket_quick_ack(socket, value) socket_receive_buffer_size(socket, value) socket_send_buffer_size(socket, value) socket_receive_timeout(socket, value) socket_send_timeout(socket, value) socket_linger(socket, on, seconds = 0L)socket_reuse_address(socket, value) socket_keep_alive(socket, value) socket_broadcast(socket, value) socket_no_delay(socket, value) socket_quick_ack(socket, value) socket_receive_buffer_size(socket, value) socket_send_buffer_size(socket, value) socket_receive_timeout(socket, value) socket_send_timeout(socket, value) socket_linger(socket, on, seconds = 0L)
socket |
A socketR socket handle. |
value |
Optional option value to set. Its type depends on the helper: logical, integer, or numeric seconds. |
on |
Optional logical linger enable flag. |
seconds |
Linger duration in seconds. |
The option value when reading; invisibly, the socket when setting.
s <- socket_create() socket_reuse_address(s, TRUE) socket_reuse_address(s) socket_close(s)s <- socket_create() socket_reuse_address(s, TRUE) socket_reuse_address(s) socket_close(s)
Control socket blocking and readiness.
Poll sockets for readiness.
socket_set_blocking(socket, blocking = TRUE) socket_poll(sockets, events = "read", timeout_ms = 60000L)socket_set_blocking(socket, blocking = TRUE) socket_poll(sockets, events = "read", timeout_ms = 60000L)
socket |
A socketR socket handle. |
blocking |
Whether operations should block. |
sockets |
A socket handle or list of handles. |
events |
Requested |
timeout_ms |
Timeout in milliseconds; use |
Invisibly, the socket handle.
A data frame with readiness flags and native revents values.
s <- socket_create() socket_set_blocking(s, FALSE) socket_close(s) s <- socket_create() socket_poll(s, "read", timeout_ms = 0L) socket_close(s)s <- socket_create() socket_set_blocking(s, FALSE) socket_close(s) s <- socket_create() socket_poll(s, "read", timeout_ms = 0L) socket_close(s)
Resolve a hostname or address into socket endpoints.
socket_resolve(address, domain = c("inet", "inet6"), port = 0L)socket_resolve(address, domain = c("inet", "inet6"), port = 0L)
address |
A hostname or numeric IP address. |
domain |
Address family, such as |
port |
Numeric service port between 0 and 65535. |
A list of resolved endpoint metadata.
Set a socket option.
socket_set_option( socket, level = "socket", option, value, type = c("int", "logical", "timeval", "linger", "raw") )socket_set_option( socket, level = "socket", option, value, type = c("int", "logical", "timeval", "linger", "raw") )
socket |
A socketR socket handle. |
level |
Option level such as |
option |
Option name or native numeric constant. |
value |
Value matching |
type |
Value type: |
Invisibly, the socket handle.
Each element of options must be a list containing option and value,
with optional level and type entries. Options are applied in list order.
If a native option fails, the error includes the zero-based count of options
already applied in its applied field.
socket_set_options(socket, options)socket_set_options(socket, options)
socket |
A socketR socket handle. |
options |
A non-empty list of option specifications. |
The socket handle, invisibly.
Create a POSIX socket.
socket_create( domain = c("inet", "inet6", "unix"), type = c("stream", "dgram"), protocol = 0L, nonblocking = FALSE, cloexec = TRUE ) socket_bind(socket, address = NULL, port = NULL) socket_listen(socket, backlog = 128L) socket_accept(socket, nonblocking = NULL) socket_connect(socket, address, port = NULL)socket_create( domain = c("inet", "inet6", "unix"), type = c("stream", "dgram"), protocol = 0L, nonblocking = FALSE, cloexec = TRUE ) socket_bind(socket, address = NULL, port = NULL) socket_listen(socket, backlog = 128L) socket_accept(socket, nonblocking = NULL) socket_connect(socket, address, port = NULL)
domain |
Address family: |
type |
Socket type: |
protocol |
Numeric protocol, usually zero. |
nonblocking |
Whether the accepted socket should be nonblocking; |
cloexec |
Whether to set close-on-exec. |
socket |
A socketR socket handle. |
address |
A hostname, IP address, or Unix-domain path. |
port |
A port between 0 and 65535; |
backlog |
Maximum pending connection queue length. |
A socketR external-pointer handle.
Invisibly, the socket handle.
Invisibly, the socket handle.
A socket handle, or NULL when no connection is pending.
TRUE when connected, or FALSE when connection is in progress.
s <- socket_create() socket_close(s)s <- socket_create() socket_close(s)