[asio] Error in async_accept

Hello. I have this code (the variables that end with _ are instance variables). The code works well as is, in a synchronous way. void tcpServer::startAccept() { tcpConnection::pointer new_connection = tcpConnection::create(acceptor_.io_service()); boost::system::error_code error; acceptor_.accept(new_connection->socket()); onAccept(new_connection, boost::system::error_code()); } void tcpServer::onAccept(tcpConnection::pointer new_connection, const boost::system::error_code & error) { if (!error) { if (currentconnections_ < maxconnections_) { boost::lock_guard<boost::mutex> l(mutexcurrconnections_); /*connectionsinfo_.push_back(ServerConnectionInfoPtr(new ServerConnectionInfo( new_connection->socket().remote_endpoint().address().to_string(), new_connection->socket().remote_endpoint().port(), new_connection))); */ currentconnections_++; } else throw std::runtime_error("The server is full. No more connections accepted"); /*boost::asio::async_read(new_connection->socket(), boost::asio::buffer(bufferread_), boost::bind(&tcpServer::onRead, this, _1, _2)); */ vector<char> vecrecv(12); std::size_t readsize = boost::asio::read(new_connection->socket(), boost::asio::buffer(vecrecv)); cout << "Server received " << readsize << " bytes: " << endl; copy(vecrecv.begin(), vecrecv.end(), ostream_iterator<char>(cout)); cout << endl; std::string helloclient = "hello client"; std::vector<char> sendvec(helloclient.begin(), helloclient.end()); //TODO:Cambiar por receiveAllData() //std::size_t writesize = new_connection->socket().send(boost::asio::buffer(sendvec, sendvec.size())); std::size_t writesize = boost::asio::write(new_connection->socket(), boost::asio::buffer(sendvec)); std::cout << "Written " << writesize << " bytes" << std::endl; } startAccept(); } But if I change the function startAccept so that instead of doing what it used to do, it does an asynchronous_accept: void tcpServer::startAccept() { tcpConnection::pointer new_connection = tcpConnection::create(acceptor_.io_service()); boost::system::error_code error; acceptor_.async_accept(new_connection->socket(), boost::bind(&tcpServer::onAccept, this, boost::ref(new_connection), boost::asio::placeholders::error)); } I get the following errors: terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error>
' what(): Socket operation on non-socket terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> ' what(): Connection reset by peer
Anyone can help? I have no clue of what's happening. The socket is a shared_ptr, so it should be there when I use onAccept. Don't know what happens. Thanks for your time.

Solved. The error is inside bind I used a boost::ref to the shared_pointer, so the shared_pointer didn't get incremented, it was destroyed. 2009/12/15 Germán Diago <germandiago@gmail.com>:
Hello. I have this code (the variables that end with _ are instance variables). The code works well as is, in a synchronous way.
void tcpServer::startAccept() { tcpConnection::pointer new_connection = tcpConnection::create(acceptor_.io_service()); boost::system::error_code error;
acceptor_.accept(new_connection->socket()); onAccept(new_connection, boost::system::error_code()); }
void tcpServer::onAccept(tcpConnection::pointer new_connection, const boost::system::error_code & error) { if (!error) { if (currentconnections_ < maxconnections_) { boost::lock_guard<boost::mutex> l(mutexcurrconnections_);
/*connectionsinfo_.push_back(ServerConnectionInfoPtr(new ServerConnectionInfo( new_connection->socket().remote_endpoint().address().to_string(), new_connection->socket().remote_endpoint().port(), new_connection))); */ currentconnections_++;
} else throw std::runtime_error("The server is full. No more connections accepted");
/*boost::asio::async_read(new_connection->socket(), boost::asio::buffer(bufferread_), boost::bind(&tcpServer::onRead, this, _1, _2)); */ vector<char> vecrecv(12); std::size_t readsize = boost::asio::read(new_connection->socket(), boost::asio::buffer(vecrecv)); cout << "Server received " << readsize << " bytes: " << endl; copy(vecrecv.begin(), vecrecv.end(), ostream_iterator<char>(cout)); cout << endl;
std::string helloclient = "hello client"; std::vector<char> sendvec(helloclient.begin(), helloclient.end());
//TODO:Cambiar por receiveAllData()
//std::size_t writesize = new_connection->socket().send(boost::asio::buffer(sendvec, sendvec.size())); std::size_t writesize = boost::asio::write(new_connection->socket(), boost::asio::buffer(sendvec)); std::cout << "Written " << writesize << " bytes" << std::endl; } startAccept(); }
But if I change the function startAccept so that instead of doing what it used to do, it does an asynchronous_accept:
void tcpServer::startAccept() { tcpConnection::pointer new_connection = tcpConnection::create(acceptor_.io_service()); boost::system::error_code error;
acceptor_.async_accept(new_connection->socket(), boost::bind(&tcpServer::onAccept, this, boost::ref(new_connection), boost::asio::placeholders::error)); }
I get the following errors:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error>
' what(): Socket operation on non-socket terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> ' what(): Connection reset by peer
Anyone can help? I have no clue of what's happening. The socket is a shared_ptr, so it should be there when I use onAccept. Don't know what happens. Thanks for your time.
participants (1)
-
Germán Diago