Date: Fri, 23 Aug 96 15:15:20 EDT From: eischen@vigrid.com (Daniel Eischen) To: hackers@FreeBSD.org, nate@mt.sri.com Cc: nate@rocky.mt.sri.com Subject: Re: Non-blocking I/O on sockets and closed sockets? Message-ID: <9608231915.AA14731@pcnet1.pcnet.com>
next in thread | raw e-mail | index | archive | help
> I need to setup my sockets as non-blocking to avoid some problems, but > it brings up a problem of determining if the remote end has closed the > connection. Normally, if you run select() on a FD, and the subsequent > read() call returns 0 you can assume the socket is dead. However, with > non-blocking I/O a read of 0 does *NOT* mean the socket is dead. > > Will read() return a negative error code on a socket if the socket is > closed/dead? I've looked in Steven's and in the obvious manpages, but > nothing jumps out at me. > > Here's the code snippet which should explain what goes on. If the socket is non-blocking, then you should expect errno to be EAGAIN on a read with no data present. You probably only need something like this: [...] /* * We may have more data than can fit in lbuff, so read in lbuff * size chunks. * XXX - How do we determine if the socket is closed? */ do { i = read(data->stream_sock, lbuff, 512); if ( i > 0 ) save_data(lbuff, i); else { if (errno != EAGAIN) /* handle error on socket */ ; } } while ( i > 0 ); } } } Dan Eischen eischen@pcnet.com
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?9608231915.AA14731>