next up previous contents index
Next: Arrays Up: Socket I/O Previous: Timeouts.   Contents   Index

Buffered, stream-oriented communication.

In Unix, socket descriptors can be ``promoted'' to file streams and the regular read/write commands can be used with such streams. In XSB, such promotion can be done using the following predicate:
fd2ioport(+Pipe, -IOport)
shell
Take a socket descriptor and convert it to an XSB I/O port that can be used for regular file I/O.
Once IOport is obtained, all I/O primitives described in Section 1.5 can be used. This is, perhaps, the easiest and the most convenient way to use sockets in XSB. (This feature has not been implemented for Windows.)

Here is an example of the use of this feature:

:- compiler_options([xpp_on]).
#include "socket_defs_xsb.h"

?- (socket(Sockfd, SOCK_OK)
   ->   socket_connect(Sockfd1, 6020, localhost, Ecode),
        (Ecode == SOCK_OK
        -> fd2ioport(Sockfd, SockIOport),
           file_write(SockIOport, 'Hello Server!')
        ;  writeln('Can''t connect to server')
        ),
    ;   writeln('Can''t open socket'), fail
    ).



Luis Fernando P. de Castro 2003-06-27