Question: How to communicate over sockets in Clozure Common Lisp?

Question

How to communicate over sockets in Clozure Common Lisp?

Answers 1
Added at 2016-12-15 16:12
Tags
Question

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?

Answers to

How to communicate over sockets in Clozure Common Lisp?

nr: #1 dodano: 2016-12-15 17:12

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>
Source Show
◀ Wstecz