[Openmcl-devel] Read all available input from socket stream?

Gary Byers gb at clozure.com
Wed Jul 24 04:31:59 PDT 2013


Here's a very simple test case that tries to do something similar to
what your code is apparently trying to do.
;---
(defun server ()
   (let* ((lsock (make-socket :connect :passive :local-host "localhost" :local-port 40000 :reuse-address t))
          (data (make-array 10 :element-type '(unsigned-byte 8))))
     (dotimes (i 10) (setf (aref data i) i))
     (let* ((stream (accept-connection lsock :wait t)))
       (write-sequence data stream)
       (when t
         (sleep 5)
         (write-sequence data stream))
       (close stream)
       (close lsock))))


(defun test ()
   (process-run-function "server" #'server)
   (let* ((stream (make-socket :remote-host "localhost" :remote-port 40000))
          (data (make-array 19 :element-type '(unsigned-byte 8)))
          (n (read-sequence data stream)))
     (close stream)
     (format t "~& requested 19 octets, got ~d" n)))
;---

Calling (TEST) shoud pause for ~5 seconds, then report that it requested
and go 19 octets.  Does this fail for you ?  If not, how does the code
that you're dealing with differ ?

If you change the WHEN T in #'SERVER to WHEN NIL (and wait a few minutes,
since I'm likely not using :reuse-address correctly), the server will close
its side of the connection after writing 10 octets; READ-SEQUENCE will see
that the stream is at EOF after those 10 octets are read.  This (EOF) is the
only situation in which READ-SEQUENCE should return prematurely, and it's the
only situation I'm aware of in which it does so in CCL.


On Tue, 23 Jul 2013, Joshua Kordani wrote:

> Greetings all.
> 
> I am attempting to read all currently available data from a stream.? I am
> not expecting much data, so I suspect I need to call finish-output.? (read)
> seems to return immediately, before it seems that data is available.? I
> guess in hindsight I don't know how the implementation is supposed to know
> when input has stopped coming in on what is conceptually a stream with no
> eof representing any termination of input.
> 
> I guess what I'm saying is, given a socket stream, is there a way to read,
> blocking until there is input to be read and until there is no more data to
> be read, this kind of stream?
> 
> http://paste.lisp.org/+2YLT
> this is a paste of the funciton I'm trying to get working.? I want to write
> something to the stream, and I expect something back, which I want to either
> return from this function or simply emit to *terminal-io*
> 
> Josh
> 
>



More information about the Openmcl-devel mailing list