Socket.TCP (socket2 v2.1.2)
This module wraps a passive TCP socket using gen_tcp.
Options
When creating a socket you can pass a series of options to use for it.
:assets the kind of value returned by recv, either:binaryor:list, the default is:binary:modecan be either:passiveor:active, default is:passive:localmust be a keyword list:addressthe local address to use:portthe local port to use:fdan already opened file descriptor to use
:backlogsets the listen backlog:watermarkmust be a keyword list:lowdefines the:low_watermark, seeinet:setopts:highdefines the:high_watermark, seeinet:setopts
:versionsets the IP version to use:optionsmust be a list of atoms or tuples::keepalivesetsSO_KEEPALIVE:nodelaysetsTCP_NODELAY:defer_acceptsetsTCP_DEFER_ACCEPT{:defer_accept, seconds}setTCP_DEFER_ACCEPTwith the given seconds defer time:fast_openenables tcp fast open (TFO) on the socket{:fast_open, queue_len}enables TFO with the given queue_len:fast_open_connectenables tcp fast open connect (TFO) on the socket
:packetseeinet:setopts:sizesets the max length of the packet body, seeinet:setopts
Examples
server = Socket.TCP.listen!(1337, packet: :line)
client = server |> Socket.accept!
client |> Socket.Stream.send!(client |> Socket.Stream.recv!)
client |> Socket.Stream.close
Summary
Functions
Accept a new client from a listening socket, optionally passing options.
Accept a new client from a listening socket, optionally passing options, raising if an error occurs.
Convert TCP options to :inet.setopts compatible arguments.
Create a TCP socket connecting to the given host and port tuple.
Create a TCP socket connecting to the given host and port tuple and options, or to the given host and port.
Create a TCP socket connecting to the given host and port.
Create a TCP socket connecting to the given host and port tuple, raising if an error occurs.
Create a TCP socket connecting to the given host and port tuple and options, or to the given host and port, raising if an error occurs.
Create a TCP socket connecting to the given host and port, raising in case of error.
Return a proper error string for the given code or nil if it can't be converted.
Create a TCP socket listening on an OS chosen port, use local to know the
port it was bound on.
Create a TCP socket listening on an OS chosen port using the given options or listening on the given port.
Create a TCP socket listening on the given port and using the given options.
Create a TCP socket listening on an OS chosen port, use local to know the
port it was bound on, raising in case of error.
Create a TCP socket listening on an OS chosen port using the given options or listening on the given port, raising in case of error.
Create a TCP socket listening on the given port and using the given options, raising in case of error.
Set options of the socket.
Set options of the socket, raising if an error occurs.
Set the process which will receive the messages.
Set the process which will receive the messages, raising if an error occurs.
Types
error()
@type error() :: nil | String.t()
@opaque t()
Functions
accept(socket, options \\ [])
Accept a new client from a listening socket, optionally passing options.
accept!(self)
Accept a new client from a listening socket, optionally passing options, raising if an error occurs.
accept!(self, options)
arguments(options)
Convert TCP options to :inet.setopts compatible arguments.
connect(arg)
@spec connect({Socket.Address.t(), :inet.port_number()}) :: {:ok, t()} | {:error, Socket.Error.t()}
Create a TCP socket connecting to the given host and port tuple.
connect(address, options)
@spec connect( {Socket.Address.t(), :inet.port_number()} | Socket.Address.t(), Keyword.t() | :inet.port_number() ) :: {:ok, t()} | {:error, Socket.Error.t()}
Create a TCP socket connecting to the given host and port tuple and options, or to the given host and port.
connect(address, port, options)
@spec connect(String.t() | :inet.ip_address(), :inet.port_number(), Keyword.t()) :: {:ok, t()} | {:error, Socket.Error.t()}
Create a TCP socket connecting to the given host and port.
connect!(descriptor)
@spec connect!({Socket.Address.t(), :inet.port_number()}) :: t() | no_return()
Create a TCP socket connecting to the given host and port tuple, raising if an error occurs.
connect!(address, port)
@spec connect!( {Socket.Address.t(), :inet.port_number()} | Socket.Address.t(), Keyword.t() | :inet.port_number() ) :: t() | no_return()
Create a TCP socket connecting to the given host and port tuple and options, or to the given host and port, raising if an error occurs.
connect!(address, port, options)
@spec connect!(String.t() | :inet.ip_address(), :inet.port_number(), Keyword.t()) :: t() | no_return()
Create a TCP socket connecting to the given host and port, raising in case of error.
error(code)
Return a proper error string for the given code or nil if it can't be converted.
listen()
@spec listen() :: {:ok, t()} | {:error, Socket.Error.t()}
Create a TCP socket listening on an OS chosen port, use local to know the
port it was bound on.
listen(port)
@spec listen(:inet.port_number() | Keyword.t()) :: {:ok, t()} | {:error, Socket.Error.t()}
Create a TCP socket listening on an OS chosen port using the given options or listening on the given port.
listen(port, options)
@spec listen(:inet.port_number(), Keyword.t()) :: {:ok, t()} | {:error, Socket.Error.t()}
Create a TCP socket listening on the given port and using the given options.
listen!()
Create a TCP socket listening on an OS chosen port, use local to know the
port it was bound on, raising in case of error.
listen!(port_or_options)
@spec listen!(:inet.port_number() | Keyword.t()) :: t() | no_return()
Create a TCP socket listening on an OS chosen port using the given options or listening on the given port, raising in case of error.
listen!(port, options)
@spec listen!(:inet.port_number(), Keyword.t()) :: t() | no_return()
Create a TCP socket listening on the given port and using the given options, raising in case of error.
options(socket, options)
@spec options(t() | Socket.SSL.t() | port(), Keyword.t()) :: :ok | {:error, Socket.Error.t()}
Set options of the socket.
options!(socket, options)
@spec options!(t() | Socket.SSL.t() | port(), Keyword.t()) :: :ok | no_return()
Set options of the socket, raising if an error occurs.
process(socket, pid)
Set the process which will receive the messages.
process!(socket, pid)
Set the process which will receive the messages, raising if an error occurs.