卷曲导致openssl错误
Curl causes openssl error
我当前的curl设置调用一个网页,将其保存为字符串,并在睡眠一秒钟后重复该过程。这是要写入字符串的代码:
#include <curl/curl.h>
#include <string>
#include <iostream>
#include <thread>
#include <chrono>
size_t curl_writefunc(void* ptr, size_t size, size_t nmemb, std::string* data)
{
data->append((const char*)ptr, size * nmemb);
return size * nmemb;
}
void curl_handler(std::string& data)
{
int http_code = 0;
CURL* curl;
// Initialize cURL
curl = curl_easy_init();
// Set the function to call when there is new data
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_writefunc);
// Set the parameter to append the new data to
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
// Set the URL to download; just for this question.
curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/");
// Download
curl_easy_perform(curl);
// Get the HTTP response code
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
// Clean up
curl_easy_cleanup(curl);
curl_global_cleanup();
}
int main()
{
bool something = true;
std::string data;
while (something)
{
curl_handler(data);
std::cout << data << 'n';
data.clear();
std:: this_thread:: sleep_for (std:: chrono:: seconds(1));
}
}
然而,它在运行约20分钟后遇到了一个问题,这就是它给我带来的信息:
140377776379824:error:02001018:system library:fopen:Too many open files:bss_file.c:173:fopen('/etc/ssl/openssl.cnf','rb')
140377776379824:error:2006D002:BIO routines:BIO_new_file:system lib:bss_file.c:178:
140377776379824:error:0E078002:configuration file routines:DEF_LOAD:system lib:conf_def.c:199:
它似乎源于一个openssl文件,一旦在单个迭代中完成任务,它就不会关闭。如果迭代不止一次,打开的文件就会累积起来,并且在某个时刻必然会出现错误。我仍然是一个初级程序员,因此不想开始扰乱openSSL,所以我来这里问,是否有解决这类问题的方法。可以通过在调用函数之外声明curl对象来解决吗?
所要做的就是在获取数据之前简单地声明句柄及其设置。然后,只有实际的下载及其伴随的响应才会在循环中被重申。鼓励根据需要经常重用处理程序,因为它的部分资源(如在该会话中打开的文件)可能需要再次重新部署。
相关文章:
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- C++OpenSSL RSA_free如果也使用EVP_PKEY_free,则会给出分段错误
- 使用 VS2017 构建 OpenSSL 后出现一些链接错误。 " OPENSSL_init_ssl referenced in function _main " ?
- 致命错误:openssl/ssl.h:没有这样的文件或目录
- ssl3_read_bytes:TLSV1警报内部错误,安全套接字,OpenSSL,C ,获取HTTPS页面
- 如何正确处理OpenSSL错误(BIO)
- OpenSSL 库中的链接错误
- 致命错误:openssl/rsa.h:没有这样的文件或目录
- UE4包装错误:将问题与OpenSSL联系起来
- OpenSSL 错误"data greater than mod len"
- 试图编译Boost和openSSL时(使用WebSocket 或CPPRESTSDK)进行编译错误
- C++OpenSSL Ripemd-160给出错误的输出
- 卷曲导致openssl错误
- OpenSSL SHA256错误的结果
- 使用OpenSSL(Windows)的Qt中出现编译错误LNK2019
- Curl-openssl编译会产生大量链接器错误
- 获取Openssl证书错误中的错误字符串
- C++ 应用程序中的 OpenSSL 错误SSL_ERROR_SSL
- OpenSSL 错误"assertion failed"
- 编译带有 openSSL 错误的静态 Qt