This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How to find out if socket closed?


This is what I use, and what works for me
Use select with a timeout

* or, you'll end up with timeout, in which case select returns < 0 and
errno should be EINTR

* or, select returns 0 and you have input, easily detected with
FD_ISSET(filedes, &ibits)

* or, select returns 0 and you don't have any input in which case the
remote side closed the connection.

Hope this helps,

Bob





On Fri, 2003-07-11 at 20:12, Nick Garnett wrote:
> Grant Edwards <grante@visi.com> writes:
> 
> > I've been browsing around the old network stack code for a
> > couple hours now trying to figure out a way to determine if a
> > TCP connection has been closed by the other end.
> > 
> > I can't read data from it because I've got nowhere to put it,
> > but I need to know if it's closed.  I can't write any data to
> > the socket either.  Is there any way to query a socket to find
> > out if the other end has been closed?
> 
> Off the top of my head: How about trying a zero sized read? If the
> socket is closed then maybe that will be detected before it notices
> that you are not reading anything. There may be an ioctl() you can try
> which will either give you some status, or generate a detectable
> error.
> 
> > 
> > Under Unix, I think I'd get a SIGPIPE when the other end does a
> > close(), right?
> > 
> 
> Even with a SIGPIPE, this only tells you that one of your connections
> has gone, not which one. So there must be some mechanism for polling
> them to find our which. Maybe something with select() will work?
> 
> I'm just casting about in the dark here. I'm sure the solution is
> something trivial and stupid, and we're both too smart to think of it :-)
> 
> -- 
> Nick Garnett                    eCos Kernel Architect
> http://www.ecoscentric.com/     The eCos and RedBoot experts
-- 
----------------------------------------------------------------------
ir. Bob Koninckx
Katholieke Universiteit Leuven
Division Production Engineering,                   tel.  +32 16 322535
Machine Design and Automation                      fax.  +32 16 322987
Celestijnenlaan 300B                  bob.koninckx@mech.kuleuven.ac.be
B-3001 Leuven Belgium               http://www.mech.kuleuven.ac.be/pma
----------------------------------------------------------------------



-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]