In the following I'm creating a client program starting from the example provided by boost asio.
So I tried to add another cycle of read and write. The first time, it reads 6 bytes, which is the right length, but it reads only 4 bytes the second time for some reason. Here's my code based from the example and just repeated:
void handle_write(const boost::system::error_code& error, size_t bytes_transferred)
{
if (!error)
{
boost::asio::async_read(socket_,
boost::asio::buffer(reply_, bytes_transferred),
boost::bind(&SSLClient::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
std::cout << "Write failed: " << error.message() << "\n";
}
}
void handle_read(const boost::system::error_code& error, size_t bytes_transferred)
{
if (!error)
{
std::cout << "Reply: ";
std::cout.write(reply_, bytes_transferred);
std::cout << "\n";
//So here I added another function to write again, which is 4 letters long
boost::asio::async_write(socket_,
boost::asio::buffer(std::string("Now?")),
boost::bind(&SSLClient::handle_write_again, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
std::cout << "Read failed: " << error.message() << "\n";
}
}
void handle_write_again(const boost::system::error_code& error, size_t bytes_transferred)
{
if (!error)
{
boost::asio::async_read(socket_,
boost::asio::buffer(reply_, bytes_transferred),
boost::bind(&SSLClient::handle_read_again, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
std::cout << "Write failed: " << error.message() << "\n";
}
}
void handle_read_again(const boost::system::error_code& error, size_t bytes_transferred)
{
if (!error)
{
//here, only 4 letters are being read again... Why???
std::cout << "Reply: ";
std::cout.write(reply_, bytes_transferred);
std::cout << "\n";
}
else
{
std::cout << "Read failed: " << error.message() << "\n";
}
}
Now the problem is that the server sends the same message twice. The message is 6 letters. Over the first time, the message is received successfully with no problems. The second time, only 4 letters are received.
Why would this problem happen when I'm just reusing the same callback functions?
Aucun commentaire:
Enregistrer un commentaire