Debian Codegraph @

lab/tcp-connect-nonblocking-example.ss

raw
1 #!/usr/bin/env scheme-script
2
3 (import (ikarus))
4
5 ;;; very simple demo for how to connect to a server,
6 ;;; send a request and receive a response.
7
8 ;;; Here, we use an asynchronous IO socket. We wrap the whole
9 ;;; operation with an exception handler to handle the would-block
10 ;;; conditions. If we do get a would-block condition, we just
11 ;;; return, causing the read/write operation to be restarted until
12 ;;; it succeeds. Pretty lame at this point, but it works.
13
14 (define (http-cat host)
15 (let-values ([(ip op) (tcp-connect-nonblocking host "http")])
16 (let ([op (transcoded-port op (native-transcoder))]
17 [ip (transcoded-port ip (native-transcoder))])
18 (display "GET /\n" op)
19 (display (get-string-all ip))
20 (newline)
21 (close-input-port ip)
22 (close-output-port op))))
23
24 (http-cat "www.google.com")
25 ;(http-cat "127.0.0.1")
26