Using Prolog streams

Sometimes the format of data being exchanged between processes is not known in advance and it is not possible to assume that the data sent are valid Prolog terms. This package provides Prolog streams for each connection that can be read from or written to using the stream input/output predicates. For more information about Prolog streams, see ref-iou-str.

Although these streams can be written to or read from using the standard input/output predicates supplied by Prolog, they must be closed using tcp_shutdown/1 instead of close/1, otherwise the database internal to library(tcp) will become inconsistent.

There is a subtle point about end_of_file: in a correct implementation of TCP it is possible to receive zero-length packets, so that a socket should be able to signal end-of-file repeatedly just like a terminal. So does end_of_file(Socket) mean "connection lost to Socket", or does it mean "zero-length packet received from Socket"? Using tcp_select/1 or tcp_select/2, end_of_file causes the connection to be shutdown. Reading the streams yourself lets you decide what end_of_file means on your sockets.