Module socket

Data Types

domain()


domain() = inet

in_addr()


in_addr() = {0..255, 0..255, 0..255, 0..255}

ip_mreq()


ip_mreq() = #{multiaddr => in_addr(), interface => in_addr()}

port_number()


port_number() = 0..65535

protocol()


protocol() = tcp | udp

sockaddr()


sockaddr() = sockaddr_in()

sockaddr_in()


sockaddr_in() = #{family => inet, port => port_number(), addr => any | loopback | in_addr()}

socket()

abstract datatype: socket()

socket_option()


socket_option() = {socket, reuseaddr | linger | type} | {otp, recvbuf} | {ip, add_membership}

type()


type() = stream | dgram

Function Index

accept/1Equivalent to socket:accept(ListeningSocket, infinity).
accept/2 Wait for the socket to accept a connection.
bind/2 Bind a socket to an interface.
close/1 Close a socket.
connect/2 Wait for the socket to connect to an address.
getopt/2 Get a socket option.
listen/1 Set the socket to listen for connections.
listen/2 Set the socket to listen for connections.
open/3 Create a socket.
peername/1 Return the address of the peer connected to the specified socket.
recv/1Equivalent to socket:recv(Socket, 0).
recv/2Equivalent to socket:recv(Socket, Length, infinity).
recv/3 Receive data on the specified socket.
recvfrom/1Equivalent to socket:recvfrom(Socket, 0).
recvfrom/2Equivalent to socket:recvfrom(Socket, Length, infinity).
recvfrom/3 Receive data on the specified socket, returning the from address.
send/2Equivalent to socket:send(Socket, Data, infinity).
send/3 Send data on the specified socket.
sendto/3Equivalent to socket:sendto(Socket, Data, Dest, infinity).
sendto/4 Send data on the specified socket to the specified destination.
setopt/3 Set a socket option.
shutdown/2 Shut down one or both ends of a full-duplex socket connection.
sockname/1 Return the current address for the specified socket.

Function Details

accept/1


accept(Socket::socket()) -> {ok, Connection::socket()} | {error, Reason::term()}

Equivalent to socket:accept(ListeningSocket, infinity).

accept/2


accept(Socket::socket(), Timeout::timeout() | nowait | reference()) -> {ok, Connection::socket()} | {select, {select_info, accept, reference()}} | {error, Reason::term()}

Socket: the socket
Timeout: timeout (in milliseconds)

returns: {ok, Connection} if successful; {error, Reason}, otherwise.

Wait for the socket to accept a connection.

Wait for the socket to accept a connection. The socket should be set to listen for connections.

Note that this function will block until a connection is made from a client, unless nowait or a reference is passed as Timeout. Typically, users will spawn a call to accept in a separate process.

Example:

{ok, ConnectedSocket} = socket:accept(ListeningSocket)

bind/2


bind(Socket::socket(), Address::sockaddr() | any | loopback) -> ok | {error, Reason::term()}

Socket: the socket
Address: the address to which to bind the socket

returns: ok if successful; {error, Reason}, otherwise.

Bind a socket to an interface.

Bind a socket to an interface, via a socket address. Use any to bind to all interfaces. Use loopback to bind to the loopback address. To specify a port, use a map containing the network family, address, and port.

Example:

ok = socket:bind(ListeningSocket, #{family => inet, addr => any, port => 44404})

close/1


close(Socket::socket()) -> ok | {error, Reason::term()}

Socket: the socket

returns: ok if successful; {error, Reason}, otherwise.

Close a socket.

Close a previously opened socket.

Example:

ok = socket:close(Socket)

connect/2


connect(Socket::socket(), Address::sockaddr()) -> ok | {error, Reason::term()}

Socket: the socket
Address: the address to which to connect the socket

returns: ok if successful; {error, Reason}, otherwise.

Wait for the socket to connect to an address.

Wait for the socket to connect to an address. The socket should be a connection-based socket.

Note that this function will block the calling process until a connection is made to a server, or the attempt fails. Only the calling process: on platforms with BSD sockets the connect is non-blocking, and this waits for the socket to become writable, so a peer that never answers does not stop the scheduler, and killing the caller abandons the attempt.

Example:

ok = socket:connect(Socket, #{family => inet, addr => loopback, port => 44404})

getopt/2


getopt(Socket::socket(), SocketOption::socket_option()) -> {ok, Value::term()} | {error, Reason::term()}

Socket: the socket
SocketOption: the option

returns: {ok, Value} if successful; {error, Reason}, otherwise.

Get a socket option.

Currently, the following options are supported:

{socket, type}type()

Example:

{ok, stream} = socket:getopt(ListeningSocket, {socket, type})

listen/1


listen(Socket::socket()) -> ok | {error, Reason::term()}

Socket: the socket

returns: ok if successful; {error, Reason}, otherwise.

Set the socket to listen for connections.

Listen for connections. The socket should be a connection-based socket and should be bound to an address and port.

Example:

ok = socket:listen(ListeningSocket)

listen/2


listen(Socket::socket(), Backlog::integer()) -> ok | {error, Reason::term()}

Socket: the socket
Backlog: the maximum length for the queue of pending connections

returns: ok if successful; {error, Reason}, otherwise.

Set the socket to listen for connections.

Listen for connections. The socket should be a connection-based socket and should be bound to an address and port.

Use the Backlog to specify the maximum length for the queue of pending connections

Example:

ok = socket:listen(ListeningSocket, 4)

open/3


open(Domain::domain(), Type::type(), Protocol::protocol()) -> {ok, socket()} | {error, Reason::term()}

Domain: the network domain
Type: the network type
Protocol: the network protocol

returns: {ok, Socket} if successful; {error, Reason}, otherwise.

Create a socket.

Create a socket with a specified domain, type, and protocol. Use the returned socket for communications.

Example:

{ok, ListeningSocket} = socket:open(inet, stream, tcp)

peername/1


peername(Socket::socket()) -> {ok, Address::sockaddr()} | {error, Reason::term()}

Socket: the socket

returns: {ok, Address} if successful; {error, Reason}, otherwise.

Return the address of the peer connected to the specified socket.

Example:

{ok, Address} = socket:peername(ConnectedSocket)

recv/1


recv(Socket::socket()) -> {ok, Data::binary()} | {error, Reason::term()}

Equivalent to socket:recv(Socket, 0).

recv/2


recv(Socket::socket(), Length::non_neg_integer()) -> {ok, Data::binary()} | {error, Reason::term()}

Equivalent to socket:recv(Socket, Length, infinity).

recv/3


recv(Socket::socket(), Length::non_neg_integer(), Timeout::timeout() | nowait | reference()) -> {ok, Data::binary()} | {select, {select_info, recvfrom, reference()}} | {select, {{select_info, recvfrom, reference()}, Data::binary()}} | {error, Reason::term()}

Socket: the socket
Length: number of bytes to receive
Timeout: timeout (in milliseconds)

returns: {ok, Data} if successful; {error, Reason}, otherwise.

Receive data on the specified socket.

This function is equivalent to recvfrom/3 except for the return type.

Example:

{ok, Data} = socket:recv(ConnectedSocket)

recvfrom/1


recvfrom(Socket::socket()) -> {ok, {Address::sockaddr(), Data::binary()}} | {error, Reason::term()}

Equivalent to socket:recvfrom(Socket, 0).

recvfrom/2


recvfrom(Socket::socket(), Length::non_neg_integer()) -> {ok, {Address::sockaddr(), Data::binary()}} | {error, Reason::term()}

Equivalent to socket:recvfrom(Socket, Length, infinity).

recvfrom/3


recvfrom(Socket::socket(), Length::non_neg_integer(), Timeout::timeout() | nowait | reference()) -> {ok, {Address::sockaddr(), Data::binary()}} | {select, {select_info, recvfrom, reference()}} | {error, Reason::term()}

Socket: the socket
Length: number of bytes to receive
Timeout: timeout (in milliseconds)

returns: {ok, {Address, Data}} if successful; {error, Reason}, otherwise.

Receive data on the specified socket, returning the from address.

Note that this function will block until data is received on the socket.

Example:

{ok, {Address, Data}} = socket:recvfrom(ConnectedSocket)

If socket is UDP, the function retrieves the first available packet and truncate it to Length bytes, unless Length is 0 in which case it returns the whole packet (“all available”).

If socket is TCP and Length is 0, this function retrieves all available data without waiting (using peek if the platform allows it). If socket is TCP and Length is not 0, this function waits until Length bytes are available and return these bytes.

send/2


send(Socket::socket(), Data::iodata()) -> ok | {error, Reason::term()} | {error, {Reason::term(), RestData::binary()}}

Equivalent to socket:send(Socket, Data, infinity).

send/3


send(Socket::socket(), Data::iodata(), Timeout::timeout() | nowait | reference()) -> ok | {select, {select_info, send, reference()}} | {select, {{select_info, send, reference()}, RestData::binary()}} | {error, Reason::term()} | {error, {Reason::term(), RestData::binary()}}

Socket: the socket
Data: the data to send
Timeout: timeout (in milliseconds), infinity, nowait or a select handle (a reference)

returns: ok if all data was accepted by the stack; {error, Reason} if an error occurred before any data was accepted, or {error, {Reason, RestData}} if an error occurred after some of the data was accepted, RestData being the data that was not sent; {select, SelectInfo} or {select, {SelectInfo, RestData}} if Timeout is nowait or a select handle and the data could not be sent (completely) without blocking.

Send data on the specified socket.

This function blocks until all of Data has been accepted by the stack, until an error occurs or until Timeout milliseconds have elapsed, in which case Reason is timeout. The data may not have been received by the intended recipient, and may not even have been sent over the network. Partial sends and transient send backpressure (e.g. a full TCP send buffer) are retried internally, waiting for the socket to become writable again, so callers do not need to implement their own retry logic for eagain-like conditions.

If Timeout is nowait or a select handle, this function never blocks. If the data cannot be sent (completely) right away, it returns {select, SelectInfo} when no data was accepted or {select, {SelectInfo, RestData}} when some data was accepted, SelectInfo being {select_info, send, SelectHandle}. The calling process then receives {$socket’, Socket, select, SelectHandle}’ when the socket is writable again, and should call this function again with RestData, or {$socket’, Socket, abort, {SelectHandle, Reason}}’ if the socket was closed meanwhile.

A process that waited for the socket to become writable stays the owner of the write direction of the socket, as a process that called recv/1,2,3 stays the owner of its read direction: the socket is monitored by the process and the ownership is released when the socket is closed, when another process selects on the same direction or when the process terminates.

Example:

ok = socket:send(ConnectedSocket, Data, 5000)

sendto/3


sendto(Socket::socket(), Data::iodata(), Dest::sockaddr()) -> ok | {error, Reason::term()} | {error, {Reason::term(), RestData::binary()}}

Equivalent to socket:sendto(Socket, Data, Dest, infinity).

sendto/4


sendto(Socket::socket(), Data::iodata(), Dest::sockaddr(), Timeout::timeout() | nowait | reference()) -> ok | {select, {select_info, sendto, reference()}} | {select, {{select_info, sendto, reference()}, RestData::binary()}} | {error, Reason::term()} | {error, {Reason::term(), RestData::binary()}}

Socket: the socket
Data: the data to send
Dest: the destination to which to send the data
Timeout: timeout (in milliseconds), infinity, nowait or a select handle (a reference)

returns: ok if successful; {error, Reason}, otherwise; {select, SelectInfo} if Timeout is nowait or a select handle and the data could not be sent without blocking.

Send data on the specified socket to the specified destination.

This function blocks until the data has been accepted by the stack, until an error occurs or until Timeout milliseconds have elapsed, in which case Reason is timeout. The data may not have been received by the intended recipient, and may not even have been sent over the network. Transient send backpressure is retried internally, waiting for the socket to become writable again, as with send/2,3.

If Timeout is nowait or a select handle, this function never blocks and returns {select, SelectInfo} if the data cannot be sent right away, SelectInfo being {select_info, sendto, SelectHandle}; see send/3.

Example:

ok = socket:sendto(Socket, Data, Dest, 5000)

setopt/3


setopt(Socket::socket(), SocketOption::socket_option(), Value::term()) -> ok | {error, any()}

Socket: the socket
SocketOption: the option
Value: the option value

returns: ok if successful; {error, Reason}, otherwise.

Set a socket option.

Set an option on a socket.

Currently, the following options are supported:

{socket, reuseaddr}boolean()
{socket, linger}#{onoff => boolean(), linger => non_neg_integer()}
{otp, recvbuf}non_neg_integer()
{ip, add_membership}ip_mreq()

Example:

ok = socket:setopt(ListeningSocket, {socket, reuseaddr}, true) ok = socket:setopt(ListeningSocket, {socket, linger}, #{onoff => true, linger => 0})

shutdown/2


shutdown(Socket::socket(), How::read | write | read_write) -> ok | {error, Reason::term()}

Socket: the socket
How: how to shut the socket down

returns: ok if successful; {error, Reason}, otherwise.

Shut down one or both ends of a full-duplex socket connection.

Example:

ok = socket:shutdown(Socket, read_write)

sockname/1


sockname(Socket::socket()) -> {ok, Address::sockaddr()} | {error, Reason::term()}

Socket: the socket

returns: {ok, Address} if successful; {error, Reason}, otherwise.

Return the current address for the specified socket.

Example:

{ok, Address} = socket:sockname(ConnectedSocket)