Class socket
provides an interface to Unix sockets. A Unix socket is a (network)
communication end-point. Normally one process creates a socket at a
specified address (see below). Other processes may use a socket to socket->connect
to the created socket. After the connection has been established, a
two-way text-based communication channel is available.
Below is a first, very simple example:
?- new(X, socket('~/xpce-socket')),
send(X, input_message, message(@pce, write, @arg1)),
send(X, listen).
In another xpce process running on the same machine we can now do
?- new(X, socket('~/xpce-socket')),
send(X, connect),
send(X, format, 'Hello There\n'),
send(X, close).
Sockets addresses may be internet addresses as (allowing for
communication between machines) and unix-domain addresses, allowing for
communication between processes running on the same machine. See also
socket->initialise.
We distinguish between server sockets and client
sockets. After the connection has been established there is no
difference between the two. Below we describe the process to establish a
connection:
->initialise.
The stream->input_message
is called whenever there is input on the socket; the
socket->accept_message
is called whenever a client makes an attempt to socket->connect
to this socket.
->listen.
This will make the socket listen to requests from other processes.
->connect.
Provided the address is right this will cause the server socket to start socket->accept.
socket->accept will object<-clone
the server socket and run the
socket->accept_message
with @receiver
bound the the server socket and @arg1
bound to the clone. After this, a communication channel exists between
the cloned server and the client socket. Data is sent to the socket
using
stream->append
or stream->format. Whenever data is
available,
stream->input_message
is called to handle this data. Class process
handles input from its inferior process using the same mechanism.
The Unix program xpce-client(l) may be used to connect
to xpce-processes. See also telnet(1). The sources of
xpce-client may be found in xpce/src/unx-client.c.
They may be used as a starting point for other unix programs that wish
to communicate to xpce.
See also class stream.
The library file pce_server defines pce_server/1
to create a server mode.
@receiver The original socket @arg1 The object <-clone’d socket for the new connection.
This message may be used to deny the client access by object->free’ing
the new socket (@arg2).
See also stream->input_message.
|tuple|int*->listen
call will replace the socket<-address
with the actually used port number.@receiver = the socket @arg1 = a new string object holding the data
See also stream<-read_line.
Defaults: The default input_message is @nil.
In this case data may only be read using stream<-read_line.
<-status
of a socket is idle. A server socket waiting for
connections has status listen. An cloned server socket
handling a connection has status accepted and finally, an
open client socket has socket<-status connected.
See also socket->listen,
and socket->connect.
new(S, socket(File)), send(S, connect), ...
To connect to an inet domain socket, use:
new(S, socket(tuple(Host, Port))), send(S, connect), ...
socket->connect
is a no-op if socket<-status
is already connected.
<-status:
accepted (i.e. it is a clone of a server
socket handling a connection), the default is to object->free
the socket object.|tuple|int*,
domain=[{unix,inet}]<-address.
Unix-domain sockets appear as an entry in the filesystem and are only supported on Unix systems.
After a socket has been created and the appropriate attributes
(notably
stream->input_message)
have been specified the socket is activated using
socket->listen
for server sockets and socket->connect
for client sockets.
->accept_message
when specified. The second argument is the backlog. It
specifies how many pending requests for connections are tolerated. Any
new connect request will be denied. The default is 5.
If the reuse argument is @on,
the system tries to reuse the specified port, dispite it still being in
use. This option is needed to restart a service at the same -well known-
port after an dirty shut-down. If you are security minded, check the
internet on setsockopt() for the option SO_REUSEADDR before
activating this option.
If an inet domain socket was created with its port specified as 0,
the system will select an available port. The actually used port can be
requested from the socket
object using socket<-address
after this method completed successfully.
|tuple->connect
(client) or is a cloned socket from a socket in socket->listen
mode (server, see socket<-clients and socket->accept_message).
If the socket is in the Unix socket<-domain,
the return value is a name describing the file (which will be the same
as the absolute
path-name of the file used to create the
socket).
If the socket is in the inet socket<-domain,
the return value is a
tuple object holding a
name describing the internet address of the socket at the other end in
decimal numeric notation and an integer describing the port.