Next: 1.9 Arrays
Up: 1.8 Socket I/O
Previous: 1.8.0.0.6 Timeouts.
Contents
Index
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 normal I/O primitives can be used by
specifying the IOport as their first argument. 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
).
Terrance Swift
2007-10-06