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

Graham Leggett minfrin at
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:

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

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

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

More information about the libev mailing list