;; echo client (use-modules (ice-9 optargs)) (use-modules (ice-9 getopt-long)) (use-modules (ice-9 rdelim)) (define (hostname->ip hostname) "Convert HOSTNAME to its IP. e.g., (hostname->ip \"www.google.com\") => \"64.233.189.104\"" (inet-ntop AF_INET (car (hostent:addr-list (gethost hostname))))) (define (main args) (let* ((option-spec '((help (single-char #\h) (value #f)) (host (single-char #\H) (value #t)) (port (single-char #\p) (value #t)))) (options (getopt-long args option-spec))) (if (option-ref options 'help #f) (usage) (client (option-ref options 'host "localhost") (option-ref options 'port "10000"))))) (define (usage) (display "\ Usage: client.scm [OPTIONS] -h, --help show this help -H, --host=LOCALHOST remote address -p, --port=10000 remote port ") (exit 1)) (define (client host port) (let ((s (socket PF_INET SOCK_STREAM 0)) (host-ip (hostname->ip host)) (port-value (string->number port))) (connect s AF_INET (inet-pton AF_INET host-ip) port-value) (display "connected to server\n") (echo-loop s))) (define (echo-loop server-port) (let ((line (read-line (current-input-port) 'concat))) (while (not (eof-object? line)) (display line server-port) (display (read-line server-port 'concat)) (set! line (read-line (current-input-port) 'concat))) (close server-port))) ;;; client.scm ends here