Subject: Re: Dumb/silly network programming question
To: Bill Studenmund <wrstuden@netbsd.org>
From: Paul Goyette <paul@whooppee.com>
List: current-users
Date: 12/09/2003 18:30:46
Yup. I verified, and I get return value of -1 for the read, and errno
is set to EAGAIN. This is on the first read after the other end of the
connection has been SIGKILLed. I do NOT get a return value of zero,
that was a typo in the original post.
On Tue, 9 Dec 2003, Bill Studenmund wrote:
> On Sat, Dec 06, 2003 at 07:30:45AM -0800, Paul Goyette wrote:
> > How does one tell if a TCP socket has been disconnected without trying
> > to write to the socket?
> >
> > If the remote end closes the socket normally, all is OK. But if the
> > remote process is SIGKILLed, my listener is notified via select(2)
> > that the socket is available for reading. Yet when I read from the
> > socket, I get zero data returned and errno=EAGAIN. Since the same
> > errno is returned if the socket is empty, how can I distinguish that
> > that remote end is gone?
>
> Uhm, getting back zero data means you hit the end. If there is nothing to
> read but the connections is still alive, you'll get -1 and errno will be
> EAGAIN.
>
> From man read:
>
> RETURN VALUES
> If successful, the number of bytes actually read is returned. Upon read-
> ing end-of-file, zero is returned. Otherwise, a -1 is returned and the
> global variable errno is set to indicate the error.
>
>
> Note that errno only matters if you get back a -1.
>
> Take care,
>
> Bill
>
----------------------------------------------------------------------
| Paul Goyette | PGP DSS Key fingerprint: | E-mail addresses: |
| Network Engineer | FA29 0E3B 35AF E8AE 6651 | paul@whooppee.com |
| & World Cruiser | 0786 F758 55DE 53BA 7731 | pgoyette@juniper.net |
----------------------------------------------------------------------