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.
Some functions allow for an asynchronous call.
This is achieved by setting the
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
This message indicates
that the (asynchronous) operation has been aborted.
If, for instance, the socket has been closed (by another process),
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).
As returned by
A tag that describes the (select) operation.
A reference that uniquely identifies the (select) operation.
Accept a connection on a socket.
This call is used with connection-based socket types
(
Accept a connection on a socket.
This call is used with connection-based socket types
(
In the case when there is no connections waiting, the function
will return with the
Bind a name to a socket.
When a socket is created
(with
The rules used for name binding vary between domains.
Cancel an asynchronous request.
Call this function in order to cancel a previous
asynchronous call to, e.g.
Closes the socket.
Note that for e.g.
One way to handle this is to use the
This function connects the socket to the address
specied by the
This function connects the socket to the address
specied by the
In the case when its not possible to immediately establish a
connection, the function will return with the
Get an option on a socket.
What properties are valid depend both on
See the
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.
What properties are valid depend both on
When specifying
See the
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 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.
Creates an endpoint (socket) for communication.
For some
The
It may not be possible to specify the default protocol (except
when
Returns the address of the peer connected to the socket.
Receive a message from a socket.
There is a special case for the argument
Receive a message from a socket.
There is a special case for the argument
In the case when there is no data waiting, the function
will return with the
Note that if a length (
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
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.
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
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
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
The
The
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.
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
The
The
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
Send a message on a connected 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
Note that if not all the data was sent, the function will return
with the remaining data and the
Send a message on a socket. The destination, if needed
(socket not connected) is provided in the
Unlike the
Send a message on a socket. The destination, if needed
(socket not connected) is provided in the
Unlike the
In the case when there is no room in the (system-) buffers,
the function will return with the
Send a message on a socket, to the specified destination.
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
Set options on a socket.
What properties are valid depend both on
See the
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.
What properties are valid depend both on
See the
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 all or part of a full-duplex connection.
Returns the current address to which the socket is bound.
This function intends to retreive information about what the platform supports. Such as if SCTP is supported. Or which socket options are supported.
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).