Go to the previous, next section.

Socket IO

This library package defines a number of predicates manipulating sockets. They are all rather straight-forward interfaces to the corresponding C-library predicates with the same name (except current_host/1). The reader should therefore study the C-manual for a deeper description.

The Domain, is either the atom 'AF_INET' or 'AF_UNIX'. They correspond directly to the same domains in UNIX.

An Address is either 'AF_INET'(Host,Port) or 'AF_UNIX'(SocketName). Host is an atomic hostname, Port is a portnumber and SocketName is an atom denoting a socket. A reader familiar with UNIX's sockets will understand this immediately.

All streams below can be both read from and written on. All IO-predicates operating on streams can be used, for example read/2, write/2, format/3, current_stream/3, etc. Socket streams are unbuffered on reads and block buffered on writes.

To load the package, enter the query

| ?- use_module(library(sockets)).

@
A socket Socket in the domain Domain is created.

@
@
The socket Socket is bound to the address. If Port is uninstantiated, the operative system picks a port number to which Port is bound.

@
@
The socket Socket is connected to the address. Stream is a special stream on which items can be both read and written.

@
The socket Socket is defined to have a maximum backlog queue of Length pending connections.

@
The first connection to socket Socket is extracted. The stream Stream stream is opened for read and write on this connection.

@
The list of streams in Streams is checked for readable characters. A stream can be any stream associated with an I/O descriptor. The list ReadStreams returns the streams with readable data. If TimeOut is instantiated to off, the predicate waits until something is available. If TimeOut is S:U the predicate waits at most S seconds and U microseconds. Both S and U must be integers >=0. If there is a timeout, ReadStreams is []. If Socket is instantiated, socket_select/5 also waits for connections to this socket. If a connection is found, NewStream will be a stream for communicating with the connecting process.

Note that instead of this predicate the built-in stream_select/3 must be used in all cases when Socket is not used.

@
HostName is unified with the fully qualified name of the machine the process is executing on. The call will also succeed if HostName is instantiated to the unqualified name of the machine.

Go to the previous, next section.