Hello, I have encountered a problem with some code that I cannot get my head around, I  have spent quite a while looking for solutions but nothing has come up.

 

When the client sends the first packet it is read fine but the header(first 8 bytes) for the second packet ALWAYS goes wrong, the first byte in the buffer is always null and the rest of the packet is pushed down the char array, e.g.

 

I have done plenty of checks on the data the client sends:

Client sends 00080002

Header reads _0008000

 

Client sends 0008TEST

Header reads _0008TES

(with _ as null)

 

CODE:

This is the first call to async_read, called when the client has connected

-----------------

void CClient::start() {

 

    Log(MSG_INFO, "[%i] Connected", socket_.native());

 

    boost::asio::async_read(socket_,

                            boost::asio::buffer(packet_.buffer(), CPacket::headerSize),

                            boost::bind(&CClient::recvHeader, shared_from_this(), boost::asio::placeholders::error)

                            );

 

}

 

 

 

The rest of the async_read’s are called here

-----------------

void CClient::recvMessage(const boost::system::error_code& error) {

    if (!error && handleMessage()) {

 

        packet_.reset();

 

        boost::asio::async_read(socket_,

                                boost::asio::buffer(packet_.buffer(), CPacket::headerSize),

                                boost::bind(&CClient::recvHeader, shared_from_this(), boost::asio::placeholders::error)

                                );

    } else {

        disconnect();

    }

}

 

 

Thanks in adavnce,

                  Matty.

 

P.S.

I am very inexperienced with boost and not great at C++ so if you see anything I could improve, feedback would be appreciated.