【发布时间】:2015-06-24 18:44:57
【问题描述】:
我想使用 Boost ASIO + SSL 创建客户端/服务器通信程序对。所以我从 boost 提供的示例开始,我了解了它是如何工作的,我几乎准备好开发我的通信协议,除了有一个问题。
所以从this example开始,我正在修改握手后的handle_read()回调函数。以下是我的代码。我唯一的修改是:添加另一个名为startComm()的回调函数,它将开始通信。
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";
boost::asio::async_write(socket_,
boost::asio::buffer(std::string("Now?")),
boost::bind(&SSLClient::startComm, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
std::cout << "Read failed: " << error.message() << "\n";
}
}
void startComm(const boost::system::error_code& error,
size_t bytes_transferred)
{
if (!error)
{
std::cout << "Reply: ";
std::cout.write(reply_, bytes_transferred); //problem here, bytes transferred should contain the number of received chars not number of written chars
std::cout << "\n";
}
else
{
std::cout << "Read failed: " << error.message() << "\n";
}
}
在上面的async_write() 中,有一个参数boost::asio::placeholders::bytes_transferred 参数化我的回调函数以提供发送到服务器的字节数。现在我想知道服务器响应的字节数。在我的简单示例中如何做到这一点?
谢谢。如果您需要任何其他详细信息,请询问。
【问题讨论】:
标签: c++ ssl boost callback boost-asio