Erlang -- socket

Erlang Run-Time System Application (ERTS)

Reference Manual

Version 10.7

- User's Guide
- Reference Manual
- Internal Documentation
- Release Notes
- PDF
- Top

- Expand All
- Contract All

### Table of Contents

- atomics
  - Top of manual page
  - add/3
  - add_get/3
  - compare_exchange/4
  - exchange/3
  - get/2
  - info/1
  - new/2
  - put/3
  - sub/3
  - sub_get/3
- counters
  - Top of manual page
  - add/3
  - get/2
  - info/1
  - new/2
  - put/3
  - sub/3
- driver_entry
- epmd
- erl
- erlang
  - Top of manual page
  - abs/1
  - adler32/1
  - adler32/2
  - adler32_combine/3
  - append_element/2
  - apply/2
  - apply/3
  - atom_to_binary/2
  - atom_to_list/1
  - binary_part/2
  - binary_part/3
  - binary_to_atom/2
  - binary_to_existing_atom/2
  - binary_to_float/1
  - binary_to_integer/1
  - binary_to_integer/2
  - binary_to_list/1
  - binary_to_list/3
  - binary_to_term/1
  - binary_to_term/2
  - bit_size/1
  - bitstring_to_list/1
  - bump_reductions/1
  - byte_size/1
  - cancel_timer/1
  - cancel_timer/2
  - ceil/1
  - check_old_code/1
  - check_process_code/2
  - check_process_code/3
  - convert_time_unit/3
  - crc32/1
  - crc32/2
  - crc32_combine/3
  - date/0
  - decode_packet/3
  - delete_element/2
  - delete_module/1
  - demonitor/1
  - demonitor/2
  - disconnect_node/1
  - display/1
  - dist_ctrl_get_data/1
  - dist_ctrl_get_data_notification/1
  - dist_ctrl_get_opt/2
  - dist_ctrl_input_handler/2
  - dist_ctrl_put_data/2
  - dist_ctrl_set_opt/3
  - element/2
  - erase/0
  - erase/1
  - error/1
  - error/2
  - exit/1
  - exit/2
  - external_size/1
  - external_size/2
  - float/1
  - float_to_binary/1
  - float_to_binary/2
  - float_to_list/1
  - float_to_list/2
  - floor/1
  - fun_info/1
  - fun_info/2
  - fun_to_list/1
  - function_exported/3
  - garbage_collect/0
  - garbage_collect/1
  - garbage_collect/2
  - get/0
  - get/1
  - get_cookie/0
  - get_keys/0
  - get_keys/1
  - get_stacktrace/0
  - group_leader/0
  - group_leader/2
  - halt/0
  - halt/1
  - halt/2
  - hd/1
  - hibernate/3
  - insert_element/3
  - integer_to_binary/1
  - integer_to_binary/2
  - integer_to_list/1
  - integer_to_list/2
  - iolist_size/1
  - iolist_to_binary/1
  - iolist_to_iovec/1
  - is_alive/0
  - is_atom/1
  - is_binary/1
  - is_bitstring/1
  - is_boolean/1
  - is_builtin/3
  - is_float/1
  - is_function/1
  - is_function/2
  - is_integer/1
  - is_list/1
  - is_map/1
  - is_map_key/2
  - is_number/1
  - is_pid/1
  - is_port/1
  - is_process_alive/1
  - is_record/2
  - is_record/3
  - is_reference/1
  - is_tuple/1
  - length/1
  - link/1
  - list_to_atom/1
  - list_to_binary/1
  - list_to_bitstring/1
  - list_to_existing_atom/1
  - list_to_float/1
  - list_to_integer/1
  - list_to_integer/2
  - list_to_pid/1
  - list_to_port/1
  - list_to_ref/1
  - list_to_tuple/1
  - load_module/2
  - load_nif/2
  - loaded/0
  - localtime/0
  - localtime_to_universaltime/1
  - localtime_to_universaltime/2
  - make_ref/0
  - make_tuple/2
  - make_tuple/3
  - map_get/2
  - map_size/1
  - match_spec_test/3
  - max/2
  - md5/1
  - md5_final/1
  - md5_init/0
  - md5_update/2
  - memory/0
  - memory/1
  - min/2
  - module_loaded/1
  - monitor/2
  - monitor_node/2
  - monitor_node/3
  - monotonic_time/0
  - monotonic_time/1
  - nif_error/1
  - nif_error/2
  - node/0
  - node/1
  - nodes/0
  - nodes/1
  - now/0
  - open_port/2
  - phash/2
  - phash2/1
  - phash2/2
  - pid_to_list/1
  - port_call/3
  - port_close/1
  - port_command/2
  - port_command/3
  - port_connect/2
  - port_control/3
  - port_info/1
  - port_info/2
  - port_to_list/1
  - ports/0
  - pre_loaded/0
  - process_display/2
  - process_flag/2
  - process_flag/3
  - process_info/1
  - process_info/2
  - processes/0
  - purge_module/1
  - put/2
  - raise/3
  - read_timer/1
  - read_timer/2
  - ref_to_list/1
  - register/2
  - registered/0
  - resume_process/1
  - round/1
  - self/0
  - send/2
  - send/3
  - send_after/3
  - send_after/4
  - send_nosuspend/2
  - send_nosuspend/3
  - set_cookie/2
  - setelement/3
  - size/1
  - spawn/1
  - spawn/2
  - spawn/3
  - spawn/4
  - spawn_link/1
  - spawn_link/2
  - spawn_link/3
  - spawn_link/4
  - spawn_monitor/1
  - spawn_monitor/3
  - spawn_opt/2
  - spawn_opt/3
  - spawn_opt/4
  - spawn_opt/5
  - split_binary/2
  - start_timer/3
  - start_timer/4
  - statistics/1
  - suspend_process/1
  - suspend_process/2
  - system_flag/2
  - system_info/1
  - system_monitor/0
  - system_monitor/1
  - system_monitor/2
  - system_profile/0
  - system_profile/2
  - system_time/0
  - system_time/1
  - term_to_binary/1
  - term_to_binary/2
  - throw/1
  - time/0
  - time_offset/0
  - time_offset/1
  - timestamp/0
  - tl/1
  - trace/3
  - trace_delivered/1
  - trace_info/2
  - trace_pattern/2
  - trace_pattern/3
  - trunc/1
  - tuple_size/1
  - tuple_to_list/1
  - unique_integer/0
  - unique_integer/1
  - universaltime/0
  - universaltime_to_localtime/1
  - unlink/1
  - unregister/1
  - whereis/1
  - yield/0
- erlc
- erl_driver
  - Top of manual page
  - add_driver_entry()
  - driver_alloc()
  - driver_alloc_binary()
  - driver_async()
  - driver_async_port_key()
  - driver_binary_dec_refc()
  - driver_binary_get_refc()
  - driver_binary_inc_refc()
  - driver_caller()
  - driver_cancel_timer()
  - driver_compare_monitors()
  - driver_connected()
  - driver_create_port()
  - driver_demonitor_process()
  - driver_deq()
  - driver_enq()
  - driver_enq_bin()
  - driver_enqv()
  - driver_failure()
  - driver_failure_atom()
  - driver_failure_eof()
  - driver_failure_posix()
  - driver_free()
  - driver_free_binary()
  - driver_get_monitored_process()
  - driver_get_now()
  - driver_lock_driver()
  - driver_mk_atom()
  - driver_mk_port()
  - driver_monitor_process()
  - driver_output()
  - driver_output2()
  - driver_output_binary()
  - driver_output_term()
  - driver_outputv()
  - driver_pdl_create()
  - driver_pdl_dec_refc()
  - driver_pdl_get_refc()
  - driver_pdl_inc_refc()
  - driver_pdl_lock()
  - driver_pdl_unlock()
  - driver_peekq()
  - driver_peekqv()
  - driver_pushq()
  - driver_pushq_bin()
  - driver_pushqv()
  - driver_read_timer()
  - driver_realloc()
  - driver_realloc_binary()
  - driver_select()
  - driver_send_term()
  - driver_set_timer()
  - driver_sizeq()
  - driver_system_info()
  - driver_vec_to_buf()
  - erl_drv_busy_msgq_limits()
  - erl_drv_cond_broadcast()
  - erl_drv_cond_create()
  - erl_drv_cond_destroy()
  - erl_drv_cond_name()
  - erl_drv_cond_signal()
  - erl_drv_cond_wait()
  - erl_drv_consume_timeslice()
  - erl_drv_convert_time_unit()
  - erl_drv_equal_tids()
  - erl_drv_getenv()
  - erl_drv_init_ack()
  - erl_drv_monotonic_time()
  - erl_drv_mutex_create()
  - erl_drv_mutex_destroy()
  - erl_drv_mutex_lock()
  - erl_drv_mutex_name()
  - erl_drv_mutex_trylock()
  - erl_drv_mutex_unlock()
  - erl_drv_output_term()
  - erl_drv_putenv()
  - erl_drv_rwlock_create()
  - erl_drv_rwlock_destroy()
  - erl_drv_rwlock_name()
  - erl_drv_rwlock_rlock()
  - erl_drv_rwlock_runlock()
  - erl_drv_rwlock_rwlock()
  - erl_drv_rwlock_rwunlock()
  - erl_drv_rwlock_tryrlock()
  - erl_drv_rwlock_tryrwlock()
  - erl_drv_send_term()
  - erl_drv_set_os_pid()
  - erl_drv_thread_create()
  - erl_drv_thread_exit()
  - erl_drv_thread_join()
  - erl_drv_thread_name()
  - erl_drv_thread_opts_create()
  - erl_drv_thread_opts_destroy()
  - erl_drv_thread_self()
  - erl_drv_time_offset()
  - erl_drv_tsd_get()
  - erl_drv_tsd_key_create()
  - erl_drv_tsd_key_destroy()
  - erl_drv_tsd_set()
  - erl_errno_id()
  - remove_driver_entry()
  - set_busy_port()
  - set_port_control_flags()
- erl_nif
  - Top of manual page
  - enif_alloc()
  - enif_alloc_binary()
  - enif_alloc_env()
  - enif_alloc_resource()
  - enif_binary_to_term()
  - enif_clear_env()
  - enif_compare()
  - enif_compare_monitors()
  - enif_compare_pids()
  - enif_cond_broadcast()
  - enif_cond_create()
  - enif_cond_destroy()
  - enif_cond_name()
  - enif_cond_signal()
  - enif_cond_wait()
  - enif_consume_timeslice()
  - enif_convert_time_unit()
  - enif_cpu_time()
  - enif_demonitor_process()
  - enif_equal_tids()
  - enif_fprintf()
  - enif_free()
  - enif_free_env()
  - enif_free_iovec()
  - enif_get_atom()
  - enif_get_atom_length()
  - enif_get_double()
  - enif_get_int()
  - enif_get_int64()
  - enif_get_list_cell()
  - enif_get_list_length()
  - enif_get_local_pid()
  - enif_get_local_port()
  - enif_get_long()
  - enif_get_map_size()
  - enif_get_map_value()
  - enif_get_resource()
  - enif_get_string()
  - enif_get_tuple()
  - enif_get_uint()
  - enif_get_uint64()
  - enif_get_ulong()
  - enif_getenv()
  - enif_has_pending_exception()
  - enif_hash()
  - enif_inspect_binary()
  - enif_inspect_iolist_as_binary()
  - enif_inspect_iovec()
  - enif_ioq_create()
  - enif_ioq_deq()
  - enif_ioq_destroy()
  - enif_ioq_enq_binary()
  - enif_ioq_enqv()
  - enif_ioq_peek()
  - enif_ioq_peek_head()
  - enif_ioq_size()
  - enif_is_atom()
  - enif_is_binary()
  - enif_is_current_process_alive()
  - enif_is_empty_list()
  - enif_is_exception()
  - enif_is_fun()
  - enif_is_identical()
  - enif_is_list()
  - enif_is_map()
  - enif_is_number()
  - enif_is_pid()
  - enif_is_pid_undefined()
  - enif_is_port()
  - enif_is_port_alive()
  - enif_is_process_alive()
  - enif_is_ref()
  - enif_is_tuple()
  - enif_keep_resource()
  - enif_make_atom()
  - enif_make_atom_len()
  - enif_make_badarg()
  - enif_make_binary()
  - enif_make_copy()
  - enif_make_double()
  - enif_make_existing_atom()
  - enif_make_existing_atom_len()
  - enif_make_int()
  - enif_make_int64()
  - enif_make_list()
  - enif_make_list1()
  - enif_make_list2()
  - enif_make_list3()
  - enif_make_list4()
  - enif_make_list5()
  - enif_make_list6()
  - enif_make_list7()
  - enif_make_list8()
  - enif_make_list9()
  - enif_make_list_cell()
  - enif_make_list_from_array()
  - enif_make_long()
  - enif_make_map_from_arrays()
  - enif_make_map_put()
  - enif_make_map_remove()
  - enif_make_map_update()
  - enif_make_monitor_term()
  - enif_make_new_binary()
  - enif_make_new_map()
  - enif_make_pid()
  - enif_make_ref()
  - enif_make_resource()
  - enif_make_resource_binary()
  - enif_make_reverse_list()
  - enif_make_string()
  - enif_make_string_len()
  - enif_make_sub_binary()
  - enif_make_tuple()
  - enif_make_tuple1()
  - enif_make_tuple2()
  - enif_make_tuple3()
  - enif_make_tuple4()
  - enif_make_tuple5()
  - enif_make_tuple6()
  - enif_make_tuple7()
  - enif_make_tuple8()
  - enif_make_tuple9()
  - enif_make_tuple_from_array()
  - enif_make_uint()
  - enif_make_uint64()
  - enif_make_ulong()
  - enif_make_unique_integer()
  - enif_map_iterator_create()
  - enif_map_iterator_destroy()
  - enif_map_iterator_get_pair()
  - enif_map_iterator_is_head()
  - enif_map_iterator_is_tail()
  - enif_map_iterator_next()
  - enif_map_iterator_prev()
  - enif_monitor_process()
  - enif_monotonic_time()
  - enif_mutex_create()
  - enif_mutex_destroy()
  - enif_mutex_lock()
  - enif_mutex_name()
  - enif_mutex_trylock()
  - enif_mutex_unlock()
  - enif_now_time()
  - enif_open_resource_type()
  - enif_open_resource_type_x()
  - enif_port_command()
  - enif_priv_data()
  - enif_raise_exception()
  - enif_realloc()
  - enif_realloc_binary()
  - enif_release_binary()
  - enif_release_resource()
  - enif_rwlock_create()
  - enif_rwlock_destroy()
  - enif_rwlock_name()
  - enif_rwlock_rlock()
  - enif_rwlock_runlock()
  - enif_rwlock_rwlock()
  - enif_rwlock_rwunlock()
  - enif_rwlock_tryrlock()
  - enif_rwlock_tryrwlock()
  - enif_schedule_nif()
  - enif_select()
  - enif_select_read()
  - enif_select_write()
  - enif_self()
  - enif_send()
  - enif_set_pid_undefined()
  - enif_sizeof_resource()
  - enif_snprintf()
  - enif_system_info()
  - enif_term_to_binary()
  - enif_term_type()
  - enif_thread_create()
  - enif_thread_exit()
  - enif_thread_join()
  - enif_thread_name()
  - enif_thread_opts_create()
  - enif_thread_opts_destroy()
  - enif_thread_self()
  - enif_thread_type()
  - enif_time_offset()
  - enif_tsd_get()
  - enif_tsd_key_create()
  - enif_tsd_key_destroy()
  - enif_tsd_set()
  - enif_vfprintf()
  - enif_vsnprintf()
  - enif_whereis_pid()
  - enif_whereis_port()
- erl_prim_loader
  - Top of manual page
  - get_file/1
  - get_path/0
  - list_dir/1
  - read_file_info/1
  - read_link_info/1
  - set_path/1
- erlsrv
- erl_tracer
  - Top of manual page
  - Module:enabled/3
  - Module:enabled_call/3
  - Module:enabled_garbage_collection/3
  - Module:enabled_ports/3
  - Module:enabled_procs/3
  - Module:enabled_receive/3
  - Module:enabled_running_ports/3
  - Module:enabled_running_procs/3
  - Module:enabled_send/3
  - Module:trace/5
  - Module:trace_call/5
  - Module:trace_garbage_collection/5
  - Module:trace_ports/5
  - Module:trace_procs/5
  - Module:trace_receive/5
  - Module:trace_running_ports/5
  - Module:trace_running_procs/5
  - Module:trace_send/5
- erts_alloc
- escript
- init
  - Top of manual page
  - boot/1
  - get_argument/1
  - get_arguments/0
  - get_plain_arguments/0
  - get_status/0
  - reboot/0
  - restart/0
  - script_id/0
  - stop/0
  - stop/1
- persistent_term
  - Top of manual page
  - erase/1
  - get/0
  - get/1
  - get/2
  - info/0
  - put/2
- run_erl
- socket
  - Top of manual page
  - accept/1
  - accept/2
  - bind/2
  - cancel/2
  - close/1
  - connect/2
  - connect/3
  - getopt/3
  - info/1
  - listen/1
  - listen/2
  - number_of/0
  - open/2
  - open/3
  - open/4
  - peername/1
  - recv/1
  - recv/2
  - recv/3
  - recv/4
  - recvfrom/1
  - recvfrom/2
  - recvfrom/3
  - recvfrom/4
  - recvmsg/1
  - recvmsg/2
  - recvmsg/3
  - recvmsg/5
  - send/2
  - send/3
  - send/4
  - sendmsg/2
  - sendmsg/3
  - sendmsg/4
  - sendto/3
  - sendto/4
  - sendto/5
  - setopt/4
  - shutdown/2
  - sockname/1
  - supports/0
  - supports/1
  - supports/2
  - supports/3
  - which_sockets/0
  - which_sockets/1
- start
- start_erl
- werl
- zlib
  - Top of manual page
  - adler32/2
  - adler32/3
  - adler32_combine/4
  - close/1
  - compress/1
  - crc32/1
  - crc32/2
  - crc32/3
  - crc32_combine/4
  - deflate/2
  - deflate/3
  - deflateEnd/1
  - deflateInit/1
  - deflateInit/2
  - deflateInit/6
  - deflateParams/3
  - deflateReset/1
  - deflateSetDictionary/2
  - getBufSize/1
  - gunzip/1
  - gzip/1
  - inflate/2
  - inflate/3
  - inflateChunk/1
  - inflateChunk/2
  - inflateEnd/1
  - inflateGetDictionary/1
  - inflateInit/1
  - inflateInit/2
  - inflateReset/1
  - inflateSetDictionary/2
  - open/0
  - safeInflate/2
  - setBufSize/2
  - set_controlling_process/2
  - uncompress/1
  - unzip/1
  - zip/1

# socket

### Module

socket

### Module Summary

Socket interface.

### Since

Module socket was introduced in OTP 22.0.

### Description

This module provides an API for network socket. Functions are provided to create, delete and manupilate the sockets aswell as sending and reciving data on them.

The intent 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.

Note

Some functions allow for an asynchronous call. This is achieved by setting the Timeout argument to nowait. For instance, if calling the recv/3 function with Timeout set to nowait (recv(Sock, 0, nowait)) when there is actually nothing to read, it will return with {select, SelectInfo} (SelectInfo contains the SelectRef). When data eventually arrives a 'select' message will be sent to the caller:

  {'$socket', socket(), select, SelectRef}

The caller can now make another call to the recv function and now expect data.

Note that all other users are locked out until the 'current user' has called the function (recv in this case).

Another message the user must be prepared for (when making asynchronous calls) is the abort message:

  {'$socket', socket(), abort, Info}

This message indicates that the (asynchronous) operation has been aborted. If, for instance, the socket has been closed (by another process), Info will be {SelectRef, closed}.

Note

There is currently no support for Windows.

Support for IPv6 has been implemented but not tested.

SCTP has only been partly implemented (and not tested).

### Data Types

domain() = local | inet | inet6
type() = stream | dgram | raw | rdm | seqpacket
protocol() =
ip | tcp | udp | sctp | icmp | igmp | {raw, integer()}
socket()

As returned by open/2,3,4 and accept/1,2.

select_tag()

A tag that describes the (select) operation.

select_ref()

A reference that uniquely identifies the (select) operation.

select_info() = {select_info, select_tag(), select_ref()}
socket_counters() = [{socket_counter(), integer() >= 0}]
socket_counter() =
read_byte | read_fails | read_pkg | read_pkg_max |
read_tries | read_waits | write_byte | write_fails |
write_pkg | write_pkg_max | write_tries | write_waits |
acc_success | acc_fails | acc_tries | acc_waits
socket_info() =
#{domain := domain(),
type := type(),
protocol := protocol(),
ctrl := pid(),
counters := socket_counters(),
num_readers := integer() >= 0,
num_writers := integer() >= 0,
num_acceptors := integer() >= 0,
writable := boolean(),
readable := boolean()}
ip4_address() = {0..255, 0..255, 0..255, 0..255}
ip6_address() =
{0..65535,
0..65535,
0..65535,
0..65535,
0..65535,
0..65535,
0..65535,
0..65535}
ip_address() = ip4_address() | ip6_address()
sockaddr() =
sockaddr_in4() |
sockaddr_in6() |
sockaddr_un() |
sockaddr_ll()
sockaddr_in4() =
#{family := inet,
port := port_number(),
addr := any | broadcast | loopback | ip4_address()}
sockaddr_in6() =
#{family := inet6,
port := port_number(),
addr := any | loopback | ip6_address(),
flowinfo := in6_flow_info(),
scope_id := in6_scope_id()}
sockaddr_un() = #{family := local, path := binary() | string()}
sockaddr_ll() =
#{family := packet,
protocol := integer() >= 0,
ifindex := integer(),
pkttype := packet_type(),
hatype := integer() >= 0,
addr := binary()}
packet_type() =
host | broadcast | multicast | otherhost | outgoing |
loopback | user | kernel | fastroute |
integer() >= 0
port_number() = 0..65535
in6_flow_info() = uint20()
in6_scope_id() = uint32()
send_flags() = [send_flag()]
send_flag() = confirm | dontroute | eor | more | nosignal | oob
recv_flags() = [recv_flag()]
recv_flag() = cmsg_cloexec | errqueue | oob | peek | trunc
shutdown_how() = read | write | read_write
sockopt_level() =
otp | socket | ip | ipv6 | tcp | udp | sctp |
integer() >= 0
otp_socket_option() =
debug | iow | controlling_process | rcvbuf | rcvctrlbuf |
sndctrlbuf | fd
socket_option() =
acceptconn | acceptfilter | bindtodevice | broadcast |
busy_poll | debug | domain | dontroute | error | keepalive |
linger | mark | oobinline | passcred | peek_off | peercred |
priority | protocol | rcvbuf | rcvbufforce | rcvlowat |
rcvtimeo | reuseaddr | reuseport | rxq_ovfl | setfib |
sndbuf | sndbufforce | sndlowat | sndtimeo | timestamp | type
ip_socket_option() =
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
ipv6_socket_option() =
addrform | add_membership | authhdr | auth_level | checksum |
drop_membership | dstopts | esp_trans_level |
esp_network_level | faith | flowinfo | hopopts |
ipcomp_level | join_group | leave_group | mtu | mtu_discover |
multicast_hops | multicast_if | multicast_loop | portrange |
pktoptions | recverr | recvhoplimit | hoplimit | recvpktinfo |
pktinfo | recvtclass | router_alert | rthdr | tclass |
unicast_hops | use_min_mtu | v6only
tcp_socket_option() =
congestion | cork | info | keepcnt | keepidle | keepintvl |
maxseg | md5sig | nodelay | noopt | nopush | syncnt |
user_timeout
udp_socket_option() = cork
sctp_socket_option() =
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
timeval() = #{sec := integer(), usec := integer()}
ip_tos() =
lowdelay | throughput | reliability | mincost | integer()
ip_mreq() =
#{multiaddr := ip4_address(),
interface := any | ip4_address()}
ip_mreq_source() =
#{multiaddr := ip4_address(),
interface := ip4_address(),
sourceaddr := ip4_address()}
ip_pmtudisc() = want | dont | do | probe
ip_msfilter_mode() = include | exclude
ip_msfilter() =
#{multiaddr := ip4_address(),
interface := ip4_address(),
mode := ip_msfilter_mode(),
slist := [ip4_address()]}
ip_pktinfo() =
#{ifindex := integer() >= 0,
spec_dst := ip4_address(),
addr := ip4_address()}
ipv6_mreq() =
#{multiaddr := ip6_address(), interface := integer() >= 0}
ipv6_pmtudisc() = ip_pmtudisc()
ipv6_pktinfo() = #{addr := ip6_address(), ifindex := integer()}
sctp_assoc_id() = int32()
sctp_sndrcvinfo() =
#{stream := uint16(),
ssn := uint16(),
flags := uint16(),
ppid := uint16(),
context := uint16(),
timetolive := uint16(),
tsn := uint16(),
cumtsn := uint16(),
assoc_id := sctp_assoc_id()}
sctp_event_subscribe() =
#{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()}
sctp_assocparams() =
#{assoc_id := sctp_assoc_id(),
max_rxt := uint16(),
num_peer_dests := uint16(),
peer_rwnd := uint32(),
local_rwnd := uint32(),
cookie_life := uint32()}
sctp_initmsg() =
#{num_outstreams := uint16(),
max_instreams := uint16(),
max_attempts := uint16(),
max_init_timeo := uint16()}
sctp_rtoinfo() =
#{assoc_id := sctp_assoc_id(),
initial := uint32(),
max := uint32(),
min := uint32()}
msghdr_flag() = ctrunc | eor | errqueue | oob | trunc
msghdr_flags() = [msghdr_flag()]
msghdr() =
#{addr := sockaddr(),
iov := [binary()],
ctrl := [cmsghdr_recv()] | [cmsghdr_send()],
flags := msghdr_flags()}
cmsghdr_level() = socket | ip | ipv6 | integer()
cmsghdr_type() =
credentials | hoplevel | origdstaddr | pktinfo | recvtos |
rights | timestamp | tos | ttl |
integer()
cmsghdr_recv() =
#{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 := recvtos, data := ip_tos()} |
#{level := ip, type := ttl, data := integer()} |
#{level := ip, type := recvttl, data := integer()} |
#{level := ip, type := pktinfo, data := ip_pktinfo()} |
#{level := ip, type := origdstaddr, data := sockaddr_in4()} |
#{level := ip,
type := recverr,
data := extended_err() | binary()} |
#{level := ip, type := integer(), data := binary()} |
#{level := ipv6, type := hoplevel, data := integer()} |
#{level := ipv6, type := pktinfo, data := ipv6_pktinfo()} |
#{level := ipv6,
type := recverr,
data := extended_err() | binary()} |
#{level := ipv6, type := tclass, data := integer()} |
#{level := ipv6, type := integer(), data := binary()} |
#{level := integer(), type := integer(), data := binary()}
cmsghdr_send() =
#{level := socket, type := timestamp, data := binary()} |
#{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() | binary()} |
#{level := ip, type := ttl, data := integer() | binary()} |
#{level := ip, type := integer(), data := binary()} |
#{level := ipv6, type := tclass, data := integer()} |
#{level := ipv6, type := integer(), data := binary()} |
#{level := udp, type := integer(), data := binary()} |
#{level := integer(), type := integer(), data := binary()}
icmp_dest_unreach() =
net_unreach | host_unreach | port_unreach | frag_needed |
net_unknown | host_unknown |
uint8()
icmpv6_dest_unreach() =
noroute | adm_prohibited | not_neighbour | addr_unreach |
port_unreach | policy_fail | reject_route |
uint8()
ee_origin() = none | local | icmp | icmp6 | uint8()
extended_err() =
#{error := term(),
origin := icmp,
type := dest_unreach,
code := icmp_dest_unreach(),
info := uint32(),
data := uint32(),
offender := undefined | sockaddr()} |
#{error := term(),
origin := icmp,
type := time_exceeded | uint8(),
code := uint8(),
info := uint32(),
data := uint32(),
offender := undefined | sockaddr()} |
#{error := term(),
origin := icmp6,
type := dest_unreach,
code := icmpv6_dest_unreach(),
info := uint32(),
data := uint32(),
offender := undefined | sockaddr()} |
#{error := term(),
origin := icmp6,
type := pkt_toobig | time_exceeded | uint8(),
code := uint8(),
info := uint32(),
data := uint32(),
offender := undefined | sockaddr()} |
#{error := term(),
origin := ee_origin(),
type := uint8(),
code := uint8(),
info := uint32(),
data := uint32(),
offender := undefined | sockaddr()}
uint8() = 0..255
uint16() = 0..65535
uint20() = 0..1048575
uint32() = 0..4294967295
int32() = -2147483648..2147483647
supports_options_socket() = [{socket_option(), boolean()}]
supports_options_ip() = [{ip_socket_option(), boolean()}]
supports_options_ipv6() = [{ipv6_socket_option(), boolean()}]
supports_options_tcp() = [{tcp_socket_option(), boolean()}]
supports_options_udp() = [{udp_socket_option(), boolean()}]
supports_options_sctp() = [{sctp_socket_option(), boolean()}]
supports_options() =
[{socket, supports_options_socket()} |
{ip, supports_options_ip()} |
{ipv6, supports_options_ipv6()} |
{tcp, supports_options_tcp()} |
{udp, supports_options_udp()} |
{sctp, supports_options_sctp()}]
supports_send_flags() = [{send_flag(), boolean()}]
supports_recv_flags() = [{recv_flag(), boolean()}]

### Exports

accept(LSocket) -> {ok, Socket} | {error, Reason} | OTP 22.0

accept(LSocket, Timeout) -> {ok, Socket} | {error, Reason} | OTP 22.0

### Types

LSocket = socket()
Timeout = timeout()
Socket = socket()
Reason = term()

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.

accept(LSocket, Timeout :: nowait) -> {ok, Socket} | {select, SelectInfo} | {error, Reason} | OTP 22.1

### Types

LSocket = Socket = socket()
SelectInfo = select_info()
Reason = term()

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.

In the case when there is no connections waiting, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo), when a client connects (a subsequent call to accept will then return the socket).

bind(Socket, Addr) -> {ok, Port} | {error, Reason} | OTP 22.0

### Types

Socket = socket()
Addr = any | broadcast | loopback | sockaddr()
Port = port_number()
Reason = term()

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.

cancel(Socket, SelectInfo) -> ok | {error, Reason} | OTP 22.1

### Types

Socket = socket()
SelectInfo = select_info()
Reason = term()

Cancel an asynchronous request.

Call this function in order to cancel a previous asynchronous call to, e.g. recv/3.

close(Socket) -> ok | {error, Reason} | OTP 22.0

### Types

Socket = socket()
Reason = term()

Closes the socket.

Note

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.

connect(Socket, SockAddr) -> ok | {error, Reason} | OTP 22.0

connect(Socket, SockAddr, Timeout) -> ok | {error, Reason} | OTP 22.0

### Types

Socket = socket()
SockAddr = sockaddr()
Timeout = timeout()
Reason = term()

This function connects the socket to the address specied by the SockAddr argument.

connect(Socket, SockAddr, Timeout :: nowait) -> ok | {select, SelectInfo} | {error, Reason} | OTP 22.1

### Types

Socket = socket()
SockAddr = sockaddr()
SelectInfo = select_info()
Reason = term()

This function connects the socket to the address specied by the SockAddr argument.

In the case when its not possible to immediately establish a connection, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo, a subsequent call to connect will then establish the connection).

getopt(Socket, Level :: otp, Key :: otp_socket_option()) -> {ok, Value} | {error, Reason} | OTP 22.0

getopt(Socket, Level :: socket, Key :: socket_option()) -> {ok, Value} | {error, Reason} | OTP 22.0

getopt(Socket, Level :: ip, Key :: ip_socket_option()) -> {ok, Value} | {error, Reason} | OTP 22.0

getopt(Socket, Level :: ipv6, Key :: ipv6_socket_option()) -> {ok, Value} | {error, Reason} | OTP 22.0

getopt(Socket, Level :: tcp, Key :: tcp_socket_option()) -> {ok, Value} | {error, Reason} | OTP 22.0

getopt(Socket, Level :: udp, Key :: udp_socket_option()) -> {ok, Value} | {error, Reason} | OTP 22.0

getopt(Socket, Level :: sctp, Key :: sctp_socket_option()) -> {ok, Value} | {error, Reason} | OTP 22.0

### Types

Socket = socket()
Value = Reason = term()

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.

Note

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.

getopt(Socket, Level, Key) -> ok | {ok, Value} | {error, Reason} | OTP 22.0

### Types

Socket = socket()
Level = integer()
Key = {NativeOpt, ValueSize}
NativeOpt = integer()
ValueSize = int | bool | integer() >= 0
Value = Reason = term()

Get an option on a socket.

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.

For more info, see getopt above.

info(Socket) -> socket_info() | OTP 22.1

### Types

Socket = socket()

Get miscellaneous info about the socket.

The function returns a map with each info item as a key-value binding. It reflects the "current" state of the socket.

Note

In order to ensure data integrity, mutex'es are taken when needed. So, do not call this function often.

listen(Socket) -> ok | {error, Reason} | OTP 22.0

listen(Socket, Backlog) -> ok | {error, Reason} | OTP 22.0

### Types

Socket = socket()
Backlog = integer() >= 1
Reason = term()

Listen for connections on a socket.

number_of() -> integer() >= 0 | OTP 22.3

Returns the number of active sockets.

open(Domain, Type) -> {ok, Socket} | {error, Reason} | OTP 22.0

open(Domain, Type, Protocol) -> {ok, Socket} | {error, Reason} | OTP 22.0

open(Domain, Type, Protocol, Extra) -> {ok, Socket} | {error, Reason} | OTP 22.0

### Types

Domain = domain()
Type = type()
Protocol = default | protocol()
Extra = map()
Socket = socket()
Reason = term()

Creates an endpoint (socket) for communication.

For some types there is a default protocol, indicated by default, which it may be possible to specify. And for Domain = local, if a protocol is pecified, it must be default.

The Extra argument is intended for "obscure" options. Currently the only supported option is netns, which is only supported on the linux platform.

Note

It may not be possible to specify the default protocol (except when Domain = local). We need to be able to retreive the resulting protocol, which is not possble on all platforms.

peername(Socket) -> {ok, SockAddr} | {error, Reason} | OTP 22.0

### Types

Socket = socket()
SockAddr = sockaddr()
Reason = term()

Returns the address of the peer connected to the socket.

recv(Socket) -> {ok, Data} | {error, Reason} | OTP 22.0

recv(Socket, Length) -> {ok, Data} | {error, Reason} | OTP 22.0

recv(Socket, Length, Flags) -> {ok, Data} | {error, Reason} | OTP 22.0

recv(Socket, Length, Timeout) -> {ok, Data} | {error, Reason} | OTP 22.0

recv(Socket, Length, Flags, Timeout) -> {ok, Data} | {error, Reason} | OTP 22.0

### Types

Socket = socket()
Length = integer() >= 0
Flags = recv_flags()
Timeout = timeout()
Data = binary()
Reason = term()

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".

recv(Socket, Length, Timeout :: nowait) -> {ok, Data} | {select, SelectInfo} | {ok, {Data, SelectInfo}} | {error, Reason} | OTP 22.1

recv(Socket, Length, Flags, Timeout :: nowait) -> {ok, Data} | {select, SelectInfo} | {ok, {Data, SelectInfo}} | {error, Reason} | OTP 22.1

### Types

Socket = socket()
Length = integer() >= 0
Flags = recv_flags()
Data = binary()
SelectInfo = select_info()
Reason = term()

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".

In the case when there is no data waiting, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo), when data has arrived (a subsequent call to recv will then return the data).

Note that if a length (> 0) is specified, and only part of that amount of data is available, the function will return with that data and the SelectInfo (if the caller don't want to wait for the remaining data, it must immediately call the cancel/2 function.)

recvfrom(Socket) -> {ok, {Source, Data}} | {error, Reason} | OTP 22.0

recvfrom(Socket, BufSz) -> {ok, {Source, Data}} | {error, Reason} | OTP 22.0

recvfrom(Socket, Flags, Timeout) -> {ok, {Source, Data}} | {error, Reason} | OTP 22.0

recvfrom(Socket, BufSz, Flags) -> {ok, {Source, Data}} | {error, Reason} | OTP 22.0

recvfrom(Socket, BufSz, Timeout) -> {ok, {Source, Data}} | {error, Reason} | OTP 22.0

recvfrom(Socket, BufSz, Flags, Timeout) -> {ok, {Source, Data}} | {error, Reason} | OTP 22.0

### Types

Socket = socket()
BufSz = integer() >= 0
Flags = recv_flags()
Timeout = timeout()
Source = sockaddr() | undefined
Data = binary()
Reason = term()

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 (if the buffer size is too small, the message will be truncated).

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.

recvfrom(Socket, Flags, Timeout :: nowait) -> {ok, {Source, Data}} | {select, SelectInfo} | {error, Reason} | OTP 22.1

recvfrom(Socket, BufSz, Timeout :: nowait) -> {ok, {Source, Data}} | {select, SelectInfo} | {error, Reason} | OTP 22.1

recvfrom(Socket, BufSz, Flags, Timeout :: nowait) -> {ok, {Source, Data}} | {select, SelectInfo} | {error, Reason} | OTP 22.1

### Types

Socket = socket()
BufSz = integer() >= 0
Flags = recv_flags()
Source = sockaddr() | undefined
Data = binary()
SelectInfo = select_info()
Reason = term()

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 (if the buffer size is too small, the message will be truncated).

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.

In the case when there is no data waiting, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo), when data has arrived (a subsequent call to recvfrom will then return the data).

recvmsg(Socket) -> {ok, MsgHdr} | {error, Reason} | OTP 22.0

recvmsg(Socket, Flags) -> {ok, MsgHdr} | {error, Reason} | OTP 22.0

recvmsg(Socket, Timeout) -> {ok, MsgHdr} | {error, Reason} | OTP 22.0

recvmsg(Socket, Flags, Timeout) -> {ok, MsgHdr} | {error, Reason} | OTP 22.0

recvmsg(Socket, BufSz, CtrlSz) -> {ok, MsgHdr} | {error, Reason} | OTP 22.0

recvmsg(Socket, BufSz, CtrlSz, Flags, Timeout) -> {ok, MsgHdr} | {error, Reason} | OTP 22.0

### Types

Socket = socket()
BufSz = CtrlSz = integer() >= 0
Flags = recv_flags()
Timeout = timeout()
MsgHdr = msghdr()
Reason = term()

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.

recvmsg(Socket, Timeout :: nowait) -> {ok, MsgHdr} | {select, SelectInfo} | {error, Reason} | OTP 22.1

recvmsg(Socket, Flags, Timeout :: nowait) -> {ok, MsgHdr} | {select, SelectInfo} | {error, Reason} | OTP 22.1

recvmsg(Socket, BufSz, CtrlSz, Flags, Timeout :: nowait) -> {ok, MsgHdr} | {select, SelectInfo} | {error, Reason} | OTP 22.1

### Types

Socket = socket()
BufSz = CtrlSz = integer() >= 0
Flags = recv_flags()
MsgHdr = msghdr()
SelectInfo = select_info()
Reason = term()

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.

In the case when there is no data waiting, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo), when data has arrived (a subsequent call to recvmsg will then return the data).

send(Socket, Data) -> ok | {error, Reason} | OTP 22.0

send(Socket, Data, Flags) -> ok | {error, Reason} | OTP 22.0

send(Socket, Data, Timeout) -> ok | {error, Reason} | OTP 22.0

send(Socket, Data, Flags, Timeout) -> ok | {error, Reason} | OTP 22.0

### Types

Socket = socket()
Data = iodata()
Flags = send_flags()
Timeout = timeout()
Reason = term()

Send a message on a connected socket.

send(Socket, Data, Timeout :: nowait) -> ok | {ok, {binary(), SelectInfo}} | {select, SelectInfo} | {ok, {RestData, SelectInfo}} | {error, Reason} | OTP 22.1

send(Socket, Data, Flags, Timeout :: nowait) -> ok | {select, SelectInfo} | {ok, {RestData, SelectInfo}} | {error, Reason} | OTP 22.1

### Types

Socket = socket()
Data = iodata()
Flags = send_flags()
RestData = binary()
SelectInfo = select_info()
Reason = term()

Send a message on a connected socket.

In the case when there is no room in the (system-) buffers, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo), when there is room for more data (a subsequent call to send will then send the data).

Note that if not all the data was sent, the function will return with the remaining data and the SelectInfo (if the caller don't want to wait to be able to send the rest, it should immediately call the cancel/2 function.)

sendmsg(Socket, MsgHdr) -> ok | {ok, Remaining} | {error, Reason} | OTP 22.0

sendmsg(Socket, MsgHdr, Flags) -> ok | {error, Reason} | OTP 22.0

sendmsg(Socket, MsgHdr, Timeout) -> ok | {error, Reason} | OTP 22.0

sendmsg(Socket, MsgHdr, Flags, Timeout) -> ok | {ok, Remaining} | {error, Reason} | OTP 22.0

### Types

Socket = socket()
MsgHdr = msghdr()
Flags = send_flags()
Timeout = timeout()
Remaining = erlang:iovec()
Reason = term()

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).

sendmsg(Socket, MsgHdr, Timeout :: nowait) -> ok | {ok, Remaining} | {error, Reason} | OTP 22.1

sendmsg(Socket, MsgHdr, Flags, Timeout :: nowait) -> ok | {ok, Remaining} | {error, Reason} | OTP 22.1

### Types

Socket = socket()
MsgHdr = msghdr()
Flags = send_flags()
Remaining = erlang:iovec()
Reason = term()

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).

In the case when there is no room in the (system-) buffers, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo), when there is room for more data (a subsequent call to sendmsg will then send the data).

sendto(Socket, Data, Dest) -> ok | {error, Reason} | OTP 22.0

sendto(Socket, Data, Dest, Flags) -> ok | {error, Reason} | OTP 22.0

sendto(Socket, Data, Dest, Timeout) -> ok | {error, Reason} | OTP 22.0

sendto(Socket, Data, Dest, Flags, Timeout) -> ok | {error, Reason} | OTP 22.0

### Types

Socket = socket()
Data = binary()
Dest = sockaddr()
Flags = send_flags()
Timeout = timeout()
Reason = term()

Send a message on a socket, to the specified destination.

sendto(Socket, Data, Dest, Timeout :: nowait) -> ok | {select, SelectInfo} | {error, Reason} | OTP 22.1

sendto(Socket, Data, Dest, Flags, Timeout :: nowait) -> ok | {ok, {binary(), SelectInfo}} | {select, SelectInfo} | {error, Reason} | OTP 22.1

### Types

Socket = socket()
Data = binary()
Dest = sockaddr()
Flags = send_flags()
SelectInfo = select_info()
Reason = term()

Send a message on a socket, to the specified destination.

In the case when there is no room in the (system-) buffers, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo), when there is room for more data (a subsequent call to sendto will then send the data).

setopt(Socket, Level :: otp, Key :: otp_socket_option(), Value) -> ok | {error, Reason} | OTP 22.0

setopt(Socket, Level :: socket, Key :: socket_option(), Value) -> ok | {error, Reason} | OTP 22.0

setopt(Socket, Level :: ip, Key :: ip_socket_option(), Value) -> ok | {error, Reason} | OTP 22.0

setopt(Socket, Level :: ipv6, Key :: ipv6_socket_option(), Value) -> ok | {error, Reason} | OTP 22.0

setopt(Socket, Level :: tcp, Key :: tcp_socket_option(), Value) -> ok | {error, Reason} | OTP 22.0

setopt(Socket, Level :: udp, Key :: udp_socket_option(), Value) -> ok | {error, Reason} | OTP 22.0

setopt(Socket, Level :: sctp, Key :: sctp_socket_option(), Value) -> ok | {error, Reason} | OTP 22.0

### Types

Socket = socket()
Value = Reason = term()

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.

Note

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.

Note

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').

setopt(Socket, Level, Key, Value) -> ok | {error, Reason} | OTP 22.0

### Types

Socket = socket()
Level = Key = integer() >= 0
Value = binary()
Reason = term()

Set options on a socket.

When specifying Level as an integer, and therefor using "native mode", it is *currently* up to the caller to know how to encode the Value.

For more info, see setopt above.

shutdown(Socket, How) -> ok | {error, Reason} | OTP 22.0

### Types

Socket = socket()
How = shutdown_how()
Reason = term()

Shut down all or part of a full-duplex connection.

sockname(Socket) -> {ok, SockAddr} | {error, Reason} | OTP 22.0

### Types

Socket = socket()
SockAddr = sockaddr()
Reason = term()

Returns the current address to which the socket is bound.

supports() -> [{options, supports_options()} | {sctp, boolean()} | {ipv6, boolean()} | {local, boolean()} | {send_flags, supports_send_flags()} | {recv_flags, supports_recv_flags()}] | OTP 22.0

supports(Key1 :: options) -> supports_options() | OTP 22.0

supports(Key1 :: sctp) -> boolean() | OTP 22.0

supports(Key1 :: ipv6) -> boolean() | OTP 22.0

supports(Key1 :: local) -> boolean() | OTP 22.0

supports(Key1 :: send_flags) -> supports_send_flags() | OTP 22.3

supports(Key1 :: recv_flags) -> supports_recv_flags() | OTP 22.3

supports(Key1) -> false | OTP 22.0

supports(Key1 :: options, Level :: socket) -> supports_options_socket() | OTP 22.0

supports(Key1 :: options, Level :: ip) -> supports_options_ip() | OTP 22.0

supports(Key1 :: options, Level :: ipv6) -> supports_options_ipv6() | OTP 22.0

supports(Key1 :: options, Level :: tcp) -> supports_options_tcp() | OTP 22.0

supports(Key1 :: options, Level :: udp) -> supports_options_udp() | OTP 22.0

supports(Key1 :: options, Level :: sctp) -> supports_options_sctp() | OTP 22.0

supports(Key1 :: send_flags, SendFlag :: send_flag()) -> boolean() | OTP 22.3

supports(Key1 :: recv_flags, RecvFlag :: recv_flag()) -> boolean() | OTP 22.3

supports(Key1, Key2) -> false | OTP 22.0

supports(Key1 :: options, Level :: socket, Opt :: socket_option()) -> boolean() | OTP 22.0

supports(Key1 :: options, Level :: ip, Opt :: ip_socket_option()) -> boolean() | OTP 22.0

supports(Key1 :: options, Level :: ipv6, Opt :: ipv6_socket_option()) -> boolean() | OTP 22.0

supports(Key1 :: options, Level :: tcp, Opt :: tcp_socket_option()) -> boolean() | OTP 22.0

supports(Key1 :: options, Level :: udp, Opt :: udp_socket_option()) -> boolean() | OTP 22.0

supports(Key1 :: options, Level :: sctp, Opt :: sctp_socket_option()) -> boolean() | OTP 22.0

supports(Key1, Key2, Key3) -> false | OTP 22.0

### Types

Key1 = Key2 = Key3 = term()

This function intends to retreive information about what the platform supports. Such as if SCTP is supported. Or which socket options are supported.

which_sockets() -> [socket()] | OTP 22.3

which_sockets(FilterRule) -> [socket()] | OTP 22.3

### Types

FilterRule =
inet | inet6 | stream | dgram | seqpacket | sctp | tcp | udp |
pid() |
fun((socket_info()) -> boolean())

Returns a list of all sockets, according to the filter rule.

There are several pre-made filter rule(s) and one general:

inet | inet6

  Selection based on the domain of the socket.
  Only a subset is valid.

stream | dgram | seqpacket

  Selection based on the type of the socket.
  Only a subset is valid.

sctp | tcp | udp

  Selection based on the protocol of the socket.
  Only a subset is valid.

pid()

  Selection base on which sockets has this pid as Controlling Process.

fun((socket_info()) -> boolean())

  The general filter rule.
  A fun that takes the socket info and returns a boolean() (true if the socket sould be included and false if should not).

### 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).