Provides access to TCP/IP protocols.
See also ERTS User's Guide, Inet configuration for more information on how to configure an Erlang runtime system for IP communication.
Two Kernel configuration parameters affect the behaviour of all
sockets opened on an Erlang node:
Using the Kernel configuration parameters mentioned above, one
can set default options for all TCP sockets on a node. This should
be used with care, but options like
$ erl -sname test -kernel \ inet_default_connect_options '[{delay_send,true}]' \ inet_default_listen_options '[{delay_send,true}]'
Note that the default option
Addresses as inputs to functions can be either a string or a
tuple. For instance, the IP address 150.236.20.73 can be passed to
IPv4 address examples:
Address ip_address()
------- ------------
127.0.0.1 {127,0,0,1}
192.168.42.2 {192,168,42,2}
IPv6 address examples:
Address ip_address()
------- ------------
::1 {0,0,0,0,0,0,0,1}
::192.168.42.2 {0,0,0,0,0,0,(192 bsl 8) bor 168,(42 bsl 8) bor 2}
FFFF::192.168.42.2
{16#FFFF,0,0,0,0,0,(192 bsl 8) bor 168,(42 bsl 8) bor 2}
3ffe:b80:1f8d:2:204:acff:fe17:bf38
{16#3ffe,16#b80,16#1f8d,16#2,16#204,16#acff,16#fe17,16#bf38}
fe80::204:acff:fe17:bf38
{16#fe80,0,0,0,0,16#204,16#acff,16#fe17,16#bf38}
A function that may be useful is
1> inet_parse:address("192.168.42.2"). {ok,{192,168,42,2}} 2> inet_parse:address("FFFF::192.168.42.2"). {ok,{65535,0,0,0,0,0,49320,10754}}
The record is defined in the Kernel include file "inet.hrl". Add the following directive to the module:
-include_lib("kernel/include/inet.hrl").
An atom which is named from the Posix error codes
used in Unix, and in the runtime libraries of most
C compilers. See
See
Closes a socket of any type.
Returns the state of the Inet configuration database in form of a list of recorded configuration parameters. (See the ERTS User's Guide, Inet configuration, for more information). Only parameters with other than default values are returned.
Returns a diagnostic error string. See the section below for possible Posix values and the corresponding strings.
Returns the IP-address for
Returns a list of all IP-addresses for
Returns a
Returns a
Returns a
Returns the local hostname. Will never fail.
Returns a list of 2-tuples containing interface names and the
interface's addresses.
The
Do not rely too much on the order of
The
On Windows, the data is fetched from quite different OS API
functions, so the
Gets one or more options for a socket.
See
The number of elements in the returned
A raw option request
The
Asking for and inspecting raw socket options require low level information about the current operating system and TCP stack.
As an example, consider a Linux machine where the
{ok,[{raw,_,_,Info}]} = inet:getopts(Sock,[{raw,6,11,92}]),
<<_:28/binary,TcpiSacked:32/native,_/binary>> = Info,
TcpiSacked.]]>
Preferably, you would check the machine type, the OS and the kernel version prior to executing anything similar to the code above.
Gets one or more statistic options for a socket.
The following options are available:
Average size of packets in bytes received to the socket.
Number of packets received to the socket.
Average packet size deviation in bytes received to the socket.
The size of the largest packet in bytes received to the socket.
Number of bytes received to the socket.
Average size of packets in bytes sent from the socket.
Number of packets sent from the socket.
Average packet size deviation in bytes received sent from the socket.
The size of the largest packet in bytes sent from the socket.
Number of bytes sent from the socket.
Parses an IPv4 address string and returns an ip4_address(). Accepts a shortened IPv4 shortened address string.
Parses an IPv4 address string containing four fields, i.e not shortened, and returns an ip4_address().
Parses an IPv6 address string and returns an ip6_address(). If an IPv4 address string is passed, an IPv4-mapped IPv6 address is returned.
Parses an IPv6 address string and returns an ip6_address(). Does not accept IPv4 adresses.
Parses an IPv4 or IPv6 address string and returns an ip4_address() or ip6_address(). Accepts a shortened IPv4 address string.
Parses an IPv4 or IPv6 address string and returns an ip4_address() or ip6_address(). Does not accept a shortened IPv4 address string.
Returns the address and port for the other end of a connection.
Returns the local port number for a socket.
Returns the local address and port number for a socket.
Sets one or more options for a socket. The following options are available:
If the value is
If the value is
When using
Note that
Enable/disable permission to send broadcasts.
Determines the size of the user-level software buffer used by
the driver. Not to be confused with
Normally, when an Erlang process sends to a socket,
the driver will try to immediately send the data. If that
fails, the driver will use any means available to queue
up the message to be sent whenever the operating system
says it can handle it. Setting
When
Enable/disable routing bypass for outgoing messages.
By default this option is set to
The only reason to set it to
This option is only meaningful if the
The socket message queue will be set into a busy state when the amount of data queued on the message queue reaches this limit. Note that this limit only concerns data that have not yet reached the ERTS internal socket implementation. Default value used is 8 kB.
Senders of data to the socket will be suspended if either the socket message queue is busy, or the socket itself is busy.
For more information see the
Note that distribution sockets will disable the use of
The socket will be set into a busy state when the amount of data queued internally by the ERTS socket implementation reaches this limit. Default value used is 8 kB.
Senders of data to the socket will be suspended if either the socket message queue is busy, or the socket itself is busy.
For more information see the
Restricts the socket to only use IPv6, prohibiting any
IPv4 connections. This is only applicable for
IPv6 sockets (option
On most platforms this option has to be set on the socket
before associating it to an address. Therefore it is only
reasonable to give it when creating the socket and not
to use it when calling the function
(
The behaviour of a socket with this socket option set to
On some platforms the only allowed value for this option
is
Setting this option on platforms where it does not exist
is ignored and getting this option with
So it boils down to that setting this option to
If you read back the option value using
Enables/disables periodic transmission on a connected socket, when no other data is being exchanged. If the other end does not respond, the connection is considered broken and an error message will be sent to the controlling process. Default disabled.
Determines the timeout in seconds for flushing unsent data in the
If the socket message queue is in a busy state, the socket message queue will be set in a not busy state when the amount of data queued in the message queue falls below this limit. Note that this limit only concerns data that have not yet reached the ERTS internal socket implementation. Default value used is 4 kB.
Senders that have been suspended due to either a busy message queue or a busy socket, will be resumed when neither the socket message queue, nor the socket are busy.
For more information see the
Note that distribution sockets will disable the use of
If the socket is in a busy state, the socket will be set in a not busy state when the amount of data queued internally by the ERTS socket implementation falls below this limit. Default value used is 4 kB.
Senders that have been suspended due to either a busy message queue or a busy socket, will be resumed when neither the socket message queue, nor the socket are busy.
For more information see the
Received
Set a network namespace for the socket. The
This option uses the Linux specific syscall
The virtual machine also needs elevated privileges either
running as superuser or (for Linux) having the capability
setcap cap_sys_admin,cap_sys_ptrace,cap_dac_read_search+epi beam.smp
Note also that the filesystem containing the virtual machine
executable (
The
Received
Received
If
Defines the type of packets to use for a socket. The following values are valid:
No packaging is done.
Packets consist of a header specifying the number of bytes in the packet, followed by that number of bytes. The length of header can be one, two, or four bytes; containing an unsigned integer in big-endian byte order. Each send operation will generate the header, and the header will be stripped off on each receive operation.
In current implementation the 4-byte header is limited to 2Gb.
These packet types only have effect on receiving.
When sending a packet, it is the responsibility of
the application to supply a correct header. On
receiving, however, there will be one message sent to
the controlling process for each complete packet
received, and, similarly, each call to
The meanings of the packet types are as follows:
The Hypertext Transfer Protocol. The packets
are returned with the format according to
These two types are often not needed as the socket will
automatically switch from
Sets the max allowed length of the packet body. If the packet header indicates that the length of the packet is longer than the max allowed length, the packet is considered invalid. The same happens if the packet header is too big for the socket receive buffer.
For line oriented protocols (
Set the protocol-defined priority for all packets to be sent on this socket.
See below.
Sets the max number of UDP packets to read without intervention from the socket when data is available. When this many packets have been read and delivered to the destination process, new packets are not read until a new notification of available data has arrived. The default is 5, and if this parameter is set too high the system can become unresponsive due to UDP packet flooding.
Gives the size of the receive buffer to use for the socket.
Allows or disallows local reuse of port numbers. By default, reuse is disallowed.
Only allowed for connection oriented sockets.
Specifies a longest time to wait for a send operation to
be accepted by the underlying TCP stack. When the limit is
exceeded, the send operation will return
Only allowed for connection oriented sockets.
Used together with
Gives the size of the send buffer to use for the socket.
Sets the SO_PRIORITY socket level option on platforms where this is implemented. The behaviour and allowed range varies on different systems. The option is ignored on platforms where the option is not implemented. Use with caution.
Sets IP_TOS IP level options on platforms where this is implemented. The behaviour and allowed range varies on different systems. The option is ignored on platforms where the option is not implemented. Use with caution.
In addition to the options mentioned above, raw
option specifications can be used. The raw options are
specified as a tuple of arity four, beginning with the tag
Using raw socket options require detailed knowledge about the current operating system and TCP stack.
As an example of the usage of raw options, consider a Linux
system where you want to set the
>}]),]]>
As many options are silently discarded by the stack if they are given out of range, it could be a good idea to check that a raw option really got accepted. This code places the value in the variable TcpLinger2:
>}]}=inet:getopts(Sock,[{raw,6,8,4}]),]]>
Code such as the examples above is inherently non portable, even different versions of the same OS on the same platform may respond differently to this kind of option manipulation. Use with care.
Note that the default options for TCP/IP sockets can be changed with the Kernel configuration parameters mentioned in the beginning of this document.