Interface to UDP sockets.
This module provides functions for communicating with sockets using the UDP protocol.
Types
option() =
{active, true | false | once | -32768..32767} |
{add_membership, {inet:ip_address(), inet:ip_address()}} |
{broadcast, boolean()} |
{buffer, integer() >= 0} |
{deliver, port | term} |
{dontroute, boolean()} |
{drop_membership, {inet:ip_address(), inet:ip_address()}} |
{header, integer() >= 0} |
{high_msgq_watermark, integer() >= 1} |
{low_msgq_watermark, integer() >= 1} |
{mode, list | binary} |
list | binary |
{multicast_if, inet:ip_address()} |
{multicast_loop, boolean()} |
{multicast_ttl, integer() >= 0} |
{priority, integer() >= 0} |
{raw,
Protocol :: integer() >= 0,
OptionNum :: integer() >= 0,
ValueBin :: binary()} |
{read_packets, integer() >= 0} |
{recbuf, integer() >= 0} |
{reuseaddr, boolean()} |
{sndbuf, integer() >= 0} |
{tos, integer() >= 0} |
{tclass, integer() >= 0} |
{ttl, integer() >= 0} |
{recvtos, boolean()} |
{recvtclass, boolean()} |
{recvttl, boolean()} |
{ipv6_v6only, boolean()}
option_name() =
active | broadcast | buffer | deliver | dontroute | header |
high_msgq_watermark | low_msgq_watermark | mode |
multicast_if | multicast_loop | multicast_ttl | priority |
{raw,
Protocol :: integer() >= 0,
OptionNum :: integer() >= 0,
ValueSpec ::
(ValueSize :: integer() >= 0) | (ValueBin :: binary())} |
read_packets | recbuf | reuseaddr | sndbuf | tos | tclass |
ttl | recvtos | recvtclass | recvttl | pktoptions |
ipv6_v6only
socket()
As returned by
open/1,2
.
Functions
controlling_process(Socket, Pid) -> ok | {error, Reason}
Socket = socket()
Pid = pid()
Reason = closed | not_owner | badarg | inet:posix()
Assigns a new controlling process
to
. The controlling process is the process
that receives messages from the socket. If called by any other
process than the current controlling process,
{error, not_owner}
is returned. If the process identified
by
is not an existing local pid,
{error, badarg}
is returned. {error, badarg}
may also
be returned in some cases when
is closed
during the execution of this function.
open(Port) -> {ok, Socket} | {error, Reason}
Port = inet:port_number()
Socket = socket()
Reason = system_limit | inet:posix()
open(Port, Opts) -> {ok, Socket} | {error, Reason}
Port = inet:port_number()
Opts = [Option]
Option =
{ip, inet:socket_address()} |
{fd, integer() >= 0} |
{ifaddr, inet:socket_address()} |
inet:address_family() |
{port, inet:port_number()} |
{netns, file:filename_all()} |
{bind_to_device, binary()} |
option()Socket = socket()
Reason = system_limit | inet:posix()
Associates a UDP port number (
) with the
calling process.
The following options are available:
list
Received Packet
is delivered as a list.
binary
Received Packet
is delivered as a binary.
{ip, Address}
If the host has many network interfaces, this option specifies which one to use.
{ifaddr, Address}
Same as {ip, Address}
. If the host has many
network interfaces, this option specifies which one to
use.
{fd, integer() >= 0}
If a socket has somehow been opened without using
gen_udp
, use this option to pass the file descriptor
for it. If
is not set to 0
and/or
{ip, ip_address()}
is combined with this option, the
fd
is bound to the specified interface and port after it is
being opened. If these options are not specified, it is assumed that
the fd
is already bound appropriately.
inet6
Sets up the socket for IPv6.
inet
Sets up the socket for IPv4.
local
Sets up a Unix Domain Socket. See
inet:local_address()
{udp_module, module()}
Overrides which callback module is used. Defaults to
inet_udp
for IPv4 and inet6_udp
for IPv6.
{multicast_if, Address}
Sets the local device for a multicast socket.
{multicast_loop, true | false}
When true
, sent multicast packets are looped back to
the local sockets.
{multicast_ttl, Integer}
Option multicast_ttl
changes the time-to-live (TTL)
for outgoing multicast datagrams to control the scope of the
multicasts.
Datagrams with a TTL of 1 are not forwarded beyond the local
network. Defaults to 1
.
{add_membership, {MultiAddress, InterfaceAddress}}
Joins a multicast group.
{drop_membership, {MultiAddress, InterfaceAddress}}
Leaves a multicast group.
Opt
See
inet:setopts/2
.
The returned socket
is used to send
packets from this port with
send/4
.
When UDP packets arrive
at the opened port, if the socket is in an active mode, the packets
are delivered as messages to the controlling process:
{udp, Socket, IP, InPortNo, Packet} % Without ancillary data {udp, Socket, IP, InPortNo, AncData, Packet} % With ancillary data
The message contains an AncData
field
if any of the socket
options
recvtos
,
recvtclass
or
recvttl
are active, otherwise it does not.
If the socket is not in an active mode, data can be
retrieved through the
recv/2,3
calls.
Notice that arriving UDP packets that are longer than
the receive buffer option specifies can be truncated
without warning.
When a socket in {active, N}
mode (see
inet:setopts/2
for details), transitions to passive ({active, false}
) mode,
the controlling process is notified by a message of the following
form:
{udp_passive, Socket}
IP
and InPortNo
define the address from which
Packet
comes. Packet
is a list of bytes if
option list
is specified. Packet
is a
binary if option binary
is specified.
Default value for the receive buffer option is
{recbuf, 8192}
.
If
, the underlying OS assigns a free
UDP port, use
inet:port/1
to retrieve it.
recv(Socket, Length) -> {ok, RecvData} | {error, Reason}
Socket = socket()
Length = integer() >= 0
RecvData =
{Address, Port, Packet} | {Address, Port, AncData, Packet}Address = inet:ip_address() | inet:returned_non_ip_address()
Port = inet:port_number()
AncData = inet:ancillary_data()
Packet = string() | binary()
Reason = not_owner | inet:posix()
recv(Socket, Length, Timeout) -> {ok, RecvData} | {error, Reason}
Socket = socket()
Length = integer() >= 0
Timeout = timeout()
RecvData =
{Address, Port, Packet} | {Address, Port, AncData, Packet}Address = inet:ip_address() | inet:returned_non_ip_address()
Port = inet:port_number()
AncData = inet:ancillary_data()
Packet = string() | binary()
Reason = not_owner | timeout | inet:posix()
Receives a packet from a socket in passive mode. Optional parameter
specifies a time-out in milliseconds.
Defaults to infinity
.
If any of the socket
options
recvtos
,
recvtclass
or
recvttl
are active, the
tuple contains an
field,
otherwise it does not.
send(Socket, Address, Port, Packet) -> ok | {error, Reason}
Socket = socket()
Address = inet:socket_address() | inet:hostname()
Port = inet:port_number()
Packet = iodata()
Reason = not_owner | inet:posix()
Sends a packet to the specified address and port. Argument
can be a hostname or a socket address.