I am working on a chat. For some reason, at the point of distributing user's message between other clients, string put together by `Server::msgHandler` is butchered by `async_write` in `Connection::write`, making it appear as only the part of that string has actually been read. Example:

> 
> Constructed message: "Hello people by Jack"  
> 
>  Appears as: "by Jack"
> 
> 
> 

that is the `string str=Hello people` is not printed out. At first I thought it was to do with the implicit `\0` at its end, but that wouldn't make any sense, moreover, as I tried various positions of string in message I noticed that if `str` is preceded with other text, the text will be shown either emitting `str` entirely, or placing it in unexpected places. E.g.

> 
> `writeMsg("It was said: \n"+str+" by \"name\"\n");`  
> 
>  will appear as:   
> 
>  It was said  
> 
>  by "name"Hello People
> 
> 
> 

Full, minimal, compilable example:   

```
#include <boost/asio.hpp>
#include <boost/bind/bind.hpp>
#include <boost/enable_shared_from_this.hpp>

#include <iostream>
#include <vector>
#include <deque>

typedef boost::asio::io_service io_service;
typedef boost::asio::ip::tcp tcp;

class Server;

class Connection : public boost::enable_shared_from_this<Connection> {
    io_service::strand strand;
    tcp::socket soc;
    std::deque<std::string> msgBuff;
    boost::asio::streambuf buf;
    Server* server;
    void(Server::*serverHandler)(std::string);

private:
    Connection(io_service& service) :soc(service), strand(service){

    }

    void writeStranded(std::string msg){
        msgBuff.push_back(msg);
        if (msgBuff.size() > 1)return;
        write();

    }
    void write(){
        std::string& tmpMsg = msgBuff[0];
        boost::asio::async_write(
            soc,
            boost::asio::buffer(tmpMsg.c_str(), tmpMsg.size()),
            strand.wrap(
            boost::bind(&Connection::handleWrite,
                this,
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred)
                )
        ); 
    }
    void handleWrite(const boost::system::error_code&, size_t s){
        msgBuff.pop_front();
        if (!msgBuff.empty())write();
    }
    void handleRead(const boost::system::error_code&, size_t s){
        std::istream is(&buf);
        std::string tmpMsg;
        std::getline(is, tmpMsg);

        (server->*serverHandler)(tmpMsg);
        readMsg();
    }

public:
    typedef boost::shared_ptr<Connection> pointer;
    static  pointer createInstance(io_service& service){
        return pointer(new Connection(service));
    }

    void init(Server* server, void(Server::*serverHandler)(std::string)){
        this->server = server;
        this->serverHandler = serverHandler; 
        writeMsg("hello\n");
        readMsg();
    }

    void writeMsg(std::string msg){
        strand.dispatch(boost::bind(&Connection::writeStranded, this, msg));
    }

    void readMsg(){
        const char delim = '\n';
        boost::asio::async_read_until(soc, buf, delim,
            boost::bind(&Connection::handleRead, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));

    }

    tcp::socket& getSocket(){
        return soc;
    }

};

class Server{
    tcp::acceptor accept;
    std::vector<Connection::pointer> connections;

public:
    Server(io_service& io_service, int port = 23) :accept(io_service, tcp::endpoint(tcp::v4(), port)){
        awaitConnection();
    };

private:
    void awaitConnection(){
        Connection::pointer con = Connection::createInstance(accept .get_io_service());
        accept.async_accept(con->getSocket(), boost::bind(&Server::conAcceptor, this, con, boost::asio::placeholders::error));
    }
    void conAcceptor(Connection::pointer con, const boost::system::error_code& err){
        if (err)return;
        con->init(this, &Server::msgHandler);
        awaitConnection();
        connections.push_back(con);
    }
    void msgHandler(std::string str){
        for (Connection::pointer ptr : connections){
            ptr->writeMsg(str+" by \"name\"\n");
        }
    }

};

int main(){
    io_service io_service;
    Server s(io_service);
    io_service.run();
    system("pause");

}

```

**Upd**  

Turns out the `async_read` was appending the string with carriage return, which was stored as it was added before the delimeter in the `name` string, and each time I tried make the name appear, everything preceding it would get overwritten by all that followed. Sometimes that carriage return would get wild and skip some characters preceding the name, which further complicated the search of this bug.