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

Graham Leggett minfrin at
Sat Aug 8 02:17:45 CEST 2009

Hi all,

I have a non blocking socket that is being watched by an io watcher, and
when the connection is to be terminated, a flag is maintained which,
when set, means "shut the connection down when you have finished writing
all buffered data to the socket".

The problem that I have is that I cannot seem to figure out the "right"
way to close the socket at this point.

If I call shutdown()/close() immediately after the write(), the data in
the send buffer is lost, and the other side loses the last bit of data.
This is because close() dumps everything in the sending buffer that is
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.

Does anyone know the right way to do this?

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