Subject: Re: Dumb/silly network programming question
To: Paul Goyette <>
From: Bill Studenmund <>
List: current-users
Date: 12/09/2003 18:11:21
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?
> 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=

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,


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

Version: GnuPG v1.2.3 (NetBSD)