socket
Module
Module Summary
Since
Description
This module provides an API for the socket interface. It is used to create, delete and manipulate sockets, send and receive data.
The idea is that it shall be as "close as possible" to the OS level socket interface. The only significant addition is that some of the functions, e.g. recv/3, has a timeout argument.
Data Types
As returned by open/2,3,4 and accept/1,2.
#{family := inet6,
port := port_number(),
addr := any | loopback | ip6_address(),
flowinfo := in6_flow_info(),
scope_id := in6_scope_id()}
acceptconn |
acceptfilter |
bindtodevice |
broadcast |
busy_poll |
debug |
domain |
dontroute |
error |
keepalive |
linger |
mark |
oobinline |
passcred |
peek_off |
peekcred |
priority |
protocol |
rcvbuf |
rcvbufforce |
rcvlowat |
rcvtimeo |
reuseaddr |
reuseport |
rxq_ovfl |
setfib |
sndbuf |
sndbufforce |
sndlowat |
sndtimeo |
timestamp |
type
add_membership |
add_source_membership |
block_source |
dontfrag |
drop_membership |
drop_source_membership |
freebind |
hdrincl |
minttl |
msfilter |
mtu |
mtu_discover |
multicast_all |
multicast_if |
multicast_loop |
multicast_ttl |
nodefrag |
options |
pktinfo |
recverr |
recvif |
recvdstaddr |
recvopts |
recvorigdstaddr |
recvtos |
recvttl |
retopts |
router_alert |
sndsrcaddr |
tos |
transparent |
ttl |
unblock_source
addrform |
add_membership |
authhdr |
auth_level |
checksum |
drop_membership |
dstopts |
esp_trans_level |
esp_network_level |
faith |
flowinfo |
hoplimit |
hopopts |
ipcomp_level |
join_group |
leave_group |
mtu |
mtu_discover |
multicast_hops |
multicast_if |
multicast_loop |
portrange |
pktoptions |
recverr |
recvpktinfo |
pktinfo |
recvtclass |
router_alert |
rthdr |
tclass |
unicast_hops |
use_min_mtu |
v6only
congestion |
cork |
info |
keepcnt |
keepidle |
keepintvl |
maxseg |
md5sig |
nodelay |
noopt |
nopush |
syncnt |
user_timeout
adaption_layer |
associnfo |
auth_active_key |
auth_asconf |
auth_chunk |
auth_key |
auth_delete_key |
autoclose |
context |
default_send_params |
delayed_ack_time |
disable_fragments |
hmac_ident |
events |
explicit_eor |
fragment_interleave |
get_peer_addr_info |
initmsg |
i_want_mapped_v4_addr |
local_auth_chunks |
maxseg |
maxburst |
nodelay |
partial_delivery_point |
peer_addr_params |
peer_auth_chunks |
primary_addr |
reset_streams |
rtoinfo |
set_peer_primary_addr |
status |
use_ext_recvinfo
#{multiaddr := ip4_address(),
interface := ip4_address(),
sourceaddr := ip4_address()}
#{multiaddr := ip4_address(),
interface := ip4_address(),
mode := ip_msfilter_mode(),
slist := [ip4_address()]}
#{stream := uint16(),
ssn := uint16(),
flags := uint16(),
ppid := uint16(),
context := uint16(),
timetolive := uint16(),
tsn := uint16(),
cumtsn := uint16(),
assoc_id := sctp_assoc_id()}
#{data_in := boolean(),
association := boolean(),
address := boolean(),
send_failure := boolean(),
peer_error := boolean(),
shutdown := boolean(),
partial_delivery := boolean(),
adaptation_layer := boolean(),
authentication := boolean(),
sender_dry := boolean()}
#{assoc_id := sctp_assoc_id(),
max_rxt := uint16(),
num_peer_dests := uint16(),
peer_rwnd := uint32(),
local_rwnd := uint32(),
cookie_life := uint32()}
#{num_outstreams := uint16(),
max_instreams := uint16(),
max_attempts := uint16(),
max_init_timeo := uint16()}
#{assoc_id := sctp_assoc_id(),
initial := uint32(),
max := uint32(),
min := uint32()}
#{addr := sockaddr(),
iov := [binary()],
ctrl := [cmsghdr_recv()] | [cmsghdr_send()],
flags := msghdr_flags()}
#{level := socket, type := timestamp, data := timeval()} |
#{level := socket, type := rights, data := binary()} |
#{level := socket, type := credentials, data := binary()} |
#{level := socket, type := integer(), data := binary()} |
#{level := ip, type := tos, data := ip_tos()} |
#{level := ip, type := ttl, data := integer()} |
#{level := ip, type := pktinfo, data := ip_pktinfo()} |
#{level := ip, type := origdstaddr, data := sockaddr_in4()} |
#{level := ip, type := integer(), data := binary()} |
#{level := ipv6, type := pktinfo, data := ipv6_pktinfo()} |
#{level := ipv6, type := integer(), data := binary()} |
#{level := integer(), type := integer(), data := binary()}
#{level := socket, type := integer(), data := binary()} |
#{level := ip, type := tos, data := ip_tos() | binary()} |
#{level := ip, type := ttl, data := integer() | binary()} |
#{level := ip, type := integer(), data := binary()} |
#{level := ipv6, type := integer(), data := binary()} |
#{level := udp, type := integer(), data := binary()} |
#{level := integer(), type := integer(), data := binary()}
[{socket, supports_options_socket()} |
{ip, supports_options_ip()} |
{ipv6, supports_options_ipv6()} |
{tcp, supports_options_tcp()} |
{udp, supports_options_udp()} |
{sctp, supports_options_sctp()}]
Exports
OTP @OTP-14831@ |
OTP @OTP-14831@ |
Accept a connection on a socket.
This call is used with connection-based socket types (stream or seqpacket). It extracs the first pending connection request for the listen socket and returns the (newly) connected socket.
OTP @OTP-14831@ |
Bind a name to a socket.
When a socket is created (with open), it has no address assigned to it. bind assigns the address specified by the Addr argument.
The rules used for name binding vary between domains.
OTP @OTP-14831@ |
Closes the socket.
Note that for e.g. protocol = tcp, most implementations doing a close does not guarantee that any data sent is delivered to the recipient before the close is detected at the remote side.
One way to handle this is to use the shutdown function (socket:shutdown(Socket, write)) to signal that no more data is to be sent and then wait for the read side of the socket to be closed.
OTP @OTP-14831@ |
OTP @OTP-14831@ |
This function connects the socket to the address specied by the SockAddr argument.
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
Get an option on a socket.
What properties are valid depend both on Level and on what kind of socket it is (domain, type and protocol).
See the socket options chapter of the users guide for more info.
Not all options are valid on all platforms. That is, even if "we" support an option, that does not mean that the underlying OS does.
OTP @OTP-14831@ |
Types
Get an option on a socket.
What properties are valid depend both on Level and on what kind of socket it is (domain, type and protocol).
When specifying Level as an integer, and therefor using "native mode", it is *currently* up to the caller to know how to interpret the result.
See the socket options chapter of the users guide for more info.
Not all options are valid on all platforms. That is, even if "we" support an option, that does not mean that the underlying OS does.
OTP @OTP-14831@ |
OTP @OTP-14831@ |
Listen for connections on a socket.
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
Types
Creates an endpoint (socket) for communication.
For some types there is a default protocol, which will be used if no protocol is specified:
stream: tcp
dgram: udp
seqpacket: sctp
The Extra argument is intended for "obscure" options. Currently the only supported option is netns, which is only supported on the linux platform.
OTP @OTP-14831@ |
Returns the address of the peer connected to the socket.
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
Types
Receive a message from a socket.
There is a special case for the argument Length. If it is set to zero (0), it means "give me everything you currently have".
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
Types
Receive a message from a socket.
This function reads "messages", which means that regardless of how much we want to read, it returns when we get a message.
The BufSz argument basically defines the size of the receive buffer. By setting the value to zero (0), the configured size (setopt with Level = otp and Key = rcvbuf) is used.
It may be impossible to know what (buffer) size is appropriate "in advance", and in those cases it may be convenient to use the (recv) 'peek' flag. When this flag is provided, the message is *not* "consumed" from the underlying buffers, so another recvfrom call is needed, possibly with a then adjusted buffer size.
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
Types
Receive a message from a socket.
This function reads "messages", which means that regardless of how much we want to read, it returns when we get a message.
The message will be delivered in the form of a msghdr(), which may contain the source address (if socket not connected), a list of cmsghdr_recv() (depends on what socket options have been set and what the protocol and platform supports) and also a set of flags, providing further info about the read.
The BufSz argument basically defines the size of the receive buffer. By setting the value to zero (0), the configured size (setopt with Level = otp and Key = rcvbuf) is used.
The CtrlSz argument basically defines the size of the receive buffer for the control messages. By setting the value to zero (0), the configured size (setopt with Level = otp) is used.
It may be impossible to know what (buffer) size is appropriate "in advance", and in those cases it may be convenient to use the (recv) 'peek' flag. When this flag is provided, the message is *not* "consumed" from the underlying buffers, so another recvmsg call is needed, possibly with a then adjusted buffer size.
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
Send a message on a connected socket.
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
Types
Send a message on a socket. The destination, if needed (socket not connected) is provided in the MsgHdr, which also contains the message to send, The MsgHdr may also contain an list of optional cmsghdr_send() (depends on what the protocol and platform supports).
Unlike the send function, this one sends one message. This means that if, for whatever reason, its not possible to send the message in one go, the function will instead return with the remaining data ({ok, Remaining}). Thereby leaving it up to the caller to decide what to do (retry with the remaining data of give up).
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
Types
Send a message on a socket, to the specified destination.
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
Set options on a socket.
What properties are valid depend both on Level and on what kind of socket it is (domain, type and protocol).
See the socket options chapter of the users guide for more info.
Not all options are valid on all platforms. That is, even if "we" support an option, that does not mean that the underlying OS does.
Sockets are set 'non-blocking' when created, so this option is *not* available (as it would adversely effect the Erlang VM to set a socket 'blocking').
OTP @OTP-14831@ |
Set options on a socket.
What properties are valid depend both on Level and on what kind of socket it is (domain, type and protocol).
See the socket options chapter of the users guide for more info.
Not all options are valid on all platforms. That is, even if "we" support an option, that does not mean that the underlying OS does.
Sockets are set 'non-blocking' when created, so this option is *not* available (as it would adversely effect the Erlang VM to set a socket 'blocking').
OTP @OTP-14831@ |
Shut down all or part of a full-duplex connection.
OTP @OTP-14831@ |
Returns the current address to which the socket is bound.
OTP @OTP-14831@ |
supports(Key1 :: options) -> supports_options()
|
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
supports(Key1 :: options, Level :: ip) -> supports_options_ip()
|
OTP @OTP-14831@ |
OTP @OTP-14831@ |
supports(Key1 :: options, Level :: tcp) -> supports_options_tcp()
|
OTP @OTP-14831@ |
supports(Key1 :: options, Level :: udp) -> supports_options_udp()
|
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
OTP @OTP-14831@ |
Types
This function intends to retreive information about what the platform supports. Such as if SCTP is supported. Or which socket options are supported.
Examples
client(Addr, SAddr, SPort) -> {ok, Sock} = socket:open(inet, stream, tcp), {ok, _} = socket:bind(Sock, #{family => inet, addr => Addr}), ok = socket:connect(Sock, #{family => inet, addr => SAddr, port => SPort}), Msg = list_to_binary("hello"), ok = socket:send(Sock, Msg), ok = socket:shutdown(Sock, write), {ok, Msg} = socket:recv(Sock), ok = socket:close(Sock). server(Addr, Port) -> {ok, LSock} = socket:open(inet, stream, tcp), {ok, _} = socket:bind(LSock, #{family => inet, port => Port, addr => Addr}), ok = socket:listen(LSock), {ok, Sock} = socket:accept(LSock), {ok, Msg} = socket:recv(Sock), ok = socket:send(Sock, Msg), ok = socket:shutdown(Sock, write), ok = socket:close(Sock), ok = socket:close(LSock).