Subject: [SWIPL] wait_for_input BUG!

From: "Leonid V. Khramov" <[email-masked]>
Date: Mon, 24 Sep 2001 19:09:28 +0400 (MSD)

Hi!

It seems, that there is a bug in wait_for_input/3.
Prolog make buffering of stream include tcp stream and wait_for_input
doesn't check that memory buffers, the result is continious waiting
even when data is ready on stream (really inside the prolog buffer)!!!
This is a bug.

I wrote two small programs tcp_test_cln.pl - client and
tcp_test_srv.pl - server for demonstrate this bug.

Server listen socket and wait for input with 5 sec timeout. If data is
ready it make get_char and print it, then wait again.

Client writes to server every 15 sec. 
It writes whole string with \n, not one byte.

tcp_test_cln.pl

:-use_module([library(socket)]).

sequence('1234567890').
sequence('qwertyuiop').

our_host_port(localhost:45555).

client_create(Host:Port,S/R/W) :-
	tcp_socket(S),
	tcp_connect(S,Host:Port),
	tcp_open_socket(S,R,W).

main:-
	our_host_port(Host),
	client_create(Host,_/_/W),
	writef("Client connected to server at %w\n",[Host]),
	repeat,
	sequence(Seq),
	writef("Sending [%w] to server\n",[Seq]),
	write(W,Seq),
	nl(W),
	flush_output(W),
	sleep(15), %Sleeping 30 sec
	fail.

tcp_test_srv.pl

:-use_module([library(socket)]).

our_port(45555).

server_create(Port,S/R) :-
	tcp_socket(S),
	(   catch((
		tcp_bind(S,Port),
		   tcp_listen(S,2),
		   tcp_open_socket(S,R,_)
		  ),
		  _,
		  fail
		 )->
	    true
	;   
	    tcp_close_socket(S),
	    writef("unable open server socket %w on port %w",[S,Port]),
	    !,fail
	),
	!.

main:-
	our_port(Port),
	server_create(Port,_/AcceptFD),
	writef("Listen client on port %w\n",[Port]),
	tcp_accept(AcceptFD,Socket,_),
	tcp_open_socket(Socket,R,W),
	writef("Accepted connection: %w/%w/%w\n",[Socket,R,W]),!,
	repeat,
	wait_for_input([R],OutList,5),
	(OutList==[]->
	    write('Timed out\n')
	;
	    get_char(R,Ch),
	    writef("Received char [%w]\n",[Ch]),
	    (Ch==end_of_file->
		halt
	    ;
		true
	    )
	),
	fail.

results on my Linux box with pl-4.0.9:

client:

Welcome to SWI-Prolog (Version 4.0.9)
Copyright (c) 1990-2000 University of Amsterdam. 
Copy policy: GPL-2 (see www.gnu.org)

For help, use ?- help(Topic). or ?- apropos(Word).

?- [tcp_test_cln].
%   library(shlib) compiled into shlib 0.01 sec, 9,128 bytes
%  library(socket) compiled into socket 0.03 sec, 11,760 bytes
% tcp_test_cln compiled 0.03 sec, 14,624 bytes

Yes
?- main.
Client connected to server at localhost:45555
Sending [1234567890] to server
Sending [qwertyuiop] to server

server:

Welcome to SWI-Prolog (Version 4.0.9)
Copyright (c) 1990-2000 University of Amsterdam. 
Copy policy: GPL-2 (see www.gnu.org)

For help, use ?- help(Topic). or ?- apropos(Word).

?- [tcp_test_srv].
%   library(shlib) compiled into shlib 0.01 sec, 9,128 bytes
%  library(socket) compiled into socket 0.02 sec, 11,760 bytes
% tcp_test_srv compiled 0.03 sec, 15,448 bytes

Yes
?- main.
Listen client on port 45555
Accepted connection: $socket(4)/$stream(213358)/$stream(291100)
Received char [1]
Timed out
Timed out
Timed out
Received char [2]
Received char [3]
Received char [4]
Received char [5]
Received char [6]
Received char [7]
Received char [8]
Received char [9]
Received char [0]
Received char [
]
Received char [q]
Timed out
Timed out
Timed out

I hope this help to fix the bug.

-- 
Best regards,
Leo <[email-masked]>
Solvo Ltd. 
St.Petersburg, Russia