GSMLG.Socket.TCP (GSMLG.Socket v0.1.0)

This module wraps a passive TCP socket using gen_tcp.

options

Options

When creating a socket you can pass a series of options to use for it.

  • :as sets the kind of value returned by recv, either :binary or :list, the default is :binary
  • :mode can be either :passive or :active, default is :passive
  • :local must be a keyword list
    • :address the local address to use
    • :port the local port to use
    • :fd an already opened file descriptor to use
  • :backlog sets the listen backlog
  • :watermark must be a keyword list
    • :low defines the :low_watermark, see inet:setopts
    • :high defines the :high_watermark, see inet:setopts
  • :version sets the IP version to use
  • :options must be a list of atoms
    • :keepalive sets SO_KEEPALIVE
    • :nodelay sets TCP_NODELAY
  • :packet see inet:setopts
  • :size sets the max length of the packet body, see inet:setopts

examples

Examples

server = GSMLG.Socket.TCP.listen!(1337, packet: :line)

client = server |> GSMLG.Socket.accept!
client |> GSMLG.Socket.Stream.send!(client |> GSMLG.Socket.Stream.recv!)
client |> GSMLG.Socket.Stream.close

Link to this section 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.

Link to this section Types

@opaque t()

Link to this section Functions

Link to this function

accept(socket, options \\ [])

@spec accept(t() | port(), Keyword.t()) :: {:ok, t()} | {:error, Error.t()}

Accept a new client from a listening socket, optionally passing options.

@spec accept!(t()) :: t() | no_return()

Accept a new client from a listening socket, optionally passing options, raising if an error occurs.

Link to this function

accept!(self, options)

@spec accept!(t(), Keyword.t()) :: t() | no_return()
Link to this function

arguments(options)

@spec arguments(Keyword.t()) :: list()

Convert TCP options to :inet.setopts compatible arguments.

@spec connect({GSMLG.Socket.Address.t(), :inet.port_number()}) ::
  {:ok, t()} | {:error, GSMLG.Socket.Error.t()}

Create a TCP socket connecting to the given host and port tuple.

Link to this function

connect(address, options)

@spec connect(
  {GSMLG.Socket.Address.t(), :inet.port_number()} | GSMLG.Socket.Address.t(),
  Keyword.t() | :inet.port_number()
) :: {:ok, t()} | {:error, GSMLG.Socket.Error.t()}

Create a TCP socket connecting to the given host and port tuple and options, or to the given host and port.

Link to this function

connect(address, port, options)

@spec connect(String.t() | :inet.ip_address(), :inet.port_number(), Keyword.t()) ::
  {:ok, t()} | {:error, GSMLG.Socket.Error.t()}

Create a TCP socket connecting to the given host and port.

Link to this function

connect!(descriptor)

@spec connect!({GSMLG.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.

Link to this function

connect!(address, port)

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.

Link to this function

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.

@spec error(term()) :: String.t()

Return a proper error string for the given code or nil if it can't be converted.

@spec listen() :: {:ok, t()} | {:error, GSMLG.Socket.Error.t()}

Create a TCP socket listening on an OS chosen port, use local to know the port it was bound on.

@spec listen(:inet.port_number() | Keyword.t()) ::
  {:ok, t()} | {:error, GSMLG.Socket.Error.t()}

Create a TCP socket listening on an OS chosen port using the given options or listening on the given port.

Link to this function

listen(port, options)

@spec listen(:inet.port_number(), Keyword.t()) ::
  {:ok, t()} | {:error, GSMLG.Socket.Error.t()}

Create a TCP socket listening on the given port and using the given options.

@spec listen!() :: t() | no_return()

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.

Link to this function

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.

Link to this function

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.

Link to this function

options(socket, options)

@spec options(t() | GSMLG.Socket.SSL.t() | port(), Keyword.t()) ::
  :ok | {:error, GSMLG.Socket.Error.t()}

Set options of the socket.

Link to this function

options!(socket, options)

@spec options!(t() | GSMLG.Socket.SSL.t() | port(), Keyword.t()) :: :ok | no_return()

Set options of the socket, raising if an error occurs.

Link to this function

process(socket, pid)

@spec process(t(), pid()) :: :ok | {:error, :closed | :not_owner | Error.t()}

Set the process which will receive the messages.

Link to this function

process!(socket, pid)

@spec process!(t() | port(), pid()) :: :ok | no_return()

Set the process which will receive the messages, raising if an error occurs.