Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7927553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:21:41+00:00 2026-06-03T19:21:41+00:00

I have to trasfer files using FTP protocol and libcurl in c. It works

  • 0

I have to trasfer files using FTP protocol and libcurl in c. It works fine, but I have some problems.

1) If a transfer is started, but at a certain point I disconnect from the network, my program remains in the libcurl function, the speed goes to 0 and I can’t do anything else. I tried to set timeout (CURLOPT_TIMEOUT), but it’s just a timeout on the transfer time.

2) The second problem I have, which is linked to the first one is, how can I know if the trasfer if successfully completed?

My trasfer code is:

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:21:50+00:00Added an answer on June 3, 2026 at 7:21 pm

    I have recently answered a similar question to this.

    1) this is by design. if you want to timeout the connection, try using these instead:

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

    If your download rate falls below your desired threshold, you can check the connectivity & take whatever action you see fit.

    NB: Added in 7.25.0: CURLOPT_TCP_KEEPALIVE, CURLOPT_TCP_KEEPIDLE

    so these might be another suitable alternative for you.

    2) like this:

    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");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this method to transfer files using a FTP Server: private void TransferNeededFiles(IEnumerable<string>
I have been using Indy to transfers files via FTP for years now but
I was uploading my file using below code,it works fine but some time it
i have a task to transfer text files from one pc to another using
I have to transfer large files between computers on via unreliable connections using WCF.
I want to transfer files using ftp in a simple and effective manner. For
I have to download some files from an FTP server. Seems prosaic enough. However,
I have try to transfer a image file using ftp command in linux, from
I'm using Net::FTP to transfer files up to a mainframe and I'm testing failure
I just started using QT. Right now I need to transfer some code I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.