Network programming with Prolog (SWI)

Network programming is an important part of the development process. Nowadays, a good programming language must ease network programming.

The Prolog programming language includes this feature. In fact, as I’m working with SWI-Prolog, I know this feature is (well) covered by this Prolog distribution but I suppose that other distributions also give tools for network programming in Prolog.

Network

SWI-Prolog’s network programming module is a binding of the C POSIX API so its use is quite similar.

Here is some predicates of the socket library.

  • tcp_socket(-SocketId)
  • tcp_close_socket(+SocketId)
  • tcp_open_socket(+SocketId, -InStream, -OutStream)
  • tcp_bind(+Socket, ?Port)
  • tcp_listen(+Socket, +Backlog)
  • tcp_accept(+Socket, -Slave, -Peer)
  • tcp_connect(+Socket, +Host:+Port)
  • udp_socket(-Socket)
  • udp_receive(+Socket, -Data, -From, +Options)
  • udp_send(+Socket, +Data, +To, +Options)

For tcp sockets, you can simply use the write* and read* predicates.

To use those functions, sometimes you’ll have to put :

?View Code PROLOG
1
:- use_module(library(socket)).

at the beggining of your Prolog source file.

Here is a sample Prolog program that creates a server. When a client gets connected to the server, it sends it Welcome on myPrologServer and then closes the connection.
<

?View Code PROLOG
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
:- use_module(library(socket)).
 
init :-
	create_server(2302,'Welcome on myPrologServer\n').
 
create_server(Port,Txt) :-
	tcp_socket(Socket),
	tcp_bind(Socket,Port),
	tcp_listen(Socket,5),
	tcp_open_socket(Socket,AcceptFd,_),
	server_loop(AcceptFd,Txt).
 
server_loop(AcceptFd,Txt) :-
	tcp_accept(AcceptFd,Socket2,_),
	tcp_open_socket(Socket2,In,Out),
	write(Out,Txt),
	close(In),
	close(Out),
	server_loop(AcceptFd,Txt).

The code is quite short and is very easy to understand.

Now, launch swipl, consult your file (test_socket.pl for me) and launch the init predicate.

?View Code CONSOLE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ swipl
Welcome to SWI-Prolog (Multi-threaded, Version 5.6.47)
Copyright (c) 1990-2007 University of Amsterdam.
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
 
For help, use ?- help(Topic). or ?- apropos(Word).
 
?- consult('test_socket.pl').
%     library(error) compiled into error 0.01 sec, 7,508 bytes
%    library(lists) compiled into lists 0.02 sec, 19,312 bytes
%   library(shlib) compiled into shlib 0.02 sec, 34,280 bytes
%  library(socket) compiled into socket 0.02 sec, 40,536 bytes
% test_socket.pl compiled 0.02 sec, 41,872 bytes
 
Yes
?- init.

Then, your server is waiting for connections.
I used telnet to connect to my server :

?View Code CONSOLE
1
2
3
4
5
6
7
$ telnet
telnet> open localhost 2302
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Welcome on myPrologServer
Connection closed by foreign host.

You can see that everything works fine.

Using both the socket module and the multithreading module of SWI-Prolog will let you create strong and efficient servers/clients and show everybody that nothing is impossible in Prolog ;-)

The documentation of the network module is included in SWI-Prolog’s C-library’s one, at the following URL : http://www.swi-prolog.org/packages/clib.html#sec:5.

You have now everything needed to write Prolog programs interacting with any servers/clients in the world using the SWI-Prolog C-library’s socket module !


Leave a Comment