How to know the TCP connection was closed?

Started by csfreebird, January 22, 2013, 10:27:54 PM

Previous topic - Next topic

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?

cormullion

#1
Is net-peek any use?

csfreebird

#2
net-receive works for me.

 (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.

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

Lutz

#3
Try: http://www.newlisp.org/downloads/newlisp_manual.html#net-select">http://www.newlisp.org/downloads/newlis ... net-select">http://www.newlisp.org/downloads/newlisp_manual.html#net-select

csfreebird

#4
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.

;; 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.