gpt4 book ai didi

c - libcurl c,超时和成功传输

转载 作者:太空宇宙 更新时间:2023-11-04 04:02:05 28 4
gpt4 key购买 nike

我必须在 c 中使用 FTP 协议(protocol)和 libcurl 传输文件。它工作正常,但我遇到了一些问题。

1) 如果开始传输,但在某个时刻我断开了网络连接,我的程序仍然在 libcurl 函数中,速度变为 0,我无法做任何其他事情。我尝试设置超时(CURLOPT_TIMEOUT),但它只是传输时间的超时。

2) 我遇到的第二个问题与第一个问题相关,我如何知道传输是否成功完成?

我的转移码是:

struct FtpFile {

const char *filename;
FILE *stream;
};
long int size;

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
struct FtpFile *out=(struct FtpFile *)stream;
if(out && !out->stream) {
/* open file for writing */
out->stream=fopen(out->filename, "ab");
if(!out->stream)
return -1; /* failure, can't open file to write */
}
return fwrite(buffer, size, nmemb, out->stream);
}

int sz;

int main(void)
{
CURL *curl;
CURLcode res;
char prova;

struct stat statbuf;
FILE *stream;

/* apro il file per leggere la dimensione*/
if ((stream = fopen("Pdf.pdf", "rb"))
== NULL)
{
fprintf(stderr, "Nessun file da aprire, il download partirà da zero.\n");
}
else
{
/* Ricevo informazioni sul file */
fstat(fileno(stream), &statbuf);
fclose(stream);
size = statbuf.st_size;
printf("Dimensione del file in byte: %ld\n", size);
}

struct FtpFile ftpfile={
"Pdf.pdf", /* name to store the file as if succesful */
NULL
};

curl_global_init(CURL_GLOBAL_DEFAULT);

curl = curl_easy_init();
if(curl) {

curl_easy_setopt(curl, CURLOPT_URL, "ftp://....");
/* Define our callback to get called when there's data to be written */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
/* Set a pointer to our struct to pass to the callback */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);

/* Switch on full protocol/debug output */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

/*Pass a long as parameter. It contains the offset in number of bytes that you want the transfer to start from.
Set this option to 0 to make the transfer start from the beginning (effectively disabling resume). For FTP, set
this option to -1 to make the transfer start from the end of the target file (useful to continue an interrupted
upload).

When doing uploads with FTP, the resume position is where in the local/source file libcurl should try to resume
the upload from and it will then append the source file to the remote target file. */

if(stream == NULL)
{
curl_easy_setopt(curl, CURLOPT_RESUME_FROM, 0);
}
else
{
curl_easy_setopt(curl, CURLOPT_RESUME_FROM, size);
}

/*Used to show file progress*/
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);

res = curl_easy_perform(curl);

/* always cleanup */
curl_easy_cleanup(curl);

if(CURLE_OK != res) {
/* we failed */
fprintf(stderr, "curl told us %d\n", res);
}
}

if(ftpfile.stream)
fclose(ftpfile.stream); /* close the local file */

curl_global_cleanup();

return 0;
}

最佳答案

我最近回答了一个 similar question对此。

1) 这是设计使然。如果您想使连接超时,请尝试使用这些:

curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, dl_lowspeed_bytes);    
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, dl_lowspeed_time);

如果您的下载速率低于您想要的阈值,您可以检查连接并采取您认为合适的任何操作。

注意:在 7.25.0 中添加:CURLOPT_TCP_KEEPALIVE, CURLOPT_TCP_KEEPIDLE
所以这些可能是另一个适合您的选择。


2) 像这样:

curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &dl_bytes_remaining);
curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &dl_bytes_received);
if (dl_bytes_remaining == dl_bytes_received)
printf("our work here is done ;)\n");

关于c - libcurl c,超时和成功传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10486119/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com