(require 'usocket) (defpackage :myserver (:use :common-lisp :usocket) (:nicknames :mysrv) (:export "start-server")) (in-package :mysrv) (defun start-server (port) (socket-server "127.0.0.1" port #'(lambda (stream) (format stream "~a~%" "hello usocket"))))该函数参考 https://common-lisp.net/project/usocket/api-docs.shtml
(require 'usocket) (defpackage :myclient (:use :common-lisp :usocket) (:nicknames :mycli) (:export "start-client")) (in-package :myclient) (defun start-client (ip port) (let ((sock (usocket:socket-connect ip port))) (progn (force-output (usocket:socket-stream sock)) (do ((line (read-line (usocket:socket-stream sock) nil) (read-line (usocket:socket-stream sock) nil))) ((not line)) (format t "~A" line)))))该函数参考 http://common-lisp.net/project/common-lisp-beginner/sockets_doc.html
测试环境为emacs+slime,telnet,并假设你已经安装好quicklisp。 html
服务端启动后,使用telnet测试其是否能够成功运行,图示以下: web
客户端链接服务端,图示以下: api
可见,telnet和客户端都正确返回"hello usocket"这一字符串。 websocket
客户端发送函数: 并发
(require 'usocket) (defvar sock '()) (defun myclient (host port) (setq sock (usocket:socket-connect host port))) (defun my-send (buf) (if sock (progn (format (usocket:socket-stream sock) "~A" buf) (force-output (usocket:socket-stream sock)) ) nil))
本节参照: socket
http://stackoverflow.com/questions/25608424/websocket-client-in-common-lisp-with-usocket-library 函数
http://stackoverflow.com/questions/17257567/receiving-data-through-lisp-usocket 测试
http://www.lispworks.com/documentation/HyperSpec/Body/f_finish.htm#force-output ui
可正常连接服务端并发送数据,请自行测试。enjoy youself ! spa