[racket] Doubt about the 'More: Systems Programming with Racket'

5 views
Skip to first unread message

Eduardo Bellani

unread,
Jun 30, 2011, 9:21:00 PM6/30/11
to Racket mailing list
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello list.
I was playing around the quoted tutorial, and found a strange behavior
in the following code:

;;code
#lang racket

(require racket/tcp)
(provide serve)

(define (serve port-no)
(define listener (tcp-listen port-no 5 #t))
(define (loop)
(accept-and-handle listener)
(loop))
(define t (thread loop))
(lambda ()
(kill-thread t)
(tcp-close listener)))

(define (accept-and-handle listener)
(define-values (in out) (tcp-accept listener))
(thread
(lambda ()
(handle in out)
(close-input-port in)
(close-output-port out))))

(define (handle in out)
(display (port->string in))) ;; it locks here

;; test

(define-test-suite small-suite
(let*-values ([(stopper) (serve 8080)]
[(tcp-input-port tcp-output-port)
(tcp-connect "127.0.0.1" 8080)])
(display "stuff" tcp-output-port)
(flush-output tcp-output-port)
(sleep 0.1)
(check-equal? (port->string tcp-input-port)
"stuff")
(stopper)))

(run-tests small-suite)

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

The code above works very well if I read the input port with
read-line, but won't budge if I use something like port->string.
Can somebody shed a light on this?
Thanks for the time.
- --
Eduardo Bellani

omnia mutantur, nihil interit.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAk4NIPwACgkQSbLl0kCTjGlixwCfW+3uDxSe/7sz5PW03iMf2XeK
MfsAn0+p95DZGrgpGDzlhMQl1UM0hYze
=yace
-----END PGP SIGNATURE-----
_________________________________________________
For list-related administrative tasks:
http://lists.racket-lang.org/listinfo/users

Neil Van Dyke

unread,
Jun 30, 2011, 9:35:16 PM6/30/11
to Eduardo Bellani, Racket mailing list
Eduardo Bellani wrote at 06/30/2011 09:21 PM:
> The code above works very well if I read the input port with
> read-line, but won't budge if I use something like port->string.
> Can somebody shed a light on this?
>

Try "close-output-port" after "flush-output". Otherwise, the
"port->string" in "handle" is waiting for an end-of-file that never arrives.

Also, in your "handle" procedure, do you want to "display" to "out"?

--
http://www.neilvandyke.org/

Eduardo Bellani

unread,
Jul 6, 2011, 11:58:44 PM7/6/11
to Neil Van Dyke, Racket mailing list
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> Try "close-output-port" after "flush-output". Otherwise, the


> "port->string" in "handle" is waiting for an end-of-file that never
> arrives.

Thanks a lot Neil, that completely solved the problem.

> Also, in your "handle" procedure, do you want to "display" to "out"?
>

Not really, that was just for initial testing purposes.


- --
Eduardo Bellani

omnia mutantur, nihil interit.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAk4VLvQACgkQSbLl0kCTjGmibACfeO3K8JNKDHLYRkH5WXb8zUs2
AUcAnjLy3x04n8czn8vgDPUzHyHFN+r8
=DpKu
-----END PGP SIGNATURE-----

Reply all
Reply to author
Forward
0 new messages