首页

2010年5月29日星期六

Scheme/Lisp 启动一个进程实现两个进程相互通讯

;首先加载TCP协议模块
(require Scheme/tcp)

;;定义一个server的线程

(define server (thread (lambda ()
   (let ((thd (tcp-listen 8080)))
     (let loop ((ready (tcp-accept-ready? thd)))
       (if ready
         (let-values (((in out) (tcp-accept thd)))
(read in)
(close-input-port in)
(write 'got-it out)
(close-output-port out)
(loop (tcp-accept-ready? thd)))))))))

;;然后一个客户机

(define client (thread (lambda ()
    (let-values (((in out) (tcp-connect "localhost" 8080)))
       (write "hello" out)
       (close-output-port out)
       (read in)
       (close-input-port in)
      (kill-thread client)))))

;;这个结合IO流的代码,也应该能看懂,前面文章我写的有IO的代码。

没有评论:

发表评论