title: Detecting socket closing using boost::asio in Windows
body: <p>So I have a service method that uses <code>io_service::poll()</code> to process any currently-ready read/write events:</p>
  
  <pre><code>void Foo::service()
  {
    io.poll(); // boost::asio::io_service
    if (socket.is_open())
      if (socket.available())
        notifyReadyToRead();
      else
        notifyReadyToWrite();
  }
  </code></pre>
  
  <p>However, I'd also like to trigger behavior if the remote socket closes (gracefully or otherwise).  I know that Winsock can handle this, as it raises a network event in this circumstance.  Boost Asio sockets, however, have no built-in support for this as far as I can see.</p>
  
  <p>I've tried reading and writing a zero-byte packet to the socket to test it for errors, but it appears that the implementation simply bails successfully if you pass it <code>boost::asio::buffer(socket, 0)</code> without actually checking the socket.</p>
  
  <p>I've also heard that leaving a hanging read operation could work, but I'm not sure how to manage this while still allowing regular <code>boost::asio::read</code> and 'boost::asio::write` operations to work correctly.</p>
  
  <p>Finally, adding a ping operation to the app-level protocol is a no-go; this class is intended to be used generically, and even if it wasn't I cannot change the remote side's protocol.</p>
  
  <p>While I'd prefer a portable solution, a Windows-specific solution would be sufficient, even if it means grabbing the native handle and enumerating events that way.  (This occurred to me, but <code>WSAEnumNetworkEvents</code> clears the internal record of events, so I suspect it would cause problems if the boost::asio implementation depended on reading it.)</p>