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:11:21
--Cgrdyab2wu3Akvjd
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Tue, Dec 09, 2003 at 06:06:16PM -0800, 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?
> >=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?
>=20
> 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.

Oh, I have a little "read" routine that plays with return values:

	rv =3D read(stuff)

	if (rv =3D=3D 0)
		rv =3D -2;	/* < 0 value indicating we're closed */
	else if ((rv =3D=3D -1) && (errno =3D=3D EAGAIN))
		rv =3D 0;

	return rv;

Then I get back the number of bytes read. If I get < 0, I got an error.

Take care,

Bill

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

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

iD8DBQE/1oDJWz+3JHUci9cRAieLAJ41WWTC/2Ae3OQCi45h68HnsttIbgCglwGm
kUgSAICw8bX4ykT/XTiqRMM=
=oHWE
-----END PGP SIGNATURE-----

--Cgrdyab2wu3Akvjd--