From jan@swi.psy.uva.nl Tue Jun 26 22:27:14 2001
Received: from gollem.swi.psy.uva.nl (root@gollem [145.18.152.30])
	by swi.psy.uva.nl (8.11.2/8.11.2) with ESMTP id f5QKREc24720;
	Tue, 26 Jun 2001 22:27:14 +0200 (MET DST)
Received: (from jan@localhost)
	by gollem.swi.psy.uva.nl (8.11.2/8.11.2/SuSE Linux 8.11.1-0.5) id f5QKR9a32277;
	Tue, 26 Jun 2001 22:27:09 +0200
Date: Tue, 26 Jun 2001 22:27:09 +0200
Message-Id: <200106262027.f5QKR9a32277@gollem.swi.psy.uva.nl>
From: Jan Wielemaker <jan@swi.psy.uva.nl>
Subject: Re: [SWIPL] sockets: interrupting a server
To: Paul Singleton <p.singleton@keele.ac.uk>,
   SWI Prolog <prolog@swi.psy.uva.nl>
In-Reply-To: Paul Singleton's message of Tue, 26 Jun 2001 17:09:20 +0100
Phone: +31 - 20 - 525 6121

Paul,

> Summary: how can I make my library(socket) application interruptible?
> 
> I'm using library(socket) (on Windows NT4) to implement a simple Prolog
> server: it runs in blocking mode, waits for a connection request, then
> elaborates (determinate) queries until the connection is closed by the
> client, whereupon it listens again ad infinitem.
> 
> Unfortunately it's very hard to kill!
> 
>   Waiting for Prolog.  Close again to force termination ..
> 
>   Main task is not responding.  Click "OK" to terminate it
> 
> Eventually it goes, but there's still a 'plwin.exe' process lurking,
> which continues to hog the port.  I have to "End Process" from Task
> Manager.  I haven't tried it, but I guess a Windows shutdown would hang.
> 
> Is this a problem with Windows and blocking mode?

Certainly.  In the Unix version you just do Control-C and abort or
exit, but Windows doesn't know about signals (= software interrupts).

> I've had a peek at socket.c, and am willing to hack it (but I only have
> documentation, and test capability, for Win32).

If you find something that works I'll be glad to integrate it.

> NB is the IO-stream stuff a model for implementing QP's with_output_to_chars
> and vice versa?  It looks straightforward...

In good old SWI-Prolog you can use '$write_on_string'/2.  See for example
sformat.  Version 4.0.7 contains `memory_file', which allows for creating
an abstract object, opening it as a stream for reading and writing and
get the content as an atom or list of character codes.  Check the clib
package documentation.

	Cheers --- Jan

