使用此代码,我将使用libcurl将字符串发送到and服务器,并将数据写入MySQL (在and服务器上完成)。我的问题是,对于这个函数的每一个调用,程序都会与new服务器启动一个新的密钥交换。我希望有一个持久的连接到服务器。我已经在这里和网上搜索过了,没有找到任何令人满意的解决方案。多处理程序和被迫保持生命仍然打开一个新的连接。
以下是我的建立SSL连接的代码:
CURL *curl;
CURLcode res;
res = curl_global_init(CURL_GLOBAL_DEFAULT); // Check for errors
if(res != CURLE_OK) {
fprintf(stderr, "curl_global_init() failed: %s\n",
curl_easy_strerror(res));
return 1;
}
// curl handler
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, STRING);
curl_easy_setopt(curl, CURLOPT_URL, "https://IP/something/something.php");
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //verbose output activated
struct curl_slist *headers=NULL;
headers = curl_slist_append(headers, "Content-Type: application/json"); // type JSON
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Perform the request, res will get the return code
res = curl_easy_perform(curl);
// Check for errors
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
// cleanup
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
curl_global_cleanup();发布于 2015-05-28 11:14:41
由Daniel Stenberg here回答,这是一个类似的/相同的问题。
在后续请求中重复使用相同的卷发句柄!不要在它们之间再次调用curl_easy_cleanup(curl)和curl_easy_init()。
因此,解决方案是只调用curl_easy_cleanup(curl)和curl_easy_init()一次。
https://stackoverflow.com/questions/30503673
复制相似问题