Greetings all;
I am working with Prolog and Python socket communication using TCP protocol. I have made a Python server that gets request and message from the Prolog client and reverts with a message to the Prolog Client. The Python side is working as desired, however on the Prolog side (client); I want to first read the message received (on In stream) and depending on its value (server sends an array basically) I want to send a message back to the Python server (Out stream): The concerned code is as follows:
:- use_module(library(socket)).
:- use_module(library(streampool)).
create_client(Host, Port) :-
setup_call_catcher_cleanup(tcp_socket(Socket),
tcp_connect(Socket, Host:Port),
exception(_),
tcp_close_socket(Socket)),
setup_call_cleanup(tcp_open_socket(Socket, Stream),
interact(Stream),
close_connection(Stream)).
interact(Stream) :-
revert_to_server(Stream), % this sends reply back to server
read_string(Stream,L,X),
split_string(X, ",", "", List),
nth0(0, List, Ps0),
nth0(7, List, Ps7),
writeln(Ps0),
writeln(Ps7).
interact(Stream) :-
print_message(warning,'chat failed'),!.
revert_to_server(Stream):-
format(Stream,'~q',Hi),
flush_output(Stream).
close_connection(Stream) :-
close(Stream, [force(true)]).
The aforementioned code works, however, here first the message is sent to server (using revert_to_server) and then the message received from the server is read. I want the other way round. The issue is, when I move the ‘revert_to_server’ clause at the end of the ‘interact’ predicate (after ‘writeln(Ps7)’), then the predicate just doesn’t work. I searched a lot but couldn’t find any explanation or to do what I want. I somehow feel that flushing the stream in ‘revert_to_server’ destroys it and doesn’t allow it to be used further. In that case, I even tested it with two separate streams (In and Out) but that didn’t work as well. Please aid over the matter.
Thanking you in anticipation.