-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.pl
36 lines (29 loc) · 939 Bytes
/
socket.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Sockets %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:-use_module(library(sockets)).
port(60070).
% launch me in sockets mode
server:-
port(Port),
socket_server_open(Port, Socket),
socket_server_accept(Socket, _Client, Stream, [type(text)]),
write('Accepted connection'), nl,
serverLoop(Stream),
socket_server_close(Socket).
% wait for commands
serverLoop(Stream) :-
repeat,
read(Stream, ClientMsg),
write('Received: '), write(ClientMsg), nl,
parse_input(ClientMsg, MyReply),
format(Stream, '~q.~n', [MyReply]),
write('Wrote: '), write(MyReply), nl,
flush_output(Stream),
(ClientMsg == quit; ClientMsg == end_of_file), !.
parse_input(comando(Arg1, Arg2), Answer) :-
comando(Arg1, Arg2, Answer).
parse_input(quit, ok-bye) :- !.
comando(Arg1, Arg2, Answer) :-
write(Arg1), nl, write(Arg2), nl,
Answer = 5.