首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >保持活力柱

保持活力柱
EN

Stack Overflow用户
提问于 2015-05-28 10:17:34
回答 1查看 3.9K关注 0票数 1

使用此代码,我将使用libcurl将字符串发送到and服务器,并将数据写入MySQL (在and服务器上完成)。我的问题是,对于这个函数的每一个调用,程序都会与new服务器启动一个新的密钥交换。我希望有一个持久的连接到服务器。我已经在这里和网上搜索过了,没有找到任何令人满意的解决方案。多处理程序和被迫保持生命仍然打开一个新的连接。

以下是我的建立SSL连接的代码:

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-28 11:14:41

由Daniel Stenberg here回答,这是一个类似的/相同的问题。

在后续请求中重复使用相同的卷发句柄!不要在它们之间再次调用curl_easy_cleanup(curl)和curl_easy_init()。

因此,解决方案是只调用curl_easy_cleanup(curl)和curl_easy_init()一次。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30503673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档