i have a problem with data transfert in boost::asio.
with the following code :
Server.cpp
void Server::accept(void)
{
Network::ptr connection = Network::create(this->my_acceptor.get_io_service());
this->my_acceptor.async_accept(connection->getSocket(), bind(&Server::endCmd, this, connection, placeholders::error));
}
void Server::endCmd(Network::ptr connection, const boost::system::error_code& error)
{
if (!error)
{
connection->Swrite("BIENVENUE");
this->accept();
}
}
Network.cpp
void Network::Sread(void)
{
async_read(this->socket, buffer(this->rbuffer), bind(&Network::endRead, shared_from_this(), placeholders::error));
}
void Network::endRead(const error_code& error)
{
if (!error)
{
this->rcv_msg = this->rbuffer.c_array();
std::cout << this->rcv_msg << std::endl;
this->Sread();
}
}
void Network::Swrite(std::string msg)
{
this->msg = msg;
async_write(this->socket, buffer(this->msg, (int)this->msg.size()), bind(&Network::endWrite, shared_from_this(), placeholders::error));
}
void Network::endWrite(const error_code &error)
{
if (!error)
{
this->Sread();
}
}
tcp::socket& Network::getSocket(void)
{
return (this->socket);
}
Network::ptr Network::create(io_service &ios)
{
return (ptr(new Network(ios)));
}
When i send a string like “Hello world” to the server with telnet, he write the following content : 
Who can tell me why the server is writting many unknow characters ?
It looks like the
rbuffer.c_array()isn’t a null terminated string but just a array of characters. Printing a character array tocoutassumes that the array is null terminated, which in this case causes the memory after the end of the array being included in the output.You should create a
std::stringfrom the data before trying to print it:(Assuming
rbufferhas asize()method or something equivalent)