gpt4 book ai didi

c++ - 使用 Boost Asio 建立 SSL 连接

转载 作者:太空宇宙 更新时间:2023-11-04 13:10:28 24 4
gpt4 key购买 nike

基本上有代码可以对服务器进行 http 调用并处理响应,但我想保护传输的数据并进行 https 调用。

为此我有这个 executor.hpp

class Executor
{
public :
Executor(Cli &cli):ssock(svc)
{
//! Get a list of endpoints corresponding to the server name.
boost::asio::ip::tcp::resolver resolver(svc);
boost::asio::ip::tcp::resolver::query query(cli.getIP(), "8080");
endpoint_iterator = resolver.resolve(query);
}

int connect();
int write(RequestCreator &requestcreator);

boost::asio::ssl::stream<boost::asio::ip::tcp::socket> &getSocket() { return ssock; }

private :

boost::asio::io_service svc;
boost::asio::ssl::context ctx(svc, ssl::context::method::sslv23_client);
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssock(svc, ctx);
boost::asio::ip::tcp::resolver::iterator endpoint_iterator;

};

和这个executor.cpp

int Executor::connect()
{
boost::system::error_code ec ;
boost::asio::connect(ssock.lowest_layer(), endpoint_iterator );
ssock.handshake(boost::asio::ssl::stream_base::handshake_type::client);

if (ec)
{
ssock.lowest_layer().close();
svc.stop();
return 0;
}
return 1;
}

int Executor::write(RequestCreator &requestCreator)
{
boost::system::error_code ec ;
boost::asio::write(ssock, requestCreator.getRequest(), ec);
if (ec)
{
ssock.lowest_layer().close();
svc.stop();
return 0;
}
return 1;
}

下面是编译器的完整错误

c:\cygwin64\home\admin\sandbox\webcli\executor.hpp(30) : error C2061:   syntax error : identifier 'svc'
c:\cygwin64\home\admin\sandbox\webcli\executor.hpp(31) : error C2061: syntax error : identifier 'svc'
c:\cygwin64\home\admin\sandbox\webcli\executor.hpp(15) : error C2436: 'ssock' : member function or nested class in constructor initializer list
c:\cygwin64\home\admin\sandbox\webcli\executor.hpp(25) : error C3867: 'Executor::ssock': function call missing argument list; use '&Executor::ssock' to create a pointer to member
c:\cygwin64\home\admin\sandbox\webcli\executor.hpp(25) : error C2440: 'return' : cannot convert from 'boost::asio::ssl::stream<Stream> (__cdecl Executor::* )(void)' to 'boost::asio::ssl::stream<Stream> &'
with
[
Stream=boost::asio::ip::tcp::socket
]

最佳答案

  1. 错误信息引用类Executor这不在您的代码中。你在编译正确的东西吗?

  2. boost::asio::ssl::context ctx(svc, ssl::context::method::sslv23_client);

    这不应该编译,除非svc也是您代码中的一种类型。使用大括号。

  3. boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssock(svc, ctx);

    这绝对不会编译。同样的问题。

  4. Connect(Data &data) : socket(ioService) {

    • 未声明套接字。它是什么? (假设您的意思可能是 ssock )。
    • ioService未声明。它是什么? (假设您的意思可能是 svc )。
  5. 对比

     int Connect::write(CreatRequest &requestCreator) {

     int write(RequestCreator &requestcreator);

    这是什么?编译器不会猜测你的意思。如果你的意思是 RequestCreator你必须输入那个,而不是 CreatRequest .

总而言之,没有真正的问题,只是大量盲目复制/粘贴“类代码”文本。

以最直接的方式修复以上所有问题:

Live On Coliru

#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>

struct Data {
std::string getIP() const { return "localhost"; }
};


class Connect {
public:
Connect(Data &data) {
//! Get a list of endpoints corresponding to the server name.
boost::asio::ip::tcp::resolver resolver(svc);
boost::asio::ip::tcp::resolver::query query(data.getIP(), "8080");
endpoint_iterator = resolver.resolve(query);
}

int connectServer();
struct RequestCreator {
auto getRequest() const { return boost::asio::buffer("GET / HTTP/1.1\r\n\r\n"); }
};
int write(RequestCreator &requestcreator);

boost::asio::ssl::stream<boost::asio::ip::tcp::socket> &getSocket() { return ssock; }

private:
boost::asio::io_service svc;
boost::asio::ssl::context ctx{svc, boost::asio::ssl::context::method::sslv23_client};
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssock{svc, ctx};
boost::asio::ip::tcp::resolver::iterator endpoint_iterator;
};

int Connect::connectServer() {
boost::system::error_code ec;

boost::asio::connect(ssock.lowest_layer(), endpoint_iterator);
if (ec) {
ssock.lowest_layer().close();
svc.stop();
return 0;
}
return 1;
}

int Connect::write(RequestCreator &requestCreator) {
boost::system::error_code ec;

boost::asio::write(ssock, requestCreator.getRequest(), ec);
if (ec) {
ssock.lowest_layer().close();
svc.stop();
return 0;
}
return 1;
}

main() {
Data data;
Connect c(data);
}

关于c++ - 使用 Boost Asio 建立 SSL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40232658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com