|Did you know ...||Search Documentation:|
|Socket predicate reference|
stream(default) to create a TCP connection or
dgramto create a UDP socket.
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
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.
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_unixif 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
ip(127,0,0,1) as resolving
Windows is often very slow. Note that we do not want to do that in
general as a system may prefer to map
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).
tcp_connect(+Address, -StreamPair, +Options).
true, do not attempt to use any proxies to obtain the connection
true, set nodelay on the resulting socket using
inet6. When omitted we use host_address/2 with
type(stream)and try the returned addresses in order.
|Address||is either a Host:Port term or a file name (atom or string). The latter connects to an AF_UNIX socket and requires unix_domain_socket/1.|
proxy_error(tried(ResultList))is raised by mode (+,-,+) if proxies are defines by proxy_for_url/3 but no proxy can establsh the connection. ResultList contains one or more terms of the form
false(Proxy)for a hook that simply failed or
error(Proxy, ErrorTerm)for a hook that raised an exception.
library(http/http_proxy)defines a hook that allows to connect through HTTP proxies that support the
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.
library(http/http_open)) collect the results of failed proxies and raise an exception no proxy is capable of realizing the connection.
The default implementation recognises the values for Proxy
described below. The
proxy(Host,Port) which allows for HTTP proxies using the
socket_create(SocketId, [type(dgram)])or, explicit,
socket_create(SocketId, [domain(inet), type(dgram)]).
term(parse as Prolog term).
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,
termmaps to CVT_WRITE_CANONICAL. This implies that arbitrary Prolog terms can be sent reliably using the option list‘[
encoding(utf8)])`, using the same option list for udp_receive/4.
send(Host, Port, Message) :- udp_socket(S), udp_send(S, Message, Host:Port, ), tcp_close_socket(S).
A broadcast is achieved by using
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.exeexecutable) 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
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),
at_end_of_stream/1 fails. On actual
inet6to limit the results to the given family.
false), return the canonical host name in the frist answer
In mode (+,-,+) Address is unified to a dict with the following keys:
inet6. The underlying
family. We use
domainfor consistency with socket_create/2.
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
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 = '188.8.131.52'. ?- 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).
socks_error(Details)if the SOCKS negotiation failed.