I use my PC as a server. The client sends messages like: "PART1:Part2", and the server performs the necessary actions. I use boost's asio for the server code.
void start_read()
{
boost::asio::async_read(socket_, input_buffer_,
boost::asio::transfer_at_least(1),
boost::bind(&tcp_connection::handle_read, shared_from_this(),
boost::asio::placeholders::error));
}
// When stream is received handle the message from the client
void handle_read(const boost::system::error_code& error)
{
if (!error)
{
boost::asio::streambuf::const_buffers_type bufs = input_buffer_.data();
std::string msgstr(boost::asio::buffers_begin(bufs),
boost::asio::buffers_begin(bufs) +
input_buffer_.size());
std::vector<std::string> msgVector;
boost::split(msgVector, msgstr, boost::is_any_of(":"));
messageFromClient_ = msgVector[0];
valueFromClient_ = msgVector[1];
};
Messages to the server are sent every second and the resulting msgstr looks like this:
PART1:part2a
PART1:part2bPART1:part2b
PART1:part2cPART1:part2cPART1:part2c
PART1:part2dPART1:part2dPART1:part2dPART1:part2d
This is not what I want. I do not want to include data from the previous buffer, i.e. I want this:
PART1:part2a
PART1:part2b
PART1:part2c
PART1:part2d
I understand that the problem most likely lies here:
std::string msgstr(boost::asio::buffers_begin(bufs),
boost::asio::buffers_begin(bufs) +
input_buffer_.size());
However, I cannot find the correct code that would work in my case.
EDIT: Tried to do this instead:
std::istream response_istream(&input_buffer_);
std::string msgstr;
response_istream >> msgstr;
The first three times, I get what I need, but then the message is multiplied. It is always like this:
PART1:part2a
PART1:part2b
PART1:part2c
PART1:part2dPART1:part2d
PART1:part2ePART1:part2e
PART1:part2fPART1:part2fPART1:part2fPART1:part2f
PART1:part2gPART1:part2g
Thanks a lot in advance.