[ASIO] async_read buffer: starting byte null

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.

boost::asio::async_read(socket_,
boost::asio::buffer(packet_.buffer(), CPacket::headerSize),
boost::bind(&CClient::recvHeader, shared_from_this(), boost::asio::placeholders::error)
);
What is packet_.buffer() and what do you do in recvHeader() ? Who calls recvMessage()?

What is packet_.buffer() and what do you do in recvHeader() ? Who calls recvMessage()?
packet_.buffer() returns the clients current buffer (buffer[512]) as char* This is the only place recvMessage is called --------------- void CClient::recvHeader(const boost::system::error_code& error) { if(!error && packet_.decode()) { boost::asio::async_read(socket_, boost::asio::buffer(packet_.buffer(), packet_.bodySize()), boost::bind(&CClient::recvMessage, shared_from_this(), boost::asio::placeholders::error) ); } else { disconnect(); } } In packet_.decode() the header is checked (through debugging I first notice the broken header before entering this function) and converted to the packet size and command, the size is used in packet_.bodySize(), which returns size_t *size

packet_.buffer() returns the clients current buffer (buffer[512]) as char*
Do you always supply the same buffer for any async_receive? So a new data always overwrites the old one? Then, maybe you calculate 1st message size incorrectly, and "zero" prefix you observe in the buffer before the beginning of the 2nd header might be just a trailing zero of the previous message. Try adding 1 to the calculated bodySize() :)

Do you always supply the same buffer for any async_receive? So a new data always overwrites the old one? Then, maybe you calculate 1st message size incorrectly, and "zero" prefix you observe in the buffer before the beginning of the 2nd header might be just a trailing zero of the previous message. Try adding 1 to the calculated bodySize() :)
Thank you! You were correct, I had forgotten to account for a trailing zero in packets and it was carrying over into following receives. I feel so foolish... Thanks again :)
participants (2)
-
Igor R
-
Matty Spatty