[Openmcl-devel] How can I detect a client closed its connection to a listener socket?

Andreas Thiele andreas at atp-media.de
Mon Nov 30 09:27:11 PST 2015


Hi,

 

I found a solution I think is appropriate. I wrote test-listener a little
different just like what I need in my application:

 

(defun test-listener ()

  (loop 

     (let ((stream (ccl:accept-connection *test-socket*

 
:stream-args '(:external-format :iso-8859-1))))

       (adt:record :dbg "connect test")

       (loop

                  (handler-case

                      (read-char stream)

                    (error ()

                      (return))))

       (adt:record :dbg "disconnect test"))))

 

It seems my problem was lack of knowledge of TCP/IP internals. Did not know
anything about sending FINs. Before closing the client connection I do

 

(ccl:shutdown *test-stream* :direction :output)

 

This causes read-char to produce an unexpected end of input error, which I
catch in the handler-case. Perhaps not elegant but probably not to handle by
other means.

 

Further suggestions welcome, but problem solved.

 

Andreas

 

 

Von: Openmcl-devel [mailto:openmcl-devel-bounces at clozure.com] Im Auftrag von
Andreas Thiele
Gesendet: Montag, 30. November 2015 16:00
An: openmcl-devel at clozure.com
Betreff: [Openmcl-devel] How can I detect a client closed its connection to
a listener socket?

 

Hi,

 

I wrote the following bit of code:

 

(defvar *test-socket* nil)

 

(defun test-listener ()

  (loop 

     (let ((stream (ccl:accept-connection *test-socket*

 
:stream-args '(:external-format :iso-8859-1))))

       (adt:record :dbg "connect test")

       (loop :unless (ccl:stream-eofp stream) :do (sleep 0.2))

       (adt:record :dbg "disconnect test"))))

 

(defun start-test-listener ()

  (setf *test-socket* (ccl:make-socket :local-port 4010

 
:connect :passive :reuse-address t :keepalive t))

  (ccl:process-run-function "Test Listener" 'test-listener))

 

(defvar *test-stream* nil)

 

(defun connect-test ()

  (ignore-errors

    (setf *test-stream* (ccl:make-socket :remote-host "127.0.0.1"

 
:remote-port 4010))))

 

adt:record just prints to my slime-repl. I start the listener using
start-test-listener connect a client via connect-test. "connect test"
appears within my slime window. I do a (close *test-stream*) but nothing
happens. (ccl:tream-eofp stream) never returns true and thus the loop never
ends and no "disconnect test" can appear. Is there a way to solve this? How
do I detect, the client has closed the connection?

 

Thankful for any hint or help.

 

Best Regards

Andreas Thiele

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.clozure.com/pipermail/openmcl-devel/attachments/20151130/532a136e/attachment.htm>


More information about the Openmcl-devel mailing list