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

PREDICATE

(True) Usage:connect_to_socket_type(Host,Port,Type,Stream)

Returns a Stream which connects to Host. 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/2 to, or to socket_recv/2 from the socket.

  • Calls should, and exit will be compatible with:
    (term_typing:atom/1)Host is currently instantiated to an atom.
    (basic_props:int/1)Port is an integer.
    (sockets:socket_type/1)Type is a valid socket type.
    (streams_basic:stream/1)Stream is an open stream.
  • The following properties should hold at call time:
    (term_typing:nonvar/1)Host is currently a term which is not a free variable.
    (term_typing:nonvar/1)Port is currently a term which is not a free variable.
    (term_typing:nonvar/1)Type is currently a term which is not a free variable.
    (term_typing:var/1)Stream is a free variable.
  • The following properties hold globally:
    (foreign_interface_properties:foreign_low/2)The Prolog predicate PrologName is implemented using the function ForeignName. The same considerations as above example are to be applied.

PREDICATE

Usage:connect_to_socket(Host,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).

  • Call and exit should be compatible with:
    (basic_props:atm/1)Host is an atom.
    (basic_props:int/1)Port is an integer.
    (streams_basic:stream/1)Stream is an open stream.
  • The following properties should hold at call time:
    (term_typing:nonvar/1)Host is currently a term which is not a free variable.
    (term_typing:nonvar/1)Port is currently a term which is not a free variable.
    (term_typing:var/1)Stream is a free variable.

PREDICATE

(True) Usage:bind_socket(Port,Length,Socket)

Returs 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.

  • Calls should, and exit will be compatible with:
    (basic_props:int/1)Port is an integer.
    (basic_props:int/1)Length is an integer.
    (basic_props:int/1)Socket is an integer.
  • The following properties should hold at call time:
    (term_typing:nonvar/1)Length is currently a term which is not a free variable.
    (term_typing:var/1)Socket is a free variable.
  • The following properties hold globally:
    (foreign_interface_properties:foreign_low/2)The Prolog predicate PrologName is implemented using the function ForeignName. The same considerations as above example are to be applied.

PREDICATE

(True) Usage:socket_accept(Sock,Stream)

Creates a new Stream connected to Sock.

  • Calls should, and exit will be compatible with:
    (basic_props:int/1)Sock is an integer.
    (streams_basic:stream/1)Stream is an open stream.
  • The following properties should hold at call time:
    (term_typing:nonvar/1)Sock is currently a term which is not a free variable.
    (term_typing:var/1)Stream is a free variable.
  • The following properties hold globally:
    (foreign_interface_properties:foreign_low/2)The Prolog predicate PrologName is implemented using the function ForeignName. The same considerations as above example are to be applied.

PREDICATE

(True) 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.

  • Calls should, and exit will be compatible with:
    (basic_props:int/1)Socket is an integer.
    (streams_basic:stream/1)NewStream is an open stream.
    (basic_props:int/1)TO_ms is an integer.
    (basic_props:list/2)Streams is a list of streams.
    (basic_props:list/2)ReadStreams is a list of streams.
  • The following properties should hold at call time:
    (term_typing:nonvar/1)Socket is currently a term which is not a free variable.
    (term_typing:var/1)NewStream is a free variable.
    (term_typing:nonvar/1)TO_ms is currently a term which is not a free variable.
    (term_typing:nonvar/1)Streams is currently a term which is not a free variable.
    (term_typing:var/1)ReadStreams is a free variable.
  • The following properties hold globally:
    (foreign_interface_properties:foreign_low/2)The Prolog predicate PrologName is implemented using the function ForeignName. The same considerations as above example are to be applied.

PREDICATE

(True) Usage:socket_send(Stream,String)

Sends String to the socket associated to Stream. The socket has to be in connected state. String is not supposed to be NULL terminated, since it is a Prolog string. If a NULL terminated string is needed at the other side, it has to be explicitly created in Prolog.

  • Calls should, and exit will be compatible with:
    (streams_basic:stream/1)Stream is an open stream.
    (basic_props:string/1)String is a string (a list of character codes).
  • The following properties should hold at call time:
    (term_typing:nonvar/1)Stream is currently a term which is not a free variable.
    (term_typing:nonvar/1)String is currently a term which is not a free variable.
  • The following properties hold globally:
    (foreign_interface_properties:foreign_low/2)The Prolog predicate PrologName is implemented using the function ForeignName. The same considerations as above example are to be applied.

PREDICATE

(True) Usage:socket_recv_code(Stream,String,Length)

Receives a String from the socket associated to Stream, and returns its Length. If Length is -1, no more data is available.

  • Calls should, and exit will be compatible with:
    (streams_basic:stream/1)Stream is an open stream.
    (basic_props:string/1)String is a string (a list of character codes).
    (basic_props:int/1)Length is an integer.
  • The following properties should hold at call time:
    (term_typing:nonvar/1)Stream is currently a term which is not a free variable.
  • The following properties hold globally:
    (foreign_interface_properties:foreign_low/2)The Prolog predicate PrologName is implemented using the function ForeignName. The same considerations as above example are to be applied.

PREDICATE

Usage:socket_recv(Stream,String)

As socket_recv_code/3, but the return code is ignored.

  • Call and exit should be compatible with:
    (streams_basic:stream/1)Stream is an open stream.
    (basic_props:string/1)String is a string (a list of character codes).
  • The following properties should hold at call time:
    (term_typing:nonvar/1)Stream is currently a term which is not a free variable.

PREDICATE

(True) 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.

  • Calls should, and exit will be compatible with:
    (streams_basic:stream/1)Stream is an open stream.
    (sockets:shutdown_type/1)How is a valid shutdown type.
  • The following properties should hold at call time:
    (term_typing:nonvar/1)Stream is currently a term which is not a free variable.
    (term_typing:nonvar/1)How is currently a term which is not a free variable.
  • The following properties hold globally:
    (foreign_interface_properties:foreign_low/2)The Prolog predicate PrologName is implemented using the function ForeignName. The same considerations as above example are to be applied.

PREDICATE

(True) Usage:hostname_address(Hostname,Address)

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

  • Calls should, and exit will be compatible with:
    (basic_props:atm/1)Hostname is an atom.
    (basic_props:atm/1)Address is an atom.
  • The following properties should hold at call time:
    (term_typing:nonvar/1)Hostname is currently a term which is not a free variable.
  • The following properties hold globally:
    (foreign_interface_properties:foreign_low/2)The Prolog predicate PrologName is implemented using the function ForeignName. The same considerations as above example are to be applied.

REGTYPE
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.

    REGTYPE

    Usage:shutdown_type(T)

    T is a valid shutdown type.