src/swipl-devel/packages/xpce/man/refmanual/md/socket.md

class socket {#class-socket}

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 ->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 ->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:

  1. Create a server socket. The address is either a file or a port number. See ->initialise. The ->input_message is called whenever there is input on the socket; the ->accept_message is called whenever a client makes an attempt to ->connect to this socket.

  2. Invoke ->listen. This will make the socket listen to requests from other processes.

  3. (In the other process) Create a client socket. The address is either the same file as created by the server or a tuple consisting of an internet node name and a port number.

  4. Invoke ->connect. Provided the address is right this will cause the server socket to start ->accept. ->accept will <-clone the server socket and run the ->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 ->append or ->format. Whenever data is available, ->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.

Instance variables {#class-socket-instvars}

Send methods {#class-socket-send}

Get methods {#class-socket-get}



Try the rswipl package in your browser

Any scripts or data that you put into this service are public.

rswipl documentation built on June 16, 2026, 5:07 p.m.