Clozure Common Lisp - TCP Socket Programming - Sending a Reply

白昼怎懂夜的黑 提交于 2020-01-03 13:06:11

问题


I have a very small program which opens a socket and accepts a connection. It then grabs the remote IP and port.

I'd like to send a text message to the remote computer (telnet) and close the connection.

I can't determine which function is for sending a message to the telnet client.

The Clozure manual lists a function called "send to" but it says it's for UDP sockets and I'm working with TCP sockets.

I hope someone can tell me what the proper function is, or, if "send-to" is the proper function, how to use it properly.

Thanks

(setq my-socket (ccl:make-socket :connect :passive :format :text
        :local-port 20000 :reuse-address t))

(setq connection (ccl:accept-connection my-socket))

(setq remote-host (ccl:remote-host connection))

(setq remote-port (ccl:remote-port connection))

回答1:


The documentation of CCL:ACCEPT-CONNECTION says that it returns a stream.

So you can use the normal I/O operations (example: PRINC) of Common Lisp with that stream. For I/O operations see the HyperSpec chapters on 'streams' and the 'printer'.

(defun st (port)
  (ccl:with-open-socket (socket :connect :passive
                                :format :text
                                :local-port port
                                :reuse-address t)
    (with-open-stream (stream (ccl:accept-connection socket))
      (princ "CCL example response" stream))))

; example call
(st 20000)



回答2:


In SBCL (using usocket), I use the SOCKET-STREAM function to return a lisp stream, then use FORMAT, WRITE and the like to send things across.



来源:https://stackoverflow.com/questions/1524912/clozure-common-lisp-tcp-socket-programming-sending-a-reply

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!