问题
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.
回答1:
The streambuf::data() member-function returns buffers representing the input sequence. To avoid accessing the data again, one can use the streambuf::consume() member-function to remove characters from the beginning of the input sequence. In this case, once data has been copied from input_buffer_
to msgstr
, the input sequence can be cleared with:
input_buffer_.consume(input_buffer_.size());
Here is a complete minimal example demonstrating the behavior of consume()
:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
// This example is not interested in the handlers, so provide a noop function
// that will be passed to bind to meet the handler concept requirements.
void noop() {}
int main()
{
using boost::asio::ip::tcp;
boost::asio::io_service io_service;
// Create all I/O objects.
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 0));
tcp::socket server_socket(io_service);
tcp::socket client_socket(io_service);
// Connect client and server sockets.
acceptor.async_accept(server_socket, boost::bind(&noop));
client_socket.async_connect(acceptor.local_endpoint(), boost::bind(&noop));
io_service.run();
// No-consuming case.
{
std::cout << "Non-consuming example" << std::endl;
boost::asio::streambuf streambuf;
for (int i = 0; i < 5; ++i)
{
std::string data = "test";
data += boost::lexical_cast<std::string>(i);
// Write to server.
std::size_t bytes_transferred =
write(client_socket, boost::asio::buffer(data));
// Read from client.
read(server_socket, streambuf,
boost::asio::transfer_exactly(bytes_transferred));
// Print results.
std::string read_data(
boost::asio::buffers_begin(streambuf.data()),
boost::asio::buffers_end(streambuf.data()));
std::cout << "size: " << streambuf.size() << ", "
<< "read: " << read_data << std::endl;
}
}
// Consuming case.
{
std::cout << "Consuming example" << std::endl;
boost::asio::streambuf streambuf;
for (int i = 0; i < 5; ++i)
{
std::string data = "test";
data += boost::lexical_cast<std::string>(i);
// Write to server.
std::size_t bytes_transferred =
write(client_socket, boost::asio::buffer(data));
// Read from client.
read(server_socket, streambuf,
boost::asio::transfer_exactly(bytes_transferred));
// Print results.
std::string read_data(
boost::asio::buffers_begin(streambuf.data()),
boost::asio::buffers_end(streambuf.data()));
std::cout << "size: " << streambuf.size() << ", "
<< "read: " << read_data << std::endl;
streambuf.consume(bytes_transferred);
}
}
}
And the output:
Non-consuming example
size: 5, read: test0
size: 10, read: test0test1
size: 15, read: test0test1test2
size: 20, read: test0test1test2test3
size: 25, read: test0test1test2test3test4
Consuming example
size: 5, read: test0
size: 5, read: test1
size: 5, read: test2
size: 5, read: test3
size: 5, read: test4
Notice how in the consuming case, the previous data is discarded.
来源:https://stackoverflow.com/questions/26208624/boost-asio-async-read-the-read-message-adds-to-itself