所以我目前正处于构建游戏的开始阶段,我正在使用 prolog 作为游戏的服务器端来对棋盘中的游戏进行验证检查。
我目前有两个问题。
一:似乎无法在不中止的情况下关闭服务器,从而使套接字保持打开状态。代码如下
服务器:
:- use_module(library(socket)).
create_server(Port) :-
tcp_socket(Socket),
tcp_bind(Socket, Port),
tcp_listen(Socket, 5),
tcp_open_socket(Socket, AcceptFd, _),
dispatch(AcceptFd).
dispatch(AcceptFd) :-
tcp_accept(AcceptFd, Socket, Peer),
thread_create(process_client(Socket, Peer), _,
[ detached(true)
]),
dispatch(AcceptFd).
process_client(Socket, _Peer) :-
setup_call_cleanup(tcp_open_socket(Socket, In, Out),
handle_service(In, Out),
close_connection(In, Out)).
close_connection(In, Out) :-
close(In, [force(true)]),
close(Out, [force(true)]).
handle_service(In, Out) :-
read(In, Int),
writeln(Int),
( Int == end_of_file
-> true
;
call_test(Int,Term),
format(Out, 'seen(~q).~n', [Term]),
flush_output(Out),
handle_service(In, Out)
).
call_test(test,Term):-Term = 'really test'.
call_test(validate(teste),Term):-
String = "validate(test)",
string_to_list(String,List),
read_from_chars(List,Stringf),
writeln(Stringf),
Term = 'foo'.
客户:
:- 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, In, Out),
chat_to_server(In, Out),
close_connection(In, Out)).
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),
%format('Reply: ~q.~n', [Reply]),
chat_to_server(In, Out)
).
close_connection(In, Out) :-
close(In, [force(true)]),
close(Out, [force(true)]).
我可以使用 ctrl+D(即 end_of_file)毫无问题地关闭客户端,但我的服务器没有关闭...它接收 end_of_file,它打印 end_of_file 但它没有关闭。即使将其直接插入服务器也不行。我究竟做错了什么?
二:我需要将一个字符串从 C++ 传递给 Swipl,其中包含要使用的谓词名称和参数。有人可以告诉我如何做到这一点,或者至少指出我正确的方向吗?
非常感谢你。