[racket] Issue reading bytes from TCP port

From: Gilbert Martinez (gil.martinez242 at gmail.com)
Date: Wed Sep 3 12:01:21 EDT 2014

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?

My traffic monitor shows that the server is receiving the request and
responding (here is the exchange):

   Time Src. & Dst. Type Function Code Data  908.875<- TCP
Req. 0x04 [0x000 - 0x027]     00 01 00 00 00 06 0B 04    00 3B 00 03
908.90510.10.10.10:49967-> TCP Resp. 0x04 [0x000 - 0x027]     00 01 00 00
00 09 0B 04    06 00 00 00 00 00 00

Here is the code I am using to query:

#lang racket

(define (hex-bytes->bytes hex-bytes-list)
  (define (hex-byte->dec-byte hex-byte)
    (string->number (string-append "#x" ((if (symbol? hex-byte)
  (apply bytes (for/list ((hex-byte (in-list hex-bytes-list)))
                 (hex-byte->dec-byte hex-byte))))

(define-values (c-in c-out) (tcp-connect "" 502))

(write-bytes (hex-bytes->bytes '(00 01 00 00 00 06 0B 04 00 3B 00 03))
(flush-output c-out)

I run the code above and execute the following in the interactions pane:

Welcome to DrRacket, version [3m].
Language: racket [custom].
>(for ((byte (in-bytes (port->bytes c-in))))
  (printf "~x " byte))

The entry above above hangs on port->bytes.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140903/84b5391e/attachment.html>

Posted on the users mailing list.