Calling close() on a non blocking socket from within an io watcher

Graham Leggett minfrin at sharp.fm
Sat Aug 8 14:56:48 CEST 2009


Marc Lehmann wrote:

>> The problem that I have is that I cannot seem to figure out the "right"
>> way to close the socket at this point.
> 
> the correct solution is to use a protocol with handshake.

Can you explain in more detail what you mean by this? I am already using
a protocol with handshake, the message I am sending is the last goodbye.
Trouble is the other side doesn't see they last goodbye, they just see
connection reset by peer.

>> as yet unsent. Because the write() is non blocking, the close is issued
>> too early. Fair enough, this is the wrong way to do it.
> 
> yes, close does not dump the data, regardless of whether write is blocking or
> not. the kernel also buffers, regardless of whether the write is blocking or
> not.
> 
> blocking vs. non-blocking is not the issue, the behaviour is exactly the
> same.

That contradicts this:

http://www.gnu.org/s/libc/manual/html_node/Closing-a-Socket.html#Closing-a-Socket

Specifically the bit that says "Stop trying to transmit data from this
socket. Discard any data waiting to be sent.".

I am seeing data waiting to be sent being discarded, thus the original
question.

>> Does anyone know the right way to do this?
> 
> yes, wait for an acknowledgement from the other side. tcp requires this,
> regardless of blocking mode.

How, using libev, do I wait for an acknowledgment from the other side? I
can get an event saying "ready to write", but I cannot find an event
that says "ready to close".

Can you confirm?

Regards,
Graham
--
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3287 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.schmorp.de/pipermail/libev/attachments/20090808/773ff4c9/attachment.bin>


More information about the libev mailing list