Re: [asio-users] multiple calls of std::ostream( asio::streambuf )
Brought to you by:
chris_kohlhoff
From: Christopher K. <ch...@ko...> - 2007-01-31 21:51:58
|
Hi Axel, On Wed, 31 Jan 2007 11:29:02 +0100, "Alexander (Axel) Straschil" <ax...@st...> said: > Hello! > > I'm fooling around with asio falling in love with that ;-) > > Now, if got the following Problem: > For testing purpose, I'm writing a Webserver just giving the content of a > File to the Client, the way I did was to read the whole file into an > asio::streambuf and send this over the socket, works perfectly. > Now, as I want to be able to deal with really large files, I want to read > an > send chucks of the file, and did something like: > > New Socket Request: > boost::asio::streambuf out_buffer; > std::ostream os( out_buffer ); > std::ifstream file( "MyFile.txt", std::ios::in|std::ios::binary ); > char buf[256]; > while ( file.read(buf, sizeof(buf)).gcount() > 0 ) > { O > os.write( buf, file.gcount() ); > boost::asio::write( socket, out_buffer ); > } > > But this does not write the whole file to the client. > On about the first two or three calls. the whole file is written, > later, only parts are written. > On each socket request, out_buffer and os is created new. > > Any Idea what I messed up? The std::ostream object includes a "state" field which can contain values like badbit, failbit, eofbit etc. If the state has been set it will no longer perform any I/O. However, the state can be cleared to allow I/O to proceed again. I would guess that one of the boost::asio::write() calls is writing all of the available data in out_buffer, which means that the eofbit state is getting set. You might try adding the following line inside the loop, after the call to boost::asio::write(): os.clear(); Cheers, Chris |