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?


On Fri, Jul 11, 2003 at 07:12:25PM +0100, Nick Garnett wrote:

>> 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.
> 
> Off the top of my head: How about trying a zero sized read?

The problem is that there is receive data in the socket queue
waiting to be read.  However, I don't have any room for the
data, so if the socket hasn't been closed, I need to leave it
there so that TCP/IP window-szie/flow-control kicks in and
throttles the other end.

If the socket has been closed, I need to shut things down.

> If the socket is closed then maybe that will be detected before
> it notices that you are not reading anything.

I tried a recvfrom with MSG_PEEK set, and it returns data fine,
even after the connection has been closed.  I tried writing 0
bytes, that returns sucess even if the connection has been
closed.

> There may be an ioctl() you can try which will either give you
> some status, or generate a detectable error.

I tried doing getsockopt() and querying for SO_ERROR, and got
nothing useful.

> > 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 don't think so.  With select, a closed connection behaves the
same as a read-data-ready connection.  Since there _is_ read
data ready, you can't determine the difference with select.

The Linux man page shows that with poll() you can ask for HUP
events, but that doesn't seem to be something implimented by
BSD stacks.

> 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 :-)

If you happen to get suddenly dumber and figure it out, let me
know. ;)

-- 
Grant Edwards
grante@visi.com

-- 
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]