我从不同的Boost源中收集了这段代码,尝试创建连接到主机并通过SSL下载根页面/文件的东西。程序运行,但返回一个空的回复。有人能建议一下为什么会出现这种情况,并提出改进建议吗?链接与-l pthread -l ssl -l加密。
//
// client.cpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <iostream>
#include <asio.hpp>
#include <asio/ssl.hpp>
#include <functional>
static const char *HOSTNAME { "google.com" };
static const char *HOSTPORT { "https" };
std::string file { "/" };
std::string request {
"GET " + file + " "
+ "HTTP/1.1\r\nHost: " + std::string(HOSTNAME) + "\r\n\r\n" };
enum { max_length = 1024 };
class Client
{
asio::ssl::stream<asio::ip::tcp::socket> ssock;
char reply[max_length];
std::size_t bytes_transferred;
std::error_code ec;
public:
Client(asio::io_service &io_service, asio::ssl::context &context, asio::ip::tcp::resolver::iterator endpoint_iterator) :
ssock(io_service, context)
{
auto handle_connect { std::bind(&Client::handle_handshake, this, ec) };
asio::async_connect(ssock.lowest_layer(), endpoint_iterator, handle_connect);
}
void handle_connect(const std::error_code &ec)
{
if (!ec)
{
auto handle_handshake { std::bind(&Client::handle_handshake, this, ec) };
ssock.async_handshake(asio::ssl::stream_base::client, handle_handshake);
}
else std::cout << "Connect failed: " << ec.message() << "\n";
}
void handle_handshake(const std::error_code &ec)
{
if (!ec)
{
std::cout << request << std::endl;
auto handle_write { std::bind(&Client::handle_write, this, ec, bytes_transferred) };
asio::async_write(ssock, asio::buffer(request), handle_write);
}
else std::cout << "Handshake failed: " << ec.message() << "\n";
}
void handle_write(const std::error_code &ec, std::size_t bytes_transferred)
{
if (!ec)
{
auto handle_read { std::bind(&Client::handle_read, this, ec, bytes_transferred) };
asio::async_read(ssock, asio::buffer(reply, bytes_transferred), handle_read);
}
else std::cout << "Write failed: " << ec.message() << "\n";
}
void handle_read(const std::error_code &ec, std::size_t bytes_transferred)
{
if (!ec)
{
std::cout << "Reply: ";
std::cout.write(reply, bytes_transferred);
std::cout << "\n";
}
else std::cout << "Read failed: " << ec.message() << "\n";
}
};
int main(int argc, char* argv[])
{
try
{
asio::io_service io_service;
asio::ip::tcp::resolver resolver(io_service);
asio::ip::tcp::resolver::query query(HOSTNAME, HOSTPORT);
asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
asio::ssl::context ctx(asio::ssl::context::sslv23);
Client client(io_service, ctx, iterator);
io_service.run();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}我认为问题出在这些回调函数上。回调中的操作本身是正确的,因为它们直接取自示例。有没有更好的方法通过使用Lambdas进行回调来重写?我不喜欢这些回调被绑定的方式。
发布于 2020-12-17 07:34:11
auto handle_connect { std::bind(&Client::handle_handshake, this, ec) };在构造函数中应该是:
auto handle_connect { std::bind(&Client::handle_connect, this, ec) };https://stackoverflow.com/questions/65318598
复制相似问题