Socket (socket2 v2.1.2)

Copy Markdown

Summary

Types

t()

@type t() :: Socket.Protocol.t()

Functions

accept(self)

See Socket.Protocol.accept/1.

accept(self, options)

See Socket.Protocol.accept/2.

accept!(self)

accept!(self, options)

active(self)

See Socket.Protocol.active/1.

active(self, mode)

See Socket.Protocol.active/2.

active!(self)

active!(self, mode)

close(self)

See Socket.Protocol.close/1.

close!(self)

connect(uri)

@spec connect(String.t() | URI.t()) :: {:ok, t()} | {:error, any()}

Create a socket connecting to somewhere using an URI.

Supported URIs

  • tcp://host:port for Socket.TCP
  • ssl://host:port for Socket.SSL
  • ws://host:port/path for Socket.Web (using Socket.TCP)
  • wss://host:port/path for Socket.Web (using Socket.SSL)
  • udp://host:port for Socket:UDP

Example

{ :ok, client } = Socket.connect "tcp://google.com:80"
client.send "GET / HTTP/1.1\r\n"
client.recv

connect!(uri)

@spec connect!(String.t() | URI.t()) :: t() | no_return()

Create a socket connecting to somewhere using an URI, raising if an error occurs, see connect.

equal?(self, other)

See Socket.Protocol.equal?/2.

listen(uri)

@spec listen(String.t() | URI.t()) :: {:ok, t()} | {:error, any()}

Create a socket listening somewhere using an URI.

Supported URIs

If host is * it will be converted to 0.0.0.0.

  • tcp://host:port for Socket.TCP
  • ssl://host:port for Socket.SSL
  • ws://host:port/path for Socket.Web (using Socket.TCP)
  • wss://host:port/path for Socket.Web (using Socket.SSL)
  • udp://host:port for Socket:UDP

Example

{ :ok, server } = Socket.listen "tcp://*:1337"
client = server |> Socket.accept!(packet: :line)
client |> Socket.Stream.send(client.recv)
client |> Socket.Stream.close

listen!(uri)

@spec listen!(String.t() | URI.t()) :: t() | no_return()

Create a socket listening somewhere using an URI, raising if an error occurs, see listen.

local(self)

See Socket.Protocol.local/1.

local!(self)

options(self, opts)

See Socket.Protocol.options/2.

options!(self, opts)

packet(self, type)

See Socket.Protocol.packet/2.

packet!(self, type)

passive(self)

See Socket.Protocol.passive/1.

passive!(self)

process(self, pid)

See Socket.Protocol.process/2.

process!(self, pid)

remote(self)

See Socket.Protocol.remote/1.

remote!(self)