Hi,
I am converting my RBaseXClient to a SWI-prolog version (and thanks to Jan, I already figured out how I can use dicts
as the main data structure to be used instead of the R object oriented classes).
I use this code to open a socketConnection to a BaseX-server (mostly copied from internet):
create_Socket(Host, Port, Username, Password, StreamPair) :-
atomic_list_concat([Host, Port], ':', HP_A),
term_to_atom(HP, HP_A),
setup_call_catcher_cleanup(tcp_socket(Socket),
tcp_connect(Socket, HP),
exception(_),
tcp_close_socket(Socket)),
setup_call_cleanup(tcp_open_socket(Socket, In, Out),
chat_to_server(In, Out),
close_connection(In, Out)).
In the example this code is intended to be used for reading from the socket:
chat_to_server(In, Out) :-
read(Term),
( Term == end_of_file
-> true
; format(Out, '~q .~n', [Term]),
flush_output(Out),
read(In, Reply),
write(Reply),
chat_to_server(In, Out)
).
In the debugger I can see that connection and streams are created.
According to the client server protocol, the first read operation after creating the socket should result in something like āBaseX:1369578179679ā. This realm and timestamp is used, together with username and password, for authenthication. And once authenticated,the socket can be used for all operations.
Executing read(Term)
in chat_to_server
however results in a loop and a creep...
message in the debugger.
From R I know that the socketconnection can be used in nonblocking mode.
I also tried using this predicate for reading but that didnāt make any difference:
readBin(StreamPair, Reply) :-
stream_pair(StreamPair, In, _),
read(In, Reply).
How can I read from a socket?
Thankds,
Ben