[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: UDP Sockets
From: |
Daniel Diaz |
Subject: |
Re: UDP Sockets |
Date: |
Fri, 12 Oct 2001 14:14:44 +0200 |
Hi All,
Since the are (were) some questions about sockets, I enclose a piece of
code managing a client and a server.
The client finds (several) solutions to a given problem and sends them
to the server (which simply displays them). You can modify the host and
the port number used (here localhost:7000). This is obviously a very
simple example but should be a good starting point for more complex
applications.
Predicates provided:
server/0 starts the server (should be done before starting the client),
waits for the client, and then receive the terms and displays them, at
the end the server-side communication.
client_init/0 initializes the communication with the server (on the
other process, server/0 should been launched).
client_send/1 sends the term passed to the server.
client_term/0 closes the client-side communication.
How to use them:
start a gprolog process (called p1), load this file (called x.pl) and
start the server:
$ gprolog
GNU Prolog 1.2.8
By Daniel Diaz
Copyright (C) 1999-2001 Daniel Diaz
| ?- [x].
compiling /home/diaz/x.pl for byte code...
/home/diaz/x.pl compiled, 1550 lines read - 127107 bytes written, 561 ms
(100 ms) yes
| ?- server.
here the server waits for a connection...
start another gprolog process (call p2), load x.pl and start the client.
$ gprolog
GNU Prolog 1.2.8
By Daniel Diaz
Copyright (C) 1999-2001 Daniel Diaz
| ?- [x].
ccompiling /home/diaz/x.pl for byte code...
/home/diaz/x.pl compiled, 1550 lines read - 127107 bytes written, 455 ms
(100 ms) yes
| ?- client_init.
(10 ms) yes
this awakes p1 which displays:
accepting a connection from client : 127.0.0.1
then send some terms to the server, e.g. typing in p2:
| ?- client_send(foo).
yes
and p1 displays:
foo
repeat this as long as needed and then, stop the client (in p2):
| ?- client_term.
yes
this finished server/0 in p1 which then displays:
(10 ms) yes
| ?-
you can restart the server in p1.
| ?- server.
here the server waits for a connection...
and send all operators to p1, typing in p2:
| ?- client_init, current_op(X,Y,Z), client_send(op(X,Y,Z)), fail ;
client_term.
(10 ms) yes
and in p1 all operators are displayed:
accepting a connection from client : 127.0.0.1
op(1200,fx,:-)
op(1200,xfx,:-)
op(700,xfx,\=)
op(700,xfx,=:=)
op(700,xfx,#>=)
op(700,xfx,#<#)
op(700,xfx,@>=)
op(1200,xfx,-->)
...
op(700,xfx,#<)
op(700,xfx,@<)
yes
Here is a the code (file x.pl)
server :-
socket('AF_INET', Sock),
socket_bind(Sock, 'AF_INET'(localhost, 7000)),
socket_listen(Sock, 4),
socket_accept(Sock, C, SI, SO),
close(SO),
format('accepting a connection from client : ~a~n', [C]),
repeat,
read(SI, T),
( T \== end_of_file ->
write(T), nl,
fail
; !),
close(SI),
socket_close(Sock).
client_init :-
socket('AF_INET',Socket),
socket_connect(Socket,'AF_INET'(localhost,7000), SI, SO),
close(SI),
add_stream_alias(SO, out),
set_stream_buffering(out, line).
client_send(T):-
format(out, '~q.~n',[T]).
client_term :-
close(out).
Hope this helps.
PS: Carlos, in your example:
- initialize the server as above
- initialize the client before starting go/2,
- define client(X,Y) :- client_send(sol(X,Y)).
- call client_term at the end.
- UDP Sockets, c.calderon, 2001/10/10
- Re: UDP Sockets,
Daniel Diaz <=