module Socket

Sockets: require "socket". Every operation that would block suspends only the calling task; Task.timeout and cancel interrupt it. Addresses are Socket::Addresses; Socket has the ways to connect and serve, and the classes TCPSocket, TCPServer, UDPSocket, UNIXSocket and UNIXServer the connections.

<std:socket>:114

Classes and modules

Constants

RESOLUTION_DELAY: Duration

Happy Eyeballs (RFC 8305): how long to wait for the IPv6 addresses once the IPv4 ones are in, and before starting another attempt while one is under way.

<std:socket>:136

CONNECTION_ATTEMPT_DELAY: Duration

Happy Eyeballs: how long an attempt runs before the next is started.

<std:socket>:138

Class methods

def self.getaddrinfo(host: String, port: Int, family: Int? = …) -> Array[Socket::Address]

The addresses of host for port, of family 4 or 6, or both: a numeric host at once, a name looked up on a helper thread (only this task waits). A name with none raises SocketError.

<std:socket>:143

def self.tcp(host: String, port: Int, connect_timeout: Duration? = …) -> TCPSocket

A connection to host (a name or a numeric address) at port. The IPv6 and IPv4 addresses are looked up at once and tried by Happy Eyeballs (RFC 8305): alternating the families, IPv6 first, another attempt started when one fails or has not connected in 250 ms; the first to connect wins, the others are cancelled. The last failure is raised when none connects (SystemCallError; SocketError when the name has no address). connect_timeout bounds it all (Task::Timeout).

<std:socket>:153

def self.tcp[R](host: String, port: Int, connect_timeout: Duration? = …, &: (TCPSocket) -> R) -> R

The same, with the connection given to the block and closed after it.

<std:socket>:162

def self.tcp_server(port: Int) -> TCPServer

A server listening on port of every IPv4 address: TCPServer.new.

<std:socket>:173

def self.tcp_server(host: String, port: Int) -> TCPServer

A server listening on port of host: TCPServer.new.

<std:socket>:175

def self.tcp_server_loop(port: Int, on_error: shareable (Exception, Socket::Address?) -> Void? = …, &handler: shareable (TCPSocket, Socket::Address) -> Void) -> Void

Listens on port of every address and serves each connection with the block until the calling task is cancelled.

Each connection is handed to handler with the peer's address, in a task of its own (so the handler is a shareable procedure), and closed when it returns. What the handler raises (a StandardError) goes to on_error with the peer's address, or to STDERR; the server goes on. A failed accept (out of file descriptors, a connection reset before it was taken) does not end the loop either: it goes to on_error with no address, or to STDERR, and the loop accepts again 100 ms later.

<std:socket>:188

def self.tcp_server_loop(host: String, port: Int, on_error: shareable (Exception, Socket::Address?) -> Void? = …, &handler: shareable (TCPSocket, Socket::Address) -> Void) -> Void

The same, on port of host only.

Socket.tcp_server_loop("127.0.0.1", 7000) do |conn, peer|
  line = conn.gets?
  conn.puts line if line
end

<std:socket>:201

def self.accept_loop(server: TCPServer, on_error: shareable (Exception, Socket::Address?) -> Void? = …, &handler: shareable (TCPSocket, Socket::Address) -> Void) -> Void

As tcp_server_loop, on a server already listening (its port known before the first connection: TCPServer.new(0)). The server is closed when the loop ends.

<std:socket>:210

def self.accept_loop(server: UNIXServer, on_error: shareable (Exception) -> Void? = …, &handler: shareable (UNIXSocket) -> Void) -> Void

The same on a UNIX socket server, whose peers have no address.

<std:socket>:249

def self.unix(path: String) -> UNIXSocket

A connection to the UNIX socket at path: UNIXSocket.new.

<std:socket>:286

def self.unix[R](path: String, &: (UNIXSocket) -> R) -> R

The same, with the connection given to the block and closed after it.

<std:socket>:288

def self.unix_server(path: String) -> UNIXServer

A server listening at path: UNIXServer.new.

<std:socket>:290

def self.unix_server_loop(path: String, on_error: shareable (Exception) -> Void? = …, &handler: shareable (UNIXSocket) -> Void) -> Void

As tcp_server_loop, on a UNIX socket at path (removed when the server closes).

<std:socket>:294