注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Chun Tian (binghe)

超越自我,洞察宇宙

 
 
 

日志

 
 

UDP on LispWorks  

2007-11-03 04:53:34|  分类: Lisp |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
(in-package :cl-user)

(eval-when (:compile-toplevel :load-toplevel :execute)
  (require "comm")
  ;; Don't warn when I'm hacking the COMM package
  (setf hcl:*packages-for-warn-on-redefinition*
        (remove "COMM" hcl:*packages-for-warn-on-redefinition* :test #'equal)))

(in-package :comm)

(export '(open-udp-stream connect-to-udp-server))

#+win32
(eval-when (:load-toplevel :execute)
  (ensure-sockets))

(defconstant *socket_sock_dgram* 2
  "Connectionless, unreliable datagrams of fixed maximum length.")

(defun open-udp-stream (hostname service &key  (direction :io) (element-type 'base-char)
                        errorp read-timeout write-timeout local-address local-port)
  "learn from open-tcp-stream"
  (let ((socket (connect-to-udp-server hostname service
                                       :errorp errorp
                                       :local-address local-address
                                       :local-port local-port)))
    (make-instance 'socket-stream :socket socket
                                  :direction direction
                                  :element-type element-type
                                  :read-timeout read-timeout
                                  :write-timeout write-timeout)))

(defun connect-to-udp-server (host service &key errorp local-address local-port)
  "see connect-to-tcp-server"
  (let ((socket-fd (socket *socket_af_inet* *socket_sock_dgram* *socket_pf_unspec*)))
    (if socket-fd
      (fli:with-dynamic-foreign-objects ((server-addr sockaddr_in)
                                         (client-addr sockaddr_in))
        ;; bind to local address/port if specified.
        (when (and local-address local-port)
          (initialize-sockaddr_in client-addr *socket_af_inet* local-address local-port "udp")
          (bind socket-fd
                (fli:copy-pointer client-addr :type 'sockaddr)
                (fli:pointer-element-size client-addr)))
        ;; connect to remote address/port
        (initialize-sockaddr_in server-addr *socket_af_inet* host service "udp")
        (if (connect socket-fd
                     (fli:copy-pointer server-addr :type 'sockaddr)
                     (fli:pointer-element-size server-addr))
          ;; return
          socket-fd
          (if errorp (error "cannot connect") nil)))
      (if errorp (error "cannot create socket") nil))))

  评论这张
 
阅读(927)| 评论(3)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017