Page 1 of 1

How to know the TCP connection was closed?

Posted: Wed Jan 23, 2013 6:27 am
by csfreebird
Hello
I have a test app programmed by newlisp. It connects to the server via TCP first, never sends request to server until server close the connection when timeout.
I read the API doc, but don't know which API could help me to determine the current connection's status.
Should I call net-send or net-receive for it?

Re: How to know the TCP connectio was closed?

Posted: Wed Jan 23, 2013 10:56 am
by cormullion
Is net-peek any use?

Re: How to know the TCP connectio was closed?

Posted: Wed Jan 23, 2013 11:21 am
by csfreebird
net-receive works for me.

Code: Select all

  (if (net-receive s buffer 2) (println "failed, the connection should be closed, time:" (date (date-value))) (println "succeeded, the connection was closed:" (net-error)))
But net-peek doesn't. It returns 0 instead of nil when connection has been closed by server.

Code: Select all

  (println (net-peek s))
  (if (= (net-peek s) nil) (println "succeeded, the connection was closed:" (net-error)))

Re: How to know the TCP connectio was closed?

Posted: Wed Jan 23, 2013 2:38 pm
by Lutz

Re: How to know the TCP connectio was closed?

Posted: Thu Jan 24, 2013 8:08 am
by csfreebird
I tried your suggestion. It works for me. Thank you.
Below is my two functions for this topic, hope it would be helpful for others.

Code: Select all

;; return true if connection was closed 
(define (net-close2? s)
  (if (net-receive s buffer 2) nil true))

(define (net-close? s)
  (if (net-select s "exception" 1000) nil true))
But why the (net-select s "exception" 1000) returns nil when connection was closed.
It seems it should return true to tell users there is an exception happened.