gpt4 book ai didi

lisp - 读行时未捕获带有挂断条件的 iolib

转载 作者:太空宇宙 更新时间:2023-11-03 18:53:54 25 4
gpt4 key购买 nike

我是common lisp新手,在用iolib写server的时候,发现telnet到server,然后断开telnet,server throw

未知类型说明符:挂断

当我打印 backtrace 时,我发现它出现在执行 make-server-line-echoer 中的 read-line 时。我在那里使用 handler-case,但没有用。有帮助吗?

这是我的代码:

(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)

最佳答案

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

关于lisp - 读行时未捕获带有挂断条件的 iolib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14439629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com