[英]C libcurl - measure download speed and time remaining
我正在使用以下代碼從 inte.net 下載文件:
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
int main(int argc, char** argv)
{
FILE *downloaded_file;
if ( (downloaded_file = fopen (download_path , "w" ) ) != NULL )
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "www.asd.com/files/file_to_download.rar");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, downloaded_file);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK)
{
printf("Download complete!\n");
}
}
fclose(downloaded_file);
}
}
如何測量當前下載速度(例如每秒)和完成下載的剩余時間?
您可以使用CURLOPT_PROGRESSFUNCTION
。 curl會將5個參數傳遞給您的回調函數clientp
, dltotal
, dlnow
, ultotal
和ulnow
。 clientp
是您隨CURLOPT_PROGRESSDATA
提供的指針。 總參數是需要下載的總數量。 現在是到目前為止的金額。 未知值為0。
要使用此功能,必須將CURLOPT_NOPROGRESS
為0。
CURLOPT_PROGRESSFUNCTION
自 v7.32.0 以來已被棄用,您可以使用CURLOPT_XFERINFOFUNCTION
,用法和回調結構(所有 arguments dltotal
, dlnow
...等)幾乎與CURLOPT_PROGRESSFUNCTION
相同
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.