1

我是一个比较新的common lisp,当我用iolib写服务器的时候,发现当telnet到服务器,然后断开telnet,服务器抛出

未知类型说明符:挂断

当我打印回溯时,我发现它出现在 make-server-line-echoer 中的 read-line 执行时。我在那里使用处理程序案例,但不起作用。有什么帮助吗?

这是我的代码:

(ql:quickload 'iolib)

(defpackage :com.server.test
  (:use :sockets :iomux :common-lisp))
(in-package :com.server.test)

(defvar *server-event-base* nil)

(defun make-server-line-echoer (socket)
  (lambda (fd event exception)
    (handler-case
        (progn
          (let ((line (read-line socket)))
            (format t "Read ~A~%" line)
            (format socket "~A~%" line)
            (finish-output socket)))
      (socket-connection-reset-error ()
        (format t "Connection reset by peer~%")
        (close socket))
      (hangup ()
        (format t "Client close connection on write~%")
        (close socket))
      (end-of-file ()
        (format t "Client closed connection on read~%")
        (close socket)))))

(defun make-server-listener-handler (socket)
  (lambda (fd event exception)
    (let ((client (accept-connection socket :wait t)))
      (when client
        (multiple-value-bind (who port)
            (remote-name client)
          (format t "Accept a client from ~A:~A~%" who port)
          (set-io-handler *server-event-base*
                          (socket-os-fd client)
                          :read
                          (make-server-line-echoer
                           client)))))))

(defun run-server (&key port)
  (setf *server-event-base* (make-instance 'event-base))
  (let ((listener (make-socket :connect :passive
                               :address-family :internet
                               :type :stream
                               :ipv6 nil
                               :external-format '(:utf-8 :eol-style :crlf))))
    (bind-address listener +ipv4-unspecified+ :port port :reuse-addr t)
    (listen-on listener :backlog 5)
    (format t "Listening on socket bound to: ~A:~A~%"
            (local-host listener)
            (local-port listener))
    (set-io-handler *server-event-base*
                    (socket-os-fd listener)
                    :read
                    (make-server-listener-handler listener))
    (handler-case
        (event-dispatch *server-event-base*)
      (socket-connection-reset-error ()
        (format t "Connection reset by peer~%"))
      (hangup ()
        (format t "Client close connection on write~%"))
      (end-of-file ()
        (format t "Client closed connection on read~%")))
    (format t "Close connection now~%")))

(run-server :port 3000)
4

1 回答 1

2

您必须use iolib.streams,因为hangup条件是在该包中定义的。

于 2013-01-21T14:08:00.547 回答