The socket interface

Author(s): Manuel Carro, Daniel Cabeza.

This module defines primitives to open sockets, send, and receive data from them. This allows communicating with other processes, on the same machine or across the Internet. The reader should also consult standard bibliography on the topic for a proper use of these primitives.

Documentation on exports

Usage:connect_to_socket_type(Hostname,Port,Type,Stream)

Returns a Stream which connects to Hostname. The Type of connection can be defined. A Stream is returned, which can be used to write/2 to, to read/2, to socket_send/3 to, or to socket_recv/3 from the socket.

Usage:connect_to_socket(Hostname,Port,Stream)

Calls connect_to_socket_type/4 with SOCK_STREAM connection type. This is the connection type you want in order to use the write/2 and read/2 predicates (and other stream IO related predicates).

PREDICATEbind_socket/3

Usage:bind_socket(Port,Length,Socket)

Returns an AF_INET Socket bound to Port (which may be assigned by the OS or defined by the caller), and listens to it (hence no listen call in this set of primitives). Length specifies the maximum number of pending connections.

Usage:socket_accept(Sock,Stream)

Creates a new Stream connected to Sock.

Usage:select_socket(Socket,NewStream,TO_ms,Streams,ReadStreams)

Wait for data available in a list of Streams and in a Socket. Streams is a list of Prolog streams which will be tested for reading. Socket is a socket (i.e., an integer denoting the O.S. port number) or a free variable. TO_ms is a number denoting a timeout. Within this timeout the Streams and the Socket are checked for the availability of data to be read. ReadStreams is the list of streams belonging to Streams which have data pending to be read. If Socket was a free variable, it is ignored, and NewStream is not checked. If Socket was instantiated to a port number and there are connections pending, a connection is accepted and connected with the Prolog stream in NewStream.

PREDICATEsocket_send/3

Usage:socket_send(Stream,Bytes,Sent)

Sends Bytes to the socket associated to Stream, return in Sent the number of sent bytes. The socket has to be in connected state.

Usage:socket_sendall(Stream,Bytes)

Sends all Bytes to the socket associated to Stream. The socket has to be in connected state.

Usage:socket_send_stream(Stream,FromStream)

Sends all bytes from stream FromStream to the socket associated to Stream. The socket has to be in connected state. FromStream cannot be a socket stream

PREDICATEsocket_recv/3

Usage:socket_recv(Stream,Bytes,Length)

Receives a byte list Bytes from the socket associated to Stream, and returns its Length. For TCP, Length is 0 if the peer has performed an orderly shutdown on the socket.

Usage:socket_shutdown(Stream,How)

Shut down a duplex communication socket with which Stream is associated. All or part of the communication can be shutdown, depending on the value of How. The atoms read, write, or read_write should be used to denote the type of closing required.

Usage:hostname_address(Hostname,Address)

Address is unified with the atom representing the address (in AF_INET format) corresponding to Hostname.

Usage:socket_getpeername(Stream,Address)

Address is unified with the atom representing the address (in AF_INET or AF_INET6 format) of the peer connected to the socket associated to Stream.

Defines the atoms which can be used to specify the socket type recognized by connect_to_socket_type/4. Defined as follows:
socket_type(stream).
socket_type(dgram).
socket_type(raw).
socket_type(seqpacket).
socket_type(rdm).

Usage:socket_type(T)

T is a valid socket type.

    Usage:shutdown_type(T)

    T is a valid shutdown type.

      Documentation on imports

      This module has the following direct dependencies: