我如何使用 OpenSSL TLS 读取超过 16384 字节的数据?

How I can read more than 16384 bytes using OpenSSL TLS?

我正在尝试使用 OpenSSL TLS 套接字读取大量数据,但我总是卡在 16384 被读取。我怎样才能阅读更多?

SSL_CTX* ctx;
int server;
SSL* ssl;
int bytes;
std::string result;
std::vector<char> buffer(999999999);

ctx = InitCTX();
server = OpenConnection();
ssl = SSL_new(ctx);
SSL_set_fd(ssl, server);
if (SSL_connect(ssl) != -1)
{
    std::string msg = 0; //request here
    SSL_write(ssl, msg.c_str(), msg.size());
    bytes = SSL_read(ssl, &buffer[0], buffer.size());
}

result.append(buffer.cbegin(), buffer.cend());

TLS协议将数据封装在records that are individually encrypted and authenticated. Records have a maximum payload of 16 kB (minus a few bytes), and SSL_read()中,一次只处理一条记录。

我建议您将 buffer 的大小更改为 16384 字节以匹配。请注意,无论如何分配 ~1 GB 的内存太多了,因为其他进程可能无法使用该内存量。

然后就像评论里说的rustyx,循环看多一点就好了。如果另一方可以用多条记录进行响应,那么如果它能以某种方式在第一条记录中发送响应的大小就好了,这样你就会知道要读取多少。