> I'm not able to see why downloading is
sometimes fails in asio asyncread().
I found out my bug resolution that is moving the std::ofstream as a class
member
to stack of function calling itself.
Thanks,
Yasuhiko Yoshimura
class
ftp_client_dtp
{
// Start socket and etc.
void
start()
{
work_resolver_.reset(new
boost::asio::ip::tcp::resolver(work_io_service_));
work_socket_.reset(new
boost::asio::ip::tcp::socket(work_io_service_));
work_.reset(new
boost::asio::io_service::work(work_io_service_));
work_thread_.reset(new boost::thread(
boost::bind(&boost::asio::io_service::run,
&work_io_service_)));
...
// Start an asynchronous
resolve to translate the server and service names
// into a list of
endpoints.
...
}
...
private:
...
void
handle_read_content(const boost::system::error_code&
err)
{
if (!err || err == boost::asio::error::eof)
{
std::string spath = scurrentpath_ + "\\" + sfilename_;
//
insert the next line that the ofstream to stack.
std::ofstream
f(spath.c_str(), std::ios::binary | std::ios_base::out | std::ios_base::app |
std::ios_base::ate);
if (response_.size() > 0)
{
f << &response_;
}
f.close();
if (!err) {
// Continue reading remaining data until EOF.
boost::asio::async_read(*work_socket_,
response_,
boost::asio::transfer_at_least(1),
boost::bind(&ftp_client_dtp::handle_read_content,
shared_from_this(),
boost::asio::placeholders::error));
} else
{
// eof
std::cout <<
err;
do_close();
}
} else
{
std::cout << err;
do_close();
}
}
void do_close()
{
work_socket_->close();
}
// comment out the next line.
//
std::ofstream f;
std::string sip_, sport_, scurrentpath_,
sfilename_;
boost::asio::streambuf
request_;
boost::asio::streambuf
response_;
boost::scoped_ptr<boost::asio::ip::tcp::resolver>
work_resolver_;
boost::scoped_ptr<boost::asio::ip::tcp::socket>
work_socket_;
boost::asio::io_service
work_io_service_;
boost::scoped_ptr<boost::asio::io_service::work>
work_;
boost::scoped_ptr<boost::thread>
work_thread_;
};