Debian Codegraph @

lab/tcp-pingpong.ss

raw
1 #!/usr/bin/env scheme-script
2 (import (ikarus))
3
4 (define max-bytes 100000000)
5
6 (define devrand (open-file-input-port "/dev/urandom"))
7
8 (define (rand-length)
9 (add1 (mod
10 (bytevector-u16-ref (get-bytevector-n devrand 2) 0 'little)
11 1024)))
12
13 (define (echo host)
14 (printf "Connecting\n")
15 (let-values ([(ip op) (tcp-connect host "echo")])
16 (printf "Connected\n")
17 (let f ([bytes 0])
18 (printf "~s " bytes)
19 (when (<= bytes max-bytes)
20 (let ([n (rand-length)])
21 (let ([bv (get-bytevector-n devrand n)])
22 (put-bytevector op bv)
23 (flush-output-port op)
24 (let ([v (get-bytevector-n ip n)])
25 (assert (equal? v bv)))
26 (f (+ bytes n))))))
27 (close-input-port ip)
28 (close-output-port op)
29 (newline)))
30
31 (echo "localhost")