Subject: Re: Dumb/silly network programming question
To: Paul Goyette <paul@whooppee.com>
From: Bill Studenmund <wrstuden@netbsd.org>
List: current-users
Date: 12/09/2003 18:06:16
--CXFpZVxO6m2Ol4tQ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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?
>=20
> 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=3DEAGAIN.  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=
=20
read but the connections is still alive, you'll get -1 and errno will be=20
EAGAIN.

=46rom man read:

RETURN VALUES
     If successful, the number of bytes actually read is returned.  Upon re=
ad-
     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

--CXFpZVxO6m2Ol4tQ
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (NetBSD)

iD8DBQE/1n+YWz+3JHUci9cRAgnDAJ4mRoJeQzxMLdMxUDC8xTrOiKenfwCeLnHr
TJjzoq7oOvUi0ifnEVnIp4c=
=gEfl
-----END PGP SIGNATURE-----

--CXFpZVxO6m2Ol4tQ--