Here's the code I use:
class Server
{
.....
void Server::accepted()
{
std::cout << "Accepted!" << std::endl;
boost::array<char, 1> buf;
boost::asio::async_read(socket, boost::asio::buffer(buf),
boost::bind(&Server::handleRead, this, buf, boost::asio::placeholders::error));
}
void Server::handleRead(boost::array<char, 1> buf, const boost::system::error_code& error)
{
if(!error)
{
std::cout << "Message: " << buf.data() << std::endl;
}
else
{
std::cout << "Error occurred." << std::endl;
}
}
.....
}
The problem is that I always get the same data from the client: a specific char. In my client I tried sending other char, but still the server shows the same char.
And when I try to read more than 1 bytes, I get an error that the buf
variable is used before it's initialized.