usage of boost::asio::async_write and boost::asio::buffer
Hi, I am modifying example echo server for my server. I started to get the attached error, and I suspect I am using buffer incorrectly since error started after a change there. I just changed a line from boost::asio::async_write(socket_, boost::asio::buffer("1^", 3, boost::bind(&session::handle_write, this, boost::asio::placeholders::error)); to boost::asio::async_write(socket_, boost::asio::buffer(replyString, replyString.size()), boost::bind(&session::handle_write, this, boost::asio::placeholders::error)); I generate the replyString to have results from my database query separated with ^ signs (which is used to tokenize the replyString on the client side...) string replyString="1^"; while(res->next()) { replyString+=res->getString("nickname")+"^"; } I checked that replyString.size() is really one more than the final filled position index in replyString. Any ideas, suggestions? TIA, Best regards, Ozgur (Oscar) Ozturk www.DrOzturk.com Phone: +1 (908) DROZGUR i.e, +1 (908) 376-9487
boost::asio::async_write(socket_, boost::asio::buffer(replyString, replyString.size()), boost::bind(&session::handle_write, this, boost::asio::placeholders::error));
I generate the replyString to have results from my database query separated with ^ signs (which is used to tokenize the replyString on the client side...)
string replyString="1^";
boost::asio::buffer does not copy your local string, so the string vanishes before the actual async. write takes place. By the way, if you wish to send the whole string, you don't have to pass a size: boost::asio::buffer(replyString) - but the string must be alive until the completion handler is called.
if you wish to send the whole string, you don't have to pass a size...but the string must be alive until the completion handler is called.
OK, now I try that. I use a member variable string instead of char*. I got
another error.
I tried to use the same string as buffer for both async_read_some and
async_write
I guess the string can not be treated as a mutable buffer, so maybe I should
keep the original char * buffer for async_read_some and use the string only
for outputting with async_write. Is that right?
(or any other recommendation?)
//NOW data_ member variable is string istead of char*
void start()
{
socket_.async_read_some(boost::asio::buffer(data_),
boost::bind(&session::handle_login, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
Error message includes:
c:\program
files\boost\boost_1_38\boost\asio\detail\win_iocp_socket_service.hpp(1409) :
error C2664: 'boost::asio::mutable_buffer::mutable_buffer(const
boost::asio::mutable_buffer &)' : cannot convert parameter 1 from 'const
boost::asio::const_buffer' to 'const boost::asio::mutable_buffer &'
Reason: cannot convert from 'const boost::asio::const_buffer' to
'const boost::asio::mutable_buffer'
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called
You can see the full error messages I get here:
http://pastebin.com/f4ee951e4
TIA
Best regards,
Ozgur (Oscar) Ozturk
www.DrOzturk.com
Phone: +1 (908) DROZGUR
i.e, +1 (908) 376-9487
On Tue, Jul 21, 2009 at 7:14 AM, Igor R
boost::asio::async_write(socket_, boost::asio::buffer(replyString, replyString.size()), boost::bind(&session::handle_write, this, boost::asio::placeholders::error));
I generate the replyString to have results from my database query separated with ^ signs (which is used to tokenize the replyString on the client side...)
string replyString="1^";
boost::asio::buffer does not copy your local string, so the string vanishes before the actual async. write takes place. By the way, if you wish to send the whole string, you don't have to pass a size: boost::asio::buffer(replyString) - but the string must be alive until the completion handler is called. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I tried to use the same string as buffer for both async_read_some and async_write
Who will allocate the buffer for the read operation? socket's async_read_some() doesn't know to do this alone.
I guess the string can not be treated as a mutable buffer, so maybe I should keep the original char * buffer for async_read_some and use the string only for outputting with async_write. Is that right? (or any other recommendation?)
Why wouldn't you use asio::streambuf? You've a set of functions that async.read/write from/to it, and it grows automatically. You can see relevant examples in asio docs.
participants (2)
-
Igor R
-
Ozgur Ozturk