c libcurl ftp上传不完整 - 缺少1个字节

时间:2016-07-08 08:57:56

标签: c curl ftp libcurl

我尝试使用libcurl来测试下载和上传速度。我在上传方面遇到了这个奇怪的问题:我通过ftp上传的文件系统地丢失了1个字节。请注意,我没有阅读文件,而是在read_callback的{​​{1}}中动态生成数据,因为我感兴趣的是速度并记录数据本身。请参阅下面的代码,该代码主要基于此示例:libcurl chkspeed example

curl

当我运行这个时,我得到了这个输出:

  

ul_total_bytes = 1000000

     

ul_total_bytes = 1000000

     

获取时出错' ftp:// xxx.xxx.xxx.xxx/curl_upload_test' :转移了部分文件

但如果我发表评论#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <curl/curl.h> #define UL_FTP_USR "user" #define UL_FTP_PWD "password" #define UL_TARGET_URL "ftp://xxx.xxx.xxx.xxx/curl_upload_test" #define UL_SIZE 1000000 long ul_total_bytes = 0; static size_t read_callback_full(void *ptr, size_t size, size_t nmemb, void *stream) { if(ul_total_bytes >= UL_SIZE) { printf("ul_total_bytes = %d\n", ul_total_bytes); return 0; // we stop upload at this point } char *pp = (char *) ptr; int i = 0; for(; i < size*nmemb; i++) { pp[i] = i; ul_total_bytes += 1; if(ul_total_bytes >= UL_SIZE) { printf("ul_total_bytes = %d\n", ul_total_bytes); return i; // if we reach UL_SIZE_LIM we send last payload } } return i; } int main(int argc, char *argv[]) { const char *url = UL_TARGET_URL; CURL *curl_handle; CURLcode res; // init libcurl curl_global_init(CURL_GLOBAL_ALL); // init the curl session curl_handle = curl_easy_init(); // specify URL to put curl_easy_setopt(curl_handle, CURLOPT_URL, url); //---------- FTP CREDENTIALS ----------- curl_easy_setopt(curl_handle, CURLOPT_USERNAME, UL_FTP_USR); curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, UL_FTP_PWD); //-------------------------------------- // tell it to "upload" to the URL curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, 1L); // bogus pointer. We don't read file, we generate data in read callback on the fly int p = 0; curl_easy_setopt(curl_handle, CURLOPT_READDATA, &p); // register read callback curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, read_callback_full); // set the file size curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, UL_SIZE); // some servers don't like requests that are made without a user-agent field, so we provide one curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-speedchecker"); // useful here ??? // execute the request! res = curl_easy_perform(curl_handle); //if(CURLE_OK == res) { double val; // check for bytes downloaded res = curl_easy_getinfo(curl_handle, CURLINFO_SIZE_UPLOAD, &val); if((CURLE_OK == res) && (val>0)) printf("Data uploaded: %0.0f bytes.\n", val); // check for average upload speed res = curl_easy_getinfo(curl_handle, CURLINFO_SPEED_UPLOAD, &val); if((CURLE_OK == res) && (val>0)) printf("Average upload speed: %0.3f kbyte/sec.\n", val / 1024); // check for name resolution time res = curl_easy_getinfo(curl_handle, CURLINFO_NAMELOOKUP_TIME, &val); if((CURLE_OK == res) && (val>0)) printf("Name lookup time: %0.3f sec.\n", val); // check for total time res = curl_easy_getinfo(curl_handle, CURLINFO_TOTAL_TIME, &val); if((CURLE_OK == res) && (val>0)) printf("Total time: %0.3f sec.\n", val); //} //else { fprintf(stderr, "Error while fetching '%s' : %s\n", url, curl_easy_strerror(res)); //} // cleanup curl stuff curl_easy_cleanup(curl_handle); // we're done with libcurl, so clean it up curl_global_cleanup(); /* curl_version_info_data *data = curl_version_info(CURLVERSION_NOW); printf("Version: %s\n", data->version); printf("Host: %s\n", data->host); printf("SSL-Version: %s\n", data->ssl_version); printf("Libz-Version: %s\n", data->libz_version); */ return 0; } ,那么我就明白了:

  

ul_total_bytes = 1000000

     

ul_total_bytes = 1000000

     

上传的数据:999999字节。

     

平均上传速度:386.591千字节/秒。

     

名称查找时间:0.000秒。

     

总时间:2.526秒

     

获取时出错&#39; ftp:// xxx.xxx.xxx.xxx/curl_upload_test' :没有错误

事实上,我在if(CURLE_OK == res)上找到的文件是999.999字节...

这是版本信息:

  

版本:7.21.1

     

主持人:i586-common_3_0-linux-gnu

     

SSL-Version:OpenSSL / 1.0.0a

     

Libz-Version:1.2.3

代码必须在嵌入式系统上运行,因此我几乎坚持使用制造商提供的ftp版本。

0 个答案:

没有答案