Boost logo

Boost Users :

Subject: [Boost-users] [asio]Why std::ofstream AND boost::asio::async_read() fails?
From: Yasuhiko Yoshimura \(informax.co.jp\) (yoshimura_at_[hidden])
Date: 2010-09-21 02:19:54


Hi All,

I'm not able to see why downloading is sometimes fails in asio asyncread().
And I appriciate any suggestion.

When 'comment===>> //' and 'insert ===>>' lines are valid, download file size is
sometimes short than the original size from internet server(with big files).
But if I put 'comment===>> //' back to no comment and remove 'insert ===>>' lines,
the downloading result is perfect.

class ftp_client_dtp {
 ...
 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_)));
  ...
comment===>> // std::string spath = scurrentpath_ + "\\" + sfilename_;
comment===>> // f.open(spath.c_str(), std::ios::binary | std::ios_base::out);
  ...
 }
 ...
 void handle_read_content(const boost::system::error_code& err)
 {
  if (!err) {
insert ===>> std::string spath = scurrentpath_ + "\\" + sfilename_;
insert ===>> f.open(spath.c_str(), std::ios::binary | std::ios_base::out | std::ios_base::app);
   if (response_.size() > 0) {
    f << &response_;
   }
insert ===>> f.close();

   // 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 if (err == boost::asio::error::eof) {
   // eof
   std::cout << err;
insert ===>> std::string spath = scurrentpath_ + "\\" + sfilename_;
insert ===>> f.open(spath.c_str(), std::ios::binary | std::ios_base::out | std::ios_base::app);
   if (response_.size() > 0) {
    f << &response_;
   }
insert ===>> f.close();

   do_close();
  } else {
   std::cout << err;
   do_close();
  }
 }
 void do_close()
 {
comment===>> // f.close();
  ...
 }
 std::ofstream f;
 std::string sip_, sport_, scurrentpath_, sfilename_;
 ...
 boost::asio::streambuf response_;
 ...
};



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