通过套接字进行通信的lisp和python程序中的计时问题

时间:2013-11-26 19:54:26

标签: python sockets common-lisp allegro-cl

我正在尝试使用python程序和Allegro Common Lisp程序通过套接字进行通信。现在,我正在尝试建立一个监听连接的Lisp服务器,让python客户端连接到服务器,然后从客户端向服务器发送一条简单的消息。 Lisp服务器如下所示:

(let ((socket (socket:make-socket :connect :passive
                              :type :stream
                              :address-family :internet
                              :local-port 45676)))
  (format t "opened up socket ~A for connections~%" socket)

  ;; now wait for connections and accept
  (let ((client (socket:accept-connection socket 
                                      :wait t)))
    (when client
      ;; we've got a new connection from client
      (format t "got a new connection from ~A~%" client)

      ;; now wait for data from client
      (loop until (listen client) do
        (format t "first character seen is ~A~%" (peek-char t client))
        (let ((data (read-line client)))
          (format t "data received: ~A~%" data))))))

python客户端如下所示:

import socket
import time
s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
s.connect (('', 45676))
time.sleep (1) # works if uncommented, does not work if commented
s.sendall ("(list A B)")
s.close ()

由于我希望在此流上传递多条消息,因此我会监听传入的数据 服务器端然后回显它。但是,当我使用sleep命令进行评论时,我注意到了一个问题。输出看起来像这样:

opened up socket #<MULTIVALENT stream socket waiting for connection
                   at */45676 @ #x207b0cd2> for connections
got a new connection from #<MULTIVALENT stream socket connected from
                            localhost/45676 to localhost/60582 @
                            #x207b34f2>

换句话说,它实际上并没有回显数据(在我的例子中,“(列表A B)”)。如果我取消注释sleep命令(在连接启动和发送数据之间引入一些延迟),输出看起来像这样:

opened up socket #<MULTIVALENT stream socket waiting for connection
                   at */45676 @ #x207b0bea> for connections
got a new connection from #<MULTIVALENT stream socket connected from
                            localhost/45676 to localhost/60572 @
                            #x207b340a>
data received: (list A B)

我不确定为什么会这样。有人有解决方案吗?为多个数据交换重用相同的套接字连接是一个坏主意吗?如果我删除整个循环宏调用(从而使其成为一次性交换),则会毫无问题地接收数据并正确回显

编辑1:即使注释了sleep命令,最后一条语句也是如此。

0 个答案:

没有答案