libcurl 라이브러리를 사용하여 네트워크 업로드 다운로드 속도를 계산했습니다. 다음 코드를 사용하고 있습니다. 그러나 업로드 및 다운로드 속도는 원래 네트워크 속도와 비교하여 정확하지 않습니다. (내 출력은 약 1.3KBps, 업로드의 경우 원래 500KBps와 비교하여 370KBps, 다운로드의 경우 1MBps 임)libcurl : 네트워크 업로드 다운로드 속도가 정확하지 않습니다.
누군가가이 이유를 알 수 있고 적절한 업로드 다운로드 속도를 얻으려면 어떤 수정을해야하는지 매우 유용합니다. . 요금을 계산하는 새로운 절차도 환영합니다.
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
int main(void) {
CURL *curl;
CURL *curl1;
FILE *fp;
CURLcode res;
CURLcode res1;
char *url =
"https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency
_demonstration_1.png";
char outfilename[FILENAME_MAX] = "aaa.jpg";
curl = curl_easy_init();
curl1 = curl_easy_init();
struct stat file_info;
double speed_upload, total_time1;
FILE *fd;
fd = fopen("aaa.jpg", "rb");
if(!fd)
return 1;
if(fstat(fileno(fd), &file_info) != 0)
return 1;
if(curl1) {
curl_easy_setopt(curl1, CURLOPT_URL,
"https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png");
curl_easy_setopt(curl1, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl1, CURLOPT_READDATA, fd);
curl_easy_setopt(curl1, CURLOPT_INFILESIZE_LARGE,(curl_off_t)file_info.st_size);
curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L);
res1 = curl_easy_perform(curl1);
/* Check for errors */
if(res1 != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res1));
}
else {
curl_easy_getinfo(curl1, CURLINFO_SPEED_UPLOAD, &speed_upload);
curl_easy_getinfo(curl1, CURLINFO_TOTAL_TIME, &total_time1);
fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
speed_upload, total_time1);
}
curl_easy_cleanup(curl1);
fclose(fd);
}
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
if (CURLE_OK == res) {
double val;
res = curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &val);
if ((CURLE_OK == res) && (val>0))
printf("Data downloaded: %0.0f bytes.\n", val);
res = curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &val);
if ((CURLE_OK == res) && (val>0))
printf("Total download time: %0.3f sec.\n", val);
res = curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &val);
if ((CURLE_OK == res) && (val>0))
printf("Average download speed: %0.3f kbyte/sec.\n", val/1024);
}
else {
fprintf(stderr, "Error while fetching '%s' : %s\n",
url, curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}