Chinaunix首页 | 论坛 | 博客
  • 博客访问: 49582
  • 博文数量: 11
  • 博客积分: 85
  • 博客等级: 民兵
  • 技术积分: 163
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-08 09:59
文章分类

全部博文(11)

文章存档

2013年(8)

2012年(3)

我的朋友

分类: 服务器与存储

2013-07-23 17:51:04


点击(此处)折叠或打开

  1. (eval-when (:compile-toplevel :load-toplevel :execute)
  2.   (require :sb-bsd-sockets)
  3.   (use-package :sb-bsd-sockets)
  4.   (use-package :sb-sys))

  5. (defun htons (hostshort)
  6.   (logior (ash (logand hostshort #x00FF) 8)
  7.           (ash (logand hostshort #xFF00) -8)))

  8. (defun ntohs (netshort)
  9.   (htons netshort))

  10. (defun htonl (hostlong)
  11.   (logior (ash (htons hostlong) 16)
  12.           (htons (ash hostlong -16))))

  13. (defun ntohl (netlong)
  14.   (htonl netlong))

  15. (defun htonll (hostlonglong)
  16.   (logior (ash (htonl hostlonglong) 32)
  17.           (htonl (ash hostlonglong -32))))

  18. (defun ntohll (netlonglong)
  19.   (htonll netlonglong))

  20. (defun write-word (value out)
  21.   (write-byte (ash value -8) out)
  22.   (write-byte (logand value #xFF) out))

  23. (defun write-dword (value out)
  24.   (write-word (ash value -16) out)
  25.   (write-word (logand value #xFFFF) out))

  26. (defun write-qword (value out)
  27.   (write-dword (ash value -32) out)
  28.   (write-dword (logand value #xFFFFFFFF) out))

  29. (defun read-word (in)
  30.   (logior (ash (read-byte in nil 0) 8) (read-byte in nil 0)))

  31. (defun read-dword (in)
  32.   (logior (ash (read-word in) 16) (read-word in)))

  33. (defun read-qword (in)
  34.   (logior (ash (read-dword in) 32) (read-dword in)))

  35. (defclass event ()
  36.   ((socket :initarg :socket :initform nil)
  37.    (handler :initarg :handler :initform nil)))

  38. (defgeneric destroy(event))

  39. (defmethod destroy ((ev event))
  40.   (socket-close (slot-value ev 'socket))
  41.   (remove-fd-handler (slot-value ev 'handler)))

  42. (let ((fd-event (make-hash-table)))
  43.   (defun insert-fd-event (fd ev)
  44.     (setf (gethash fd fd-event) ev))
  45.   (defun remove-fd-event (fd)
  46.     (destroy (get-event fd))
  47.     (remhash fd fd-event))
  48.   (defun get-event (fd)
  49.     (gethash fd fd-event)))

  50. (defun socket-stream-close? (s)
  51.   (null (peek-char nil s nil nil)))

  52. (defun process-client (cfd)
  53.   (let* ((cev (get-event cfd))
  54.          (c (slot-value cev 'socket))
  55.          (s (socket-make-stream c :input t :output t :element-type :default)))
  56.     (handler-case
  57.         (let ((str (read-line s)))
  58.           (format t "~a recive: ~a~%" c str)
  59.           (write-line str s)
  60.           (force-output s))
  61.       (end-of-file ()
  62.         (format t "~a closed~%" c)
  63.         (remove-fd-event cfd)))))

  64. (defun accept-handler (serverfd)
  65.   (let* ((ev (get-event serverfd))
  66.          (c (socket-accept (slot-value ev 'socket)))
  67.          (clientfd (socket-file-descriptor c))
  68.          (cev (make-instance 'event
  69.                              :socket c
  70.                              :handler (add-fd-handler clientfd :input #'process-client))))
  71.     (format t "accept ~a~%" c)
  72.     (insert-fd-event clientfd cev)))

  73. (defun tcp-server (addr port)
  74.   (let* ((s (make-instance 'inet-socket :type :stream :protocol :tcp))
  75.          (fd (socket-file-descriptor s))
  76.          (ev (make-instance 'event
  77.                             :socket s
  78.                             :handler (add-fd-handler fd :input #'accept-handler))))
  79.     (socket-bind s (make-inet-address addr) port)
  80.     (socket-listen s 512)
  81.     (insert-fd-event fd ev)
  82.     (loop (serve-event))))

  83. (defun client (c)
  84.   (let ((s (socket-make-stream c :input t :output t :element-type :default)))
  85.     (dolist (str '("11111111" "22222222222" "33333333333" "444444444" "5555555"))
  86.       (sleep 1)
  87.       (write-line str s)
  88.       (force-output s)
  89.       (format t "~a send: ~a~%" c str)
  90.       (format t "~a recevie: ~a~%" c (read-line s)))
  91.     (socket-close c)))

  92. (defun tcp-client (addr port)
  93.   (let ((c (make-instance


阅读(2262) | 评论(0) | 转发(0) |
0

上一篇:python构造网络包

下一篇:strlcpy

给主人留下些什么吧!~~