[racket] Issue reading bytes from TCP port

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Wed Sep 3 12:23:10 EDT 2014

Gilbert Martinez wrote at 09/03/2014 12:01 PM:
>
> I run the code above and execute the following in the interactions pane:
>
>
>     Welcome to DrRacket, version 6.1.0.5--2014-08-25(32ae3f8/a) [3m].
>     Language: racket [custom].
>     12
>     >(for ((byte (in-bytes (port->bytes c-in))))
>       (printf "~x " byte))
>
>
> The entry above above hangs on port->bytes.

Unless "in-bytes" is doing more magic than I feared, I believe that here 
"port->bytes" is a procedure that reads all the input to the end-of-file 
(which won't happen until the TCP connection is closed), before 
"in-bytes" or "for" or anyone else sees anything from the port.

"port->bytes" is a convenience, mainly for reading files.  You instead 
probably want to read one byte at a time from the port, or (a little 
harder) to do block reads of available bytes from the port.

BTW, I think this is another example of how "for" and friends are 
confusing to people.  I still think that people are better off first 
learning how to do things using named-"let" and mutually-recursive 
procedures *before* being introduced to "for". (My biggest objection is 
that people introduced to "for" first then end up shoehorning 
control-flow into "for" even when it doesn't fit.  But I'm saving this 
new kind of anecdote, about syntactic sugar obscuring evaluation model, 
as additional evidence of "for"'s crimes against humanity.)

Neil V.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140903/5af31ec5/attachment-0001.html>

Posted on the users mailing list.