How to know the TCP connection was closed?

Q&A's, tips, howto's
Locked
csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

How to know the TCP connection was closed?

Post 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?
Last edited by csfreebird on Thu Jan 24, 2013 11:49 am, edited 1 time in total.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: How to know the TCP connectio was closed?

Post by cormullion »

Is net-peek any use?

csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

Re: How to know the TCP connectio was closed?

Post 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)))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: How to know the TCP connectio was closed?

Post by Lutz »


csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

Re: How to know the TCP connectio was closed?

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

Locked