gSOAP:soap_ssl_accept() 中没有套接字

gSOAP: No socket in soap_ssl_accept()

我正在尝试使用 SSL 设置示例 gSOAP 服务器。我创建了一个自签名证书,如下所示:

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes

然后将证书合并到一个文件中:

cat cert.pem key.pem >both.pem

然后 运行 这个代码:

#include <stdsoap2.h>
#include <stdexcept>
#include <sstream>
#include <iostream>

#define PORT 6667

int main(int argc, char **argv) {
    struct soap engine(SOAP_C_UTFSTRING);
    engine.bind_flags=SO_REUSEADDR;

    soap_ssl_noinit();

    int rc = soap_ssl_server_context(&engine,
            SOAP_SSL_DEFAULT,
            "both.pem", // keyfile
            nullptr, // password
            nullptr, // cacert
            nullptr, // capath
            nullptr, // dhfile
            nullptr, // randfile
            nullptr  // serverid
    );

    if (rc) {
            std::stringstream error;
            soap_stream_fault(&engine, error);
            throw std::runtime_error("soap_ssl_server context failed " + error.str());
    }

    rc = soap_bind(&engine, NULL, PORT, 100);
    if (rc < 0) {
            throw std::runtime_error("soap_bind failed " + std::to_string(rc));
    }

    int master_socket = rc;
    std::cout << "master socket: " << master_socket << std::endl;

    engine.accept_timeout = 50;
    engine.send_timeout = 50;
    engine.recv_timeout = 50;

    rc = soap_ssl_accept(&engine);
    if (rc) {
            soap_stream_fault(&engine, std::cout);
            throw std::runtime_error("soap_ssl_accept failed " + std::to_string(rc));
    }
    std::cout << "soap_ssl_accept " << rc << std::endl;

    return 0;
}

但我得到了这个输出:

master socket: 3
Error 30 fault detected[no subcode]
"SSL/TLS error"
Detail: No socket in soap_ssl_accept()
terminate called after throwing an instance of 'std::runtime_error'
  what():  soap_ssl_accept failed 30

我做错了什么?

我明白了。在调用 soap_ssl_accept 之前必须先调用 soap_accept。正确的代码是:

    rc = soap_accept(&engine);
    if (rc) {
            soap_stream_fault(&engine, std::cout);
            throw std::runtime_error("soap_accept failed " + std::to_string(rc));
    }

    rc = soap_ssl_accept(&engine);
    if (rc) {
            soap_stream_fault(&engine, std::cout);
            throw std::runtime_error("soap_ssl_accept failed " + std::to_string(rc));
    }

然后错误就修复了。