Boost logo

Boost Users :

From: Andrej van der Zee (mavdzee_at_[hidden])
Date: 2007-06-28 02:24:23


Hi,

I was wondering how I can find out with ASIO whether a
connection is broken. I have a client connected to the
server that continuously streams data. I have to know
at the client-side when the server has hung up. I
tried to use an async_write and after 5 erroneous
tries the running thread for the connection is
terminated. Like this:

void
connection::run_thread()
{
  while (!stopped())
  {
    event_ptr e = fetch_event();
    if (!stopped())
    {
      int write_tries = 0;
      boost::asio::async_write(_socket,
boost::asio::buffer(*e, e->length()),
boost::bind(&connection::handle_write, this,
boost::asio::placeholders::error, e, write_tries));
    }
  }
}

void
connection::handle_write(const
boost::system::error_code &err,
                const event_ptr event, int
write_tries)
{
  if (write_tries == max_write_tries)
    stopped(true); // thread will break out of its
main-loop after being notified

  if (stopped())
    return; // no meaning to continue

  if (err)
  {
    // incremenent write_tries and retry async_write
    boost::asio::async_write(_socket,
boost::asio::buffer(*event, event->length()),
boost::bind(&connection::handle_write, this,
boost::asio::placeholders::error, event,
++write_tries));
  }
}

I think I am misunderstanding something. I thought
that after a async_write the registered handler will
be invoked. But the handler is NEVER invoked somehow,
although data is send from the client to the server.

I have the following questions:
- Why is the handler never invoked? When is it
supposed to be invoked?
- What is the best way to find out a connection has
been reset by the server? And client?

Thanks alot,
Andrej

      ___________________________________________________________
Yahoo! Mail is the world's favourite email. Don't settle for less, sign up for
your free account today http://uk.rd.yahoo.com/evt=44106/*http://uk.docs.yahoo.com/mail/winter07.html


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net