Boost logo

Boost Users :

Subject: Re: [Boost-users] Modify an asio echo server so that it does not send message back to client
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2015-06-09 20:14:41


On 10/06/2015 09:11, Nicholas Yue wrote:
> Yes, I want to keep reading data an process them. Here is my
> implementation base on my understanding of your suggestion:
[...]
> void handle_read(const boost::system::error_code& error,
> size_t bytes_transferred)
> {
> if (!error)
> {
> std::cout << boost::format("handle_read bytes_transferred ='%1%'") %
> bytes_transferred << std::endl;
> process(data_);
> socket_.async_read_some(boost::asio::buffer(data_, max_length),
> boost::bind(&session::handle_read, this,
> boost::asio::placeholders::error,
> boost::asio::placeholders::bytes_transferred));
> std::cout << boost::format("handle_read data_ ='%1%'") % data_ << std::endl;
> }
> else
> {
> delete this;
> }
> }
>
> void process(char *data)
> {
> std::cout << boost::format("process data_ ='%1%'") % data_ << std::endl;
> }

You need to pass the bytes_transferred to process as well, so that it
knows how much data was actually read and therefore how much of data_ is
actually valid. Don't forget that this may be less than max_length and
may be different from what the client actually sent (ie. data sent in
one chunk may be received in multiple chunks).


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