1

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?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
Capstone
  • 2,254
  • 2
  • 20
  • 39
  • There is also a `:format :text` option (http://stackoverflow.com/a/1526444/124319), not sure if that helps. – coredump Dec 15 '16 at 16:01
  • 1
    @coredump The default value of format is text according to CCL docs. So it's unneeded. But thanks. – Capstone Dec 15 '16 at 16:05

1 Answers1

7

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>
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • Thanks. I checked out the links and added (FINISH-OUTPUT) to the end, after the format call. Made no difference. The behaviour is exactly the same. Maybe it's not buffering that's the issue? – Capstone Dec 15 '16 at 23:35
  • @λ-: and which stream did you call 'finish-output' on? Did you gave the right stream to FINISH-OUTPUT ? – Rainer Joswig Dec 15 '16 at 23:37