简体   繁体   中英

How to communicate over sockets in Clozure Common Lisp?

In one CCL REPL I enter:

(WITH-OPEN-SOCKET (socket :LOCAL-PORT 6667 
                          :LOCAL-HOST "localhost" 
                          :CONNECT :PASSIVE 
                          :REUSE-ADDRESS t)
  (let ((stream (ACCEPT-CONNECTION socket :wait t)))
    (format stream "hello from server.~%")))

It waits for a connection.

In another CCL process I enter:

(WITH-OPEN-SOCKET (socket-stream :REMOTE-PORT 6667 
                                 :REMOTE-HOST "localhost" 
                                 :CONNECT :ACTIVE 
                                 :REUSE-ADDRESS t)
    (format t (READ-LINE socket-stream)))

At this point this process goes into waiting. It neither reads from the server, nor exits.

However, the moment the client connects to the server, the server exits with NIL. So clearly a connection is at least established, but the string "Hello from server." never gets communicated.

I am sure this is something basic I am overlooking. How do I send messages across? Is READ-LINE not the right way to read from a stream? Am I writing from the server incorrectly? How would I establish a bi-directional simple string based communication?

You know that output can be buffered?

That's one typical problem. see FINISH-OUTPUT and FORCE-OUTPUT .

If you write to a buffered stream, you have to make sure that the buffered output is actually fully delivered.

? (WITH-OPEN-SOCKET (socket-stream :REMOTE-PORT 6667 
                                 :REMOTE-HOST "localhost" 
                                 :CONNECT :ACTIVE 
                                 :REUSE-ADDRESS t)
    (format t (READ-LINE socket-stream)))
hello from server.
NIL


---

? (WITH-OPEN-SOCKET (socket :LOCAL-PORT 6667 
                          :LOCAL-HOST "localhost" 
                          :CONNECT :PASSIVE 
                          :REUSE-ADDRESS t)
  (let ((stream (ACCEPT-CONNECTION socket :wait t)))
    (format stream "hello from server.~%")
    (finish-output stream)
    stream))
#<BASIC-TCP-STREAM ISO-8859-1 (SOCKET/21) #x302000E3FD9D>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM