如何保持与 libcurl 的连接?
我想要的用法如下。Keep-Alive我想连接到支持但在 90 秒不活动后关闭连接的服务器。当某些事件(在此连接之外)发生时,我想POST偶尔执行一次,并且我想保持连接处于活动状态以减少延迟。可能会发生超过 90 秒没有事件发生的情况,所以我想要一种方法来告诉服务器我没有空闲。
示例页面非常好,但我没有看到涉及Keep-Alive: https: //curl.haxx.se/libcurl/c/example.html
我确实找到了CURLOPT_TCP_KEEPINTVL: https: //curl.haxx.se/libcurl/c/CURLOPT_TCP_KEEPINTVL.html,但我不清楚它应该如何工作。在该示例中,我们看到代码:
CURL *curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
/* enable TCP keep-alive for this transfer */
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
/* set keep-alive idle time to 120 seconds */
curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 120L);
/* interval time between keep-alive probes: 60 seconds */
curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 60L);
curl_easy_perform(curl);
}
Run Code Online (Sandbox Code Playgroud)
描述如下:
过了很长。设置操作系统在发送保活探测之间等待的时间间隔(以秒为单位)。并非所有操作系统都支持此选项。(7.25.0 中添加)
两个问题:
A) 在操作上,我该如何使用它?从描述来看,只要我保持curl在范围内,连接就会保持打开状态。它是否正确?然后我应该继续做吗
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis); …Run Code Online (Sandbox Code Playgroud)