cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: async socket help

From: <koettermarkus_at_gmx.de>
Date: Wed, 03 Nov 2010 02:10:28 +0100

john skaller wrote:
> I'm not quite sure I follow. If I shutdown for read, how can the socket ever
> be readable again?

readable after shutdown(SHUT_RD) -> the remote closed the connection, as
the remote got your "I'm not reading your any more" and closes the
connection on their side, so we get an EOF.

> How do I detect this (without blocking)? What has this
> got to do with the output being flushed?

poll the shutdown()'ed socket for beeing readable, read/recv 'something'
of 0 bytes.

> Aren't the input and output
> buffers independent?

Of course, but you have your outgoing (tcp) queue, and there may reside
some data in this queue, and then you put a "I'm done" on the end of
this queue, and once this will be sent, and the remote finally closes
the connection as a reaction, you can be sure the buffer was flushed.

So, once the remote gets your shutdown(SHUT_RD)s "I'm done with this
conversation, I won't listen to you any longer" message and close()s the
connection afterwards, your socket will be readable, you'll
read()/recv() 0 bytes, and you can finally close() the connection.

And yes, the remote may close the connection right after you tried to
initiate a graceful shutdown, so it does not receive the data you'va had
waiting in your sliding window.
So you can only try to make sure you are doing things right, if the
remote disconnects somewhere in the middle, there is no guarantee which
data it received already.

> Sorry, excuse my ignorance, I've actually heard something like this
> before, it just doesn't seem to make sense and I don't recall where
> this would be supported in the Posix standard.. ;(

I'd refer to the unix socket faq.

Markus
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-11-03