socket-client.stk 3.05 KB
Newer Older
eg's avatar
eg committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
;;;;
;;;; socket-client.stk	-- A simple client socket
;;;; 
;;;; Copyright © 2003-2005 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
;;;; 
;;;; 
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
;;;; 
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
;;;; USA.
;;;; 
;;;;           Author: Erick Gallesio [eg@essi.fr]
;;;;    Creation date: 27-Jan-2003 22:20 (eg)
;;;; Last file update:  4-Aug-2005 00:11 (eg)
;;;;

(define (port-idle-register! port proc)
  (unless (procedure? proc) (error "bad procedure ~S" proc))

  (let ((idle (%port-idle port)))
    (%port-idle port (cons proc idle))))
    

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (do-client host port)
  (let* ((p   (make-client-socket host port))
	 (in  (socket-input p))
	 (out (socket-output p)))
    ;; Create an idle handler which looks at the socket when reading stdin 
    (call/ec (lambda (exit)
	       (port-idle-register! (current-input-port)
				    (lambda ()
				      (when (char-ready? in)
					(let ((l (read-line in)))
					  (if (eof-object? l)
					      (exit #f)
					      (format #t "~~==> ~S\n" l))))))
	       ;; Send all read lines on the socket
	       (do ((l (read-line) (read-line)))
		   ((eof-object? l))
		 (format out "~A\n" l))))
    (format #t "Exiting client.")))

(define (do-client host port)
  (let* ((p   (make-client-socket host port))
	 (in  (socket-input p))
	 (out (socket-output p)))
    ;; Create an idle handler which looks at the socket when reading stdin 
    (port-idle-register! (current-input-port)
			 (lambda ()
			   (when (char-ready? in)
			     (format #t "~~==> ~S\n" (read-line in)))))
    ;; Send all read lines on the socket
    (do ((l (read-line) (read-line)))
	((eof-object? l))
      (format out "~A\n" l))))



(define (main args)
  (let ((port #f)
	(hostname "localhost"))
    (parse-arguments args
       "Usage: socket-client [options] port"
       "  This program read lines and send them to a given socket"
       "  displaying the response given by the server"
       "Options:"
       (("host" :arg host :alternate "h"  :help "host to connect to [localhost]")
	(set! hostname host))
       (("help" :help "provides help for the command")
	(arg-usage (current-error-port))
	(exit 0))
     (else
       (unless (= (length other-arguments) 1)
	 (arg-usage (current-error-port))
	 (exit 1))
       (set! port (string->number (car other-arguments)))))

    ;; parameters are parsed. make the client
    (do-client hostname port)))

     
; LocalWords:  stdin