tcp: Network Communication Package

This package supplies the necessary primitives for network communication. This allows the user to take advantage of the computing power of a network of computers by allowing the construction of a set of cooperating processes running on different machines.

In general, the tcp package provides facilities to

This package implements a stream socket with the TCP protocol providing the underlying communication support. A stream socket provides for bidirectional, reliable, sequenced and unduplicated flow of data without record boundaries. Two other types of sockets, the datagram socket and the raw socket, are not used here. TCP stands for the Internet Transmission Control Protocol.

This library package is intended for network communication, however, it does not require that each process be on a separate machine. It can be used to establish connections and communicate with processes on the same machine in just the same way that it would establish connections and communicate with processes on other machines.

Here is a simple example of the kind of thing you can do with this package. The example is the producer-filter-consumer problem, each running as a separate process. The producer produces successive terms and passes them on to the filter. The filter reads successive terms from the producer and then either passes them on to the consumer or discards them. The consumer reads and echos the terms passed to it by the filter. It is taken from the example program IPC/TCP/demo/ce.pl.

     :-use_module(library(random)).
     :-use_module(library(tcp)).
     
     % on machine A we have the producer process:
     
     producer:-
         tcp_address_from_file(filter, Address),
         tcp_connect(Address, Filter),
         repeat,
             random(X),
             tcp_send(Filter, X),
         fail.
     
     % on machine B we have the filter process:
     
     filter:-
         tcp_create_listener(AddressA, _),
         tcp_address_to_file(filter, AddressA),
         tcp_address_from_file(consumer, AddressC),
         tcp_connect(AddressC, Consumer),
         repeat,
             tcp_select(term(_,X)),
             0.2 =< X, X < 0.7,
             tcp_send(Consumer, X),
         fail.
     
     % and on machine C we have the consumer process:
     
     consumer:-
         tcp_create_listener(Address, _),
         tcp_address_to_file(consumer, Address),
         repeat,
             tcp_select(term(_,X)),
             format('The filtered number: ~d~n', [X]),
         fail.
     

Footnotes

  1. Not available for C processes.