Did you know ... | Search Documentation: |
socket.pl -- Network socket (TCP and UDP) library |
The library(socket) provides TCP and UDP inet-domain sockets from SWI-Prolog, both client and server-side communication. The interface of this library is very close to the Unix socket interface, also supported by the MS-Windows winsock API. SWI-Prolog applications that wish to communicate with multiple sources have two options:
Using this library to establish a TCP connection to a server is as simple as opening a file. See also http_open/3.
dump_swi_homepage :- setup_call_cleanup( tcp_connect('www.swi-prolog.org':http, Stream, []), ( format(Stream, 'GET / HTTP/1.1~n\c Host: www.swi-prolog.org~n\c Connection: close~n~n', []), flush_output(Stream), copy_stream_data(Stream, current_output) ), close(Stream)).
To deal with timeouts and multiple connections, threads, wait_for_input/3 and/or non-blocking streams (see tcp_fcntl/3) can be used.
The typical sequence for generating a server application is given below. To close the server, use close/1 on the StreamPair.
create_server(Port) :- tcp_socket(Socket), tcp_bind(Socket, Port), tcp_listen(Socket, 5), tcp_open_socket(Socket, StreamPair), stream_pair(StreamPair, AcceptFd, _), <dispatch>
There are various options for <dispatch>. The most commonly used option is to start a Prolog thread to handle the connection. Alternatively, input from multiple clients can be handled in a single thread by listening to these clients using wait_for_input/3. Finally, on Unix systems, we can use fork/1 to handle the connection in a new process. Note that fork/1 and threads do not cooperate well. Combinations can be realised but require good understanding of POSIX thread and fork-semantics.
Below is the typical example using a thread. Note the use of setup_call_cleanup/3 to guarantee that all resources are reclaimed, also in case of failure or exceptions.
dispatch(AcceptFd) :- tcp_accept(AcceptFd, Socket, Peer), thread_create(process_client(Socket, Peer), _, [ detached(true) ]), dispatch(AcceptFd). process_client(Socket, Peer) :- setup_call_cleanup( tcp_open_socket(Socket, StreamPair), handle_service(StreamPair), close(StreamPair)). handle_service(StreamPair) :- ...
Errors that are trapped by the low-level library are mapped to an
exception of the shape below. In this term, Code is a lower case atom
that corresponds to the C macro name, e.g., epipe
for a broken pipe.
Message is the human readable string for the error code returned by
the OS or the same as Code if the OS does not provide this
functionality. Note that Code is derived from a static set of macros
that may or may not be defines for the target OS. If the macro name is
not known, Code is ERROR_nnn
, where nnn is an integer.
error(socket_error(Code, Message), _)
Note that on Windows Code is a wsa*
code which makes it hard to
write portable code that handles specific socket errors. Even on POSIX
systems the exact set of errors produced by the network stack is not
defined.
The library supports both IP4 and IP6 addresses. On Unix systems it also
supports Unix domain sockets (AF_UNIX
). The address of a Unix
domain sockets is a file name. Unix domain sockets are created using
socket_create/2 or unix_domain_socket/1.
IP4 or IP6 sockets can be created using socket_create/2 or tcp_connect/3
with the inet
(default, IP3) or inet6
domain option. Some of the
predicates produce or consume IP addresses as a Prolog term. The format
of this term is one of:
The predicate ip_name/2 translates between the canonical textual representation and the above defined address terms.
inet
(default), inet6
, unix
or local
(same
as unix
)stream
(default) to create a TCP connection or
dgram
to create a UDP socket.This predicate subsumes tcp_socket/1, udp_socket/1 and unix_domain_socket/1.
socket_create(SocketId, [])
or, explicit,
socket_create(SocketId, [domain(inet), type(stream)])
.socket_create(SocketId, [domain(unix)])
or,
explicit, socket_create(SocketId, [domain(unix), type(stream)])
Unix domain socket affect tcp_connect/2 (for clients) and
tcp_bind/2 and tcp_accept/3 (for servers). The address is an atom
or string that is handled as a file name. On most systems the
length of this file name is limited to 128 bytes (including null
terminator), but according to the Linux documentation (unix(7)
),
portable applications must keep the address below 92 bytes. Note
that these lengths are in bytes. Non-ascii characters may be
represented as multiple bytes. If the length limit is exceeded a
representation_error(af_unix_name)
exception is raised.
tcp_bind(Socket, localhost:8080)
If Port is unbound, the system picks an arbitrary free port and unifies Port with the selected port number. Port is either an integer or the name of a registered service. See also tcp_connect/4.
af_unix
if Socket is an AF_UNIX socket (see
unix_domain_socket/1).tcp_socket(Socket), tcp_connect(Socket, Host:Port), tcp_open_socket(Socket, StreamPair)
Typical client applications should use the high level interface provided by tcp_connect/3 which avoids resource leaking if a step in the process fails, and can be hooked to support proxies. For example:
setup_call_cleanup( tcp_connect(Host:Port, StreamPair, []), talk(StreamPair), close(StreamPair))
If SocketId is an AF_UNIX socket (see unix_domain_socket/1), Address is an atom or string denoting a file name.
This hook is currently defined in Windows to map localhost
to
ip(127,0,0,1)
as resolving localhost
on Windows is often very
slow. Note that we do not want to do that in general as a system may
prefer to map localhost
to `::1`, i.e., the IPv6 loopback address.
:- multifile socket:tcp_connect_hook/4. socket:tcp_connect_hook(Socket, Address, Read, Write) :- proxy(ProxyAdress), tcp_connect(Socket, ProxyAdress), tcp_open_socket(Socket, Read, Write), proxy_connect(Address, Read, Write).
false
. If true
, do not attempt to use any
proxies to obtain the connectionfalse
. If true
, set nodelay on the
resulting socket using tcp_setopt(Socket, nodelay)
inet6
. When omitted we use host_address/2
with type(stream)
and try the returned addresses in order.The +,+,- mode is deprecated and does not support proxies. It behaves like tcp_connect/4, but creates a stream pair (see stream_pair/3).
select()
call
underlying wait_for_input/3. As input multiplexing typically happens
in a background thread anyway we accept the loss of timeouts and
interrupts.
The default implementation recognises the values for Proxy
described below. The library(http/http_proxy) adds
proxy(Host,Port)
which allows for HTTP proxies using the
CONNECT
method.
These correspond to the proxy methods defined by PAC Proxy auto-config. Additional methods can be returned if suitable clauses for http:http_connection_over_proxy/6 or try_proxy/4 are defined.
socket_create(SocketId, [type(dgram)])
or, explicit,
socket_create(SocketId, [domain(inet), type(dgram)])
.atom
, codes
,
string
(default) or term
(parse as Prolog term).octet
. iso_latin_1
, text
or utf8
.For example:
receive(Port) :- udp_socket(Socket), tcp_bind(Socket, Port), repeat, udp_receive(Socket, Data, From, [as(atom)]), format('Got ~q from ~q~n', [Data, From]), fail.
as(Type)
option of
udp_receive/4. The are interpreted differently though. No Type
corresponds to CVT_ALL of PL_get_chars(). Using atom
corresponds to CVT_ATOM and any of string or codes is mapped
to CVT_STRING|CVT_LIST, allowing for a SWI-Prolog string
object, list of character codes or list of characters.
Finally, term
maps to CVT_WRITE_CANONICAL. This implies that
arbitrary Prolog terms can be sent reliably using the option
list `[as(term)
,encoding(utf8)
])`, using the same option list
for udp_receive/4.For example
send(Host, Port, Message) :- udp_socket(S), udp_send(S, Message, Host:Port, []), tcp_close_socket(S).
A broadcast is achieved by using tcp_setopt(Socket, broadcast)
prior to sending the datagram and using the local network
broadcast address as a ip/4 term.
setsockopt()
and the socket interface (e.g.,
socket(7)
on Linux) for details.
tcp_socket(Socket), tcp_setopt(Socket, bindtodevice(lo))
true
, disable the Nagle optimization on this socket,
which is enabled by default on almost all modern TCP/IP
stacks. The Nagle optimization joins small packages, which is
generally desirable, but sometimes not. Please note that the
underlying TCP_NODELAY setting to setsockopt()
is not
available on all platforms and systems may require additional
privileges to change this option. If the option is not
supported, tcp_setopt/2 raises a domain_error exception. See
Wikipedia
for details.setsockopt()
with the
corresponding arguments.swipl-win.exe
executable) this flags defines whether or not any events are
dispatched on behalf of the user interface. Default is
true
. Only very specific situations require setting
this to false
.fcntl()
call. Currently only suitable to deal
switch stream to non-blocking mode using:
tcp_fcntl(Stream, setfl, nonblock),
An attempt to read from a non-blocking stream while there is no
data available returns -1 (or end_of_file
for read/1), but
at_end_of_stream/1 fails. On actual end-of-input,
at_end_of_stream/1 succeeds.
domain_error
exception.
inet
or inet6
to limit the results to the given
family.stream
or dgram
.true
(default false
), return the canonical host name
in the frist answerIn mode (+,-,+) Address is unified to a dict with the following keys:
inet
or inet6
. The underlying getaddrinfo()
calls
this family
. We use domain
for consistency with
socket_create/2.stream
or dgram
.canonname(true)
is specified on the first
returned address. Holds the official canonical host name.getaddrinfo()
and the
IP-number is unified to Address using a term of the format
ip(Byte1,Byte2,Byte3,Byte4)
. Otherwise, if Address is bound to an
ip(Byte1,Byte2,Byte3,Byte4)
term, it is resolved by gethostbyaddr()
and the canonical hostname is unified with HostName.
gethostname()
and return the canonical name
returned by getaddrinfo()
.ip(A,B,C,D)
and ip6 addresses as ip(A,B,C,D,E,F,H)
. For example:
?- ip_name(ip(1,2,3,4), Name) Name = '1.2.3.4'. ?- ip_name(IP, '::'). IP = ip(0,0,0,0,0,0,0,0). ?- ip_name(IP, '1:2::3'). IP = ip(1,2,0,0,0,0,0,3).
ip(A,B,C,D)
: port