简体   繁体   English

保持活动卷曲C htttps开机自检

[英]keep-alive curl C htttps POST

With this code I am sending strings to a webserver using libcurl and write the data to a MySQL (is done at the webserver). 通过此代码,我使用libcurl将字符串发送到Web服务器,并将数据写入MySQL(在Web服务器上完成)。 My Problem is that for every call of this function the program starts a new key exchange with the webserver. 我的问题是,对于此函数的每次调用,程序都会与Web服务器启动新的密钥交换。 I would like to have a persistant connection to the Server. 我想与服务器建立持久连接。 I searched here and web already and didnt find any satisfying solutions. 我已经在这里搜索和搜索网页,没有找到令人满意的解决方案。 Multi-handler and forced keep alive still open a new connection. 多处理程序和强制保持活动状态仍会打开新连接。

Following is my Code to establish an SSL Connection: 以下是我的建立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();

is Answered by Daniel Stenberg here , which is a similar/the same question. 由Daniel Stenberg 在这里回答,这是一个相似/相同的问题。

Re-use the same curl handle in subsequent requests! 在后续请求中重复使用相同的卷曲手柄! Don't call curl_easy_cleanup(curl) and curl_easy_init() again between them. 不要在它们之间再次调用curl_easy_cleanup(curl)和curl_easy_init()。

So the Solution is to call curl_easy_cleanup(curl) and curl_easy_init() only once. 因此,解决方案是只调用一次curl_easy_cleanup(curl)和curl_easy_init()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM