开发者

boost::asio error "The I/O operation has been aborted..."

I am receiving this error message

"The I/O operation has been aborted because of either a thread exit or an application request"

when using boost::asio::socket::async_read_some()

What does the error mean? What should I be looking for?

Here is the relevant code:

void tcp_connection::start()
{
  printf("Connected to simulator\n");

  socket_.async_read_some(boost::asio::buffer(myBuffer,256),
      boost::bind(&tcp_connection::read_sim_handler,this,
      boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred));
}

void tcp_connection::read_sim_handler(
                                  const boost::system::error_code& error, // Result of op开发者_JAVA技巧eration.
                                  std::size_t len )         // Number of bytes read.
{
try {
if (error == boost::asio::error::eof) {
    // Connection closed cleanly by peer.
    printf("Sim connection closed\n");
    return;
} else if (error) {
    throw boost::system::system_error(error); // Some other error.  if( ! error ) 
}

socket_.async_read_some(boost::asio::buffer(myBuffer,256),
    boost::bind(&tcp_connection::read_sim_handler,this,
    boost::asio::placeholders::error,
    boost::asio::placeholders::bytes_transferred));
}
 catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}

When I replace the call to async_read_some() with read_some() in the start() method, everything works fine ( except the server blocks waiting for a message! )

Following a comment i see that tcp_connection is going out of scope. I copied the code from http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/tutorial/tutdaytime3.html which says this: "We will use shared_ptr and enable_shared_from_this because we want to keep the tcp_connection object alive as long as there is an operation that refers to it." I confess that I do not know what all that means. So I have broken it somehow?

Following further comments, the answer is

void tcp_connection::start()
{
  printf("Connected to simulator\n");

  socket_.async_read_some(boost::asio::buffer(myBuffer,256),
      boost::bind(&tcp_connection::read_sim_handler,
              shared_from_this(),
      boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred));
}

Passing shared_from_this() rather than this employs the clever ( too clever? ) keep alive infrastructure established by the server code, even though the connection manager is not in scope, by normal means. For technical details, see comments under accepted answer.


Your tcp_connection object or your buffer object is likely going out of scope prior to the async operation completing.

Since your program is based on one of the tutorial examples, why don't you check out another of the examples that reads some data as well: http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/example/echo/async_tcp_echo_server.cpp

The reason your class goes out of scope is that you are no longer using shared_from_this(). What this does is create a shared_ptr to your class that is stored by the bind handler. This means that the shared_ptr will keep your class alive until your handler is called.

This is also why you need to inherit from enable_shared_from_this.

The last shared_ptr that goes out of scope will delete your class instance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜