20182019 Ericsson AB. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. socket socket.xml
socket Socket interface.

This module provides an API for the socket interface. It is used to create, delete and manipulate sockets, send and receive data.

The idea is that it shall be as "close as possible" to the OS level socket interface. The only significant addition is that some of the functions, e.g. recv/3, has a timeout argument.

Some functions allow for an asynchronous call. This is achieved by setting the Timeout argument to nowait. For instance, if calling the recv/3 function with Timeout set to nowait (recv(Sock, 0, nowait)) when there is actually nothing to read, it will return with {select, SelectInfo} (SelectInfo contains the SelectRef). When data eventually arrives a 'select' message will be sent to the caller:

{'$socket', socket(), select, SelectRef}

The caller can now make another call to the recv function and now expect data.

Note that all other users are locked out until the 'current user' has called the function (recv in this case).

Another message the user must be prepared for (when making asynchronous calls) is the abort message:

{'$socket', socket(), abort, Info}

This message indicates that the (asynchronous) operation has been aborted. If, for instance, the socket has been closed (by another process), Info will be {SelectRef, closed}.

There is currently no support for Windows.

Support for IPv6 has been implemented but not tested.

SCTP has only been partly implemented (and not tested).

socket()

As returned by open/2,3,4 and accept/1,2.

A tag that describes the (select) operation.

A reference that uniquely identifies the (select) operation.

Accept a connection on a socket.

Accept a connection on a socket.

This call is used with connection-based socket types (stream or seqpacket). It extracs the first pending connection request for the listen socket and returns the (newly) connected socket.

Accept a connection on a socket.

Accept a connection on a socket.

This call is used with connection-based socket types (stream or seqpacket). It extracs the first pending connection request for the listen socket and returns the (newly) connected socket.

In the case when there is no connections waiting, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo), when a client connects (a subsequent call to accept will then return the socket).

Bind a name to a socket.

Bind a name to a socket.

When a socket is created (with open), it has no address assigned to it. bind assigns the address specified by the Addr argument.

The rules used for name binding vary between domains.

Cancel an asynchronous request.

Cancel an asynchronous request.

Call this function in order to cancel a previous asynchronous call to, e.g. recv/3.

Close a socket.

Closes the socket.

Note that for e.g. protocol = tcp, most implementations doing a close does not guarantee that any data sent is delivered to the recipient before the close is detected at the remote side.

One way to handle this is to use the shutdown function (socket:shutdown(Socket, write)) to signal that no more data is to be sent and then wait for the read side of the socket to be closed.

Initiate a connection on a socket.

This function connects the socket to the address specied by the SockAddr argument.

Initiate a connection on a socket.

This function connects the socket to the address specied by the SockAddr argument.

In the case when its not possible to immediately establish a connection, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo, a subsequent call to connect will then establish the connection).

Get an option on a socket.

Get an option on a socket.

What properties are valid depend both on Level and on what kind of socket it is (domain, type and protocol).

See the socket options chapter of the users guide for more info.

Not all options are valid on all platforms. That is, even if "we" support an option, that does not mean that the underlying OS does.

Get an option on a socket.

Get an option on a socket.

What properties are valid depend both on Level and on what kind of socket it is (domain, type and protocol).

When specifying Level as an integer, and therefor using "native mode", it is *currently* up to the caller to know how to interpret the result.

See the socket options chapter of the users guide for more info.

Not all options are valid on all platforms. That is, even if "we" support an option, that does not mean that the underlying OS does.

Get miscellaneous socket info.

Get miscellaneous info about the socket.

The function returns a map with each info item as a key-value binding. It reflects the "current" state of the socket.

In order to ensure data integrity, mutex'es are taken when needed. So, do not call this function often.

Listen for connections on a socket.

Listen for connections on a socket.

Create an endpoint for communication.

Creates an endpoint (socket) for communication.

For some types there is a default protocol, indicated by default, which it may be possible to specify. And for Domain = local, if a protocol is pecified, it must be default.

The Extra argument is intended for "obscure" options. Currently the only supported option is netns, which is only supported on the linux platform.

It may not be possible to specify the default protocol (except when Domain = local). We need to be able to retreive the resulting protocol, which is not possble on all platforms.

Get name of connected socket peer.

Returns the address of the peer connected to the socket.

Receive a message from a socket.

Receive a message from a socket.

There is a special case for the argument Length. If it is set to zero (0), it means "give me everything you currently have".

Receive a message from a socket.

Receive a message from a socket.

There is a special case for the argument Length. If it is set to zero (0), it means "give me everything you currently have".

In the case when there is no data waiting, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo), when data has arrived (a subsequent call to recv will then return the data).

Note that if a length (> 0) is specified, and only part of that amount of data is available, the function will return with that data and the SelectInfo (if the caller don't want to wait for the remaining data, it must immediately call the cancel/2 function.)

Receive a message from a socket.

Receive a message from a socket.

This function reads "messages", which means that regardless of how much we want to read, it returns when we get a message (if the buffer size is to small, the message will be truncated).

The BufSz argument basically defines the size of the receive buffer. By setting the value to zero (0), the configured size (setopt with Level = otp and Key = rcvbuf) is used.

It may be impossible to know what (buffer) size is appropriate "in advance", and in those cases it may be convenient to use the (recv) 'peek' flag. When this flag is provided, the message is *not* "consumed" from the underlying buffers, so another recvfrom call is needed, possibly with a then adjusted buffer size.

Receive a message from a socket.

Receive a message from a socket.

This function reads "messages", which means that regardless of how much we want to read, it returns when we get a message (if the buffer size is to small, the message will be truncated).

The BufSz argument basically defines the size of the receive buffer. By setting the value to zero (0), the configured size (setopt with Level = otp and Key = rcvbuf) is used.

It may be impossible to know what (buffer) size is appropriate "in advance", and in those cases it may be convenient to use the (recv) 'peek' flag. When this flag is provided, the message is *not* "consumed" from the underlying buffers, so another recvfrom call is needed, possibly with a then adjusted buffer size.

In the case when there is no data waiting, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo), when data has arrived (a subsequent call to recvfrom will then return the data).

Receive a message from a socket.

Receive a message from a socket.

This function reads "messages", which means that regardless of how much we want to read, it returns when we get a message.

The message will be delivered in the form of a msghdr(), which may contain the source address (if socket not connected), a list of cmsghdr_recv() (depends on what socket options have been set and what the protocol and platform supports) and also a set of flags, providing further info about the read.

The BufSz argument basically defines the size of the receive buffer. By setting the value to zero (0), the configured size (setopt with Level = otp and Key = rcvbuf) is used.

The CtrlSz argument basically defines the size of the receive buffer for the control messages. By setting the value to zero (0), the configured size (setopt with Level = otp) is used.

It may be impossible to know what (buffer) size is appropriate "in advance", and in those cases it may be convenient to use the (recv) 'peek' flag. When this flag is provided, the message is *not* "consumed" from the underlying buffers, so another recvmsg call is needed, possibly with a then adjusted buffer size.

Receive a message from a socket.

Receive a message from a socket.

This function reads "messages", which means that regardless of how much we want to read, it returns when we get a message.

The message will be delivered in the form of a msghdr(), which may contain the source address (if socket not connected), a list of cmsghdr_recv() (depends on what socket options have been set and what the protocol and platform supports) and also a set of flags, providing further info about the read.

The BufSz argument basically defines the size of the receive buffer. By setting the value to zero (0), the configured size (setopt with Level = otp and Key = rcvbuf) is used.

The CtrlSz argument basically defines the size of the receive buffer for the control messages. By setting the value to zero (0), the configured size (setopt with Level = otp) is used.

It may be impossible to know what (buffer) size is appropriate "in advance", and in those cases it may be convenient to use the (recv) 'peek' flag. When this flag is provided, the message is *not* "consumed" from the underlying buffers, so another recvmsg call is needed, possibly with a then adjusted buffer size.

In the case when there is no data waiting, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo), when data has arrived (a subsequent call to recvmsg will then return the data).

Send a message on a socket.

Send a message on a connected socket.

Send a message on a socket.

Send a message on a connected socket.

In the case when there is no room in the (system-) buffers, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo), when there is room for more data (a subsequent call to send will then send the data).

Note that if not all the data was sent, the function will return with the remaining data and the SelectInfo (if the caller don't want to wait to be able to send the rest, it should immediately call the cancel/2 function.)

Send a message on a socket.

Send a message on a socket. The destination, if needed (socket not connected) is provided in the MsgHdr, which also contains the message to send, The MsgHdr may also contain an list of optional cmsghdr_send() (depends on what the protocol and platform supports).

Unlike the send function, this one sends one message. This means that if, for whatever reason, its not possible to send the message in one go, the function will instead return with the remaining data ({ok, Remaining}). Thereby leaving it up to the caller to decide what to do (retry with the remaining data of give up).

Send a message on a socket.

Send a message on a socket. The destination, if needed (socket not connected) is provided in the MsgHdr, which also contains the message to send, The MsgHdr may also contain an list of optional cmsghdr_send() (depends on what the protocol and platform supports).

Unlike the send function, this one sends one message. This means that if, for whatever reason, its not possible to send the message in one go, the function will instead return with the remaining data ({ok, Remaining}). Thereby leaving it up to the caller to decide what to do (retry with the remaining data of give up).

In the case when there is no room in the (system-) buffers, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo), when there is room for more data (a subsequent call to sendmsg will then send the data).

Send a message on a socket.

Send a message on a socket, to the specified destination.

Send a message on a socket.

Send a message on a socket, to the specified destination.

In the case when there is no room in the (system-) buffers, the function will return with the SelectInfo. The caller can then await a select message, {'$socket', Socket, select, Info} (where Info is the ref field from the SelectInfo), when there is room for more data (a subsequent call to sendto will then send the data).

Set options on a socket.

Set options on a socket.

What properties are valid depend both on Level and on what kind of socket it is (domain, type and protocol).

See the socket options chapter of the users guide for more info.

Not all options are valid on all platforms. That is, even if "we" support an option, that does not mean that the underlying OS does.

Sockets are set 'non-blocking' when created, so this option is *not* available (as it would adversely effect the Erlang VM to set a socket 'blocking').

Set options on a socket.

Set options on a socket.

What properties are valid depend both on Level and on what kind of socket it is (domain, type and protocol).

See the socket options chapter of the users guide for more info.

Not all options are valid on all platforms. That is, even if "we" support an option, that does not mean that the underlying OS does.

Sockets are set 'non-blocking' when created, so this option is *not* available (as it would adversely effect the Erlang VM to set a socket 'blocking').

Shut down part of a full-duplex connection.

Shut down all or part of a full-duplex connection.

Get socket name.

Returns the current address to which the socket is bound.

Report info about what the platform supports.

This function intends to retreive information about what the platform supports. Such as if SCTP is supported. Or which socket options are supported.

Examples client(Addr, SAddr, SPort) -> {ok, Sock} = socket:open(inet, stream, tcp), {ok, _} = socket:bind(Sock, #{family => inet, addr => Addr}), ok = socket:connect(Sock, #{family => inet, addr => SAddr, port => SPort}), Msg = list_to_binary("hello"), ok = socket:send(Sock, Msg), ok = socket:shutdown(Sock, write), {ok, Msg} = socket:recv(Sock), ok = socket:close(Sock). server(Addr, Port) -> {ok, LSock} = socket:open(inet, stream, tcp), {ok, _} = socket:bind(LSock, #{family => inet, port => Port, addr => Addr}), ok = socket:listen(LSock), {ok, Sock} = socket:accept(LSock), {ok, Msg} = socket:recv(Sock), ok = socket:send(Sock, Msg), ok = socket:shutdown(Sock, write), ok = socket:close(Sock), ok = socket:close(LSock).