[英]Boost ASIO SSL number of bytes received
我想使用Boost ASIO + SSL创建客户端/服务器通信程序对。 因此,我从boost提供的示例开始,我了解了它是如何工作的,除了有一个问题外,我几乎准备开发我的通信协议。
因此,从本示例开始,我将在握手后修改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
可以参数化我的回调函数以提供发送到服务器的字节数 。 现在我想知道服务器响应的字节数 。 在我的简单示例中,我该怎么做?
谢谢。 如果您需要任何其他详细信息,请询问。
write
调用发送数据。
因为它不,在所有, 接收数据接收到的字节的数量是通过定义0。
如果要接收数据,请使用(async_)read
,它会告诉您接收到的字节数。
这些bytes_transferred
使用相同的占位符 ( bytes_transferred
),但根据已完成的传输方向,它具有不同的含义。
这是一个从技术上讲 startComm
您需要的解决方案:定义一个额外的startComm
参数并将其绑定(不带占位符)。
void handle_read(const boost::system::error_code &error, size_t bytes_received) {
if (!error) {
std::cout << "Reply: ";
std::cout.write(reply_, bytes_received);
std::cout << "\n";
boost::asio::async_write(socket_, boost::asio::buffer(std::string("Now?")),
boost::bind(&SSLClient::startComm,
this,
boost::asio::placeholders::error,
bytes_received,
boost::asio::placeholders::bytes_transferred));
} else {
std::cout << "Read failed: " << error.message() << "\n";
}
}
void startComm(const boost::system::error_code &error, size_t had_received, size_t bytes_sent) {
if (!error) {
std::cout << "Reply: ";
std::cout.write(reply_, had_received);
std::cout << "\n";
} else {
std::cout << "Write failed: " << error.message() << "\n";
}
}
请注意,我仍然认为您可能会误以为 async_write
会收到回复,(显然是这样)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.