[racket] Read timeouts on tcp ports?...

69 views
Skip to first unread message

Rüdiger Asche

unread,
May 31, 2012, 6:28:45 AM5/31/12
to us...@racket-lang.org
Hi there everybody,

I'm not sure if this hasn't been discussed before (at least didn't
find any reference in google), so apologies if this should be a dupe:

How do I set a read timeout on a port (in this case, a TCP port)? In
native socket API, it's done via setsockopt() but the socket option
set is naturally shielded from application programmers. I couldn't
find a hint about timeouting read in the documentation for file I/O
which the port concept is a part of.

Thanks!



____________________
Racket Users list:
http://lists.racket-lang.org/users

Sam Tobin-Hochstadt

unread,
May 31, 2012, 7:30:41 AM5/31/12
to Rüdiger Asche, us...@racket-lang.org
I think the simplest way to do this would be with `read-bytes-evt` and
`sync/timeout`:

(sync/timeout 1 (read-bytes-evt 50 port))

This reads 50 bytes (or to eof, if that's less than 50) from port,
with a timeout of 1 second.

--
sam th
sa...@ccs.neu.edu

Neil Van Dyke

unread,
May 31, 2012, 7:55:10 AM5/31/12
to Rüdiger Asche, us...@racket-lang.org
I would use "sync/timeout" or "sync/timeout/enable-break", like Sam
said. I don't recall using "read-bytes-evt", but I am fond of
"read-bytes-avail!" in conjunction with "sync/timeout"*. Would be
interesting to see how efficiently one could handle a particular I/O
profile using "read-bytes-avail!", "read-bytes!-evt", and "read-bytes-evt".

Neil V.

Matthias Felleisen

unread,
May 31, 2012, 10:40:18 AM5/31/12
to Neil Van Dyke, us...@racket-lang.org

Does this deserve an entry in our Wiki on code nuggets whose name escapes me once again? -- Matthias
Reply all
Reply to author
Forward
0 new messages