Reading bytes from a TCP port

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Reading bytes from a TCP port

Fredrik Appelberg

Hello all, new chicken schemer here.

I'm writing an AMPQ client for fun (as One does) and can't figure out
the correct way to read from a TCP port. I want to block until input is
available, and then read all buffered data into a byte buffer (I'm using
bitstring, but it can be anything really). This is what I've come up with:

(let* ((buf (->bitstring ""))
       (first-byte (read-string 1 in)))
  (if (eq? #!eof first-byte)
      ... handle eof
      (begin
        (bitstring-append! buf (string->bitstring (string-append first-byte (read-buffered in))))
        ... do something with buf))

It works okay, but it seems a bit clunky to me. Is there a more
idiomatic way of reading a byte buffer from a port?

Cheers,
-- Fredrik

Reply | Threaded
Open this post in threaded view
|

Re: Reading bytes from a TCP port

Kooda
Fredrik Appelberg <[hidden email]> wrote:

>
> Hello all, new chicken schemer here.
>
> I'm writing an AMPQ client for fun (as One does) and can't figure out
> the correct way to read from a TCP port. I want to block until input is
> available, and then read all buffered data into a byte buffer (I'm using
> bitstring, but it can be anything really). This is what I've come up with:
>
> (let* ((buf (->bitstring ""))
>        (first-byte (read-string 1 in)))
>   (if (eq? #!eof first-byte)
>       ... handle eof
>       (begin
>         (bitstring-append! buf (string->bitstring (string-append first-byte (read-buffered in))))
>         ... do something with buf))
>
> It works okay, but it seems a bit clunky to me. Is there a more
> idiomatic way of reading a byte buffer from a port?
>
> Cheers,
> -- Fredrik

The read-u8vector procedure from SRFI-4 might be what you want:

http://wiki.call-cc.org/man/5/Module%20srfi-4#vector-io

Reply | Threaded
Open this post in threaded view
|

Re: Reading bytes from a TCP port

Fredrik Appelberg
[hidden email] writes:

> Fredrik Appelberg <[hidden email]> wrote:
>>
>> Hello all, new chicken schemer here.
>>
>> I'm writing an AMPQ client for fun (as One does) and can't figure out
>> the correct way to read from a TCP port. I want to block until input is
>> available, and then read all buffered data into a byte buffer (I'm using
>> bitstring, but it can be anything really). This is what I've come up with:
>>
>> (let* ((buf (->bitstring ""))
>>        (first-byte (read-string 1 in)))
>>   (if (eq? #!eof first-byte)
>>       ... handle eof
>>       (begin
>>         (bitstring-append! buf (string->bitstring (string-append first-byte (read-buffered in))))
>>         ... do something with buf))
>>
>> It works okay, but it seems a bit clunky to me. Is there a more
>> idiomatic way of reading a byte buffer from a port?
>>
>> Cheers,
>> -- Fredrik
>
> The read-u8vector procedure from SRFI-4 might be what you want:
>
> http://wiki.call-cc.org/man/5/Module%20srfi-4#vector-io

That seems to be a more natural way to read binary data, but there
doesn't seem to be a u8vector-analogue for read-buffered, which would
have been nice.

I guess the proper way to read an AMQP frame would be to begin by
reading the first 7 bytes, which contain the payload size, so that I
know how many more bytes to expect.

Anyhoo, thanks for pointing me in the right direction.

Cheers,
-- Fredrik