[racket] Issue reading bytes from TCP port

From: David Vanderson (david.vanderson at gmail.com)
Date: Wed Sep 3 17:48:36 EDT 2014

On 09/03/2014 12:01 PM, Gilbert Martinez wrote:
> I'm having issues reading the response from a TCP server. 
>  Specifically, any attempt to read the input port does not terminate. 
>  I've used port->bytes, read-byte, and read-line. In all cases the 
> effect is the same.
>
> I thought that if there were no bytes available on an input port that 
> the read attempt would just return <eof>.  Is this some kind of 
> exclusivity issue?  Can I not read from the port until the connection 
> with the server is closed or something?
This is not true.  Most functions like 'read-byte' block until there are 
bytes available to read.  You don't get <eof> until the port 
(connection) is closed.  If you want a non-blocking call, look at 
'read-bytes-avail!*'.

#lang racket

(define-values (in out) (make-pipe))

(thread
  (lambda ()
    (for ((i 5))
      (write-byte i out)
      (sleep 1))
    (close-output-port out)))

(for ((b in))
   (printf "~x " b))

'for' is pretty smart in my experience.  I believe in this case it's 
using a sequence from 'in-port'.  But in any case, you should see 
numbers print out before the port is closed.

Does this help?

Thanks,
Dave
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140903/638b41e8/attachment.html>

Posted on the users mailing list.