   boost asio and dropped connections
  Hi. I'm slowly getting the hang of Boost::asio but one thing I haven't found in the examples or in the tutorials was how to detect and handle a socket closing from the other end (ie not locally.) Is there an async_ handler for closing connections or do I have to look for exceptions when reading from the socket? Can someone point me towards any example code? I'm interested in both tcp and udp.Thanks   
  TCP:When connection is closed cleanly, the pending read will fail with exception. If socket is closed, any sends performed on that connection will fail as well. Periodic sends of zero bytes can be also used to probe the connection.For other cases, there is keep_alive option.Alternatively, keep a timer. Any time a connection receives data, it stores current time (or approximation). Every few seconds (can be 10 or 30), the timer fires, and iterates over all connections. Those that haven't been active for some time (30, 60 seconds) are closed.UDP:Does not have connections, so any connection handling will be custom built.   ;  Interesting. You say pending reads will fail -- what if there is simply nothing to read but the connection is still open?   ;Quote:Original post by whiterook6Interesting. You say pending reads will fail -- what if there is simply nothing to read but the connection is still open?Asynchronous reads do not trigger callback until something arrives, or connection is closed.   ;  The TCP protocol defines what it means to be "closed" versus just "nothing to read."This is, in fact, a possible problem with vanilla TCP implementations. If there is no traffic going either way, then there will be no packets, sometimes for hours. If the remote machine then closes the connection, there will be a RST packet to you, but if the network is broken, or the remote host crashes hard, you won't get that. This is one reason why many TCP users often turn on keepalives.Btw: "hard crash" means that the host itself goes down. If the process/program keeping the socket open crashes, the kernel will close the socket, which will send a RST to the other end.