QT5 integrated Libcurl implementation of TFTP and FTP two methods: tftp file upload and download

Source: Internet
Author: User
Tags fread ftp commands

QT5 itself to the FTP and TFTP support is not very good, find a lot of places also did not find the easy way, but the use of open source curl to achieve. But how to use TFTP has not been found in the instructions. There's a bunch of examples under Curl's doc, but there's no TFTP, but the document says TFTP. The answer is that people feel that TFTP is too simple, are lazy to write a demo, ╮(╯▽╰)╭ deeply despised. Let me tell you how to use the TFTP feature.

1. Setting up a TFTP test environmentwriting code in the process of a problem at any time, once the environment is out of the question, everything is useless, so first set up a TFTP test environment, the method is very simple. In order to reduce the accident, it is best to use two computers (or computer + virtual machine, in short, as far as possible), each computer installed a TFTPD program, window comes with the function, but too troublesome, download a tftpd32.exe on the line. Open the Tftp32.exe program on one of the computers and select the "TFTP server" side as the server. Then select the "Client" option after another computer opens Tftp32.exe, as shown in:
Note The settings here:1. "Current directory" is the working directory of TFTP, the files in this directory can be directly TFTP transfer, the downloaded files are automatically placed here. 2. "Server Interface" is the native IP address, this should pay attention to network card and network segment, choose the wrong words will cause the transmission connection can not be established. 3.Host: Server IP Address, port: ports, tftp default is4.LocalFile: Local files, such as "Ds.txt". Remote File: Server-side file name, such as "2.2". Note that the settings here, if it is downloaded (get mode), is to save the server-side "2.2" file to local, the name is Ds.txt. If it is uploaded (put mode), the local file "Ds.txt" is uploaded to the server, the name is "2.2". If you repeat the operation, the file will be overwritten, so be aware of it here. When the transfer is complete, the following interface appears:
2.curl implementation file DownloadWell, if the test is successful, you can write the code with curl. The process is not elaborate, directly on the code:
/*upload*/#define LOCAL_FILE "D:/TMP/CURL.C" #define REMOTE_URL "Tftp://192.168.1.110:69/uploading.txt" Static siz  e_t read_callback (void *ptr, size_t size, size_t nmemb, void *stream) {curl_off_t nread; /* In real-world cases, this would probably get this data differently as this fread () stuff are exactly what the Librar  Y already would do by default internally */size_t Retcode = Fread (ptr, size, nmemb, (file*) stream);  Nread = (curl_off_t) retcode;  fprintf (stderr, "* * * We read%" curl_format_curl_off_t "bytes from file\n", nread); return retcode;}      int Tftpfileupload () {CURL *curl;      Curlcode Res;      FILE *hd_src;      struct stat file_info;      curl_off_t fsize;      struct Curl_slist *headerlist=null; /* Get the file size of the local file */if (stat (Local_file, &file_info)) {printf ("Couldnt open '%s ':%s")        \ n ", Local_file, Strerror (errno));      return 1;      } fsize = (curl_off_t) file_info.st_size; printf ("LocaL File Size:% "curl_format_curl_off_t" bytes.\n ", fsize);      /* Get a file * of the same file */hd_src = fopen (Local_file, "RB");      /* In Windows, this would init the Winsock stuff */Curl_global_init (Curl_global_all);      /* Get a curl handle */curl = Curl_easy_init (); if (curl) {/* We want to use our own read function */curl_easy_setopt (curl, curlopt_readfunction, Read_call        back);        /* Enable uploading */curl_easy_setopt (curl, Curlopt_upload, 1L);        /* Specify Target */curl_easy_setopt (Curl,curlopt_url, Remote_url); /* Pass in so last of FTP commands to run after the transfer */curl_easy_setopt (curl, Curlopt_postquote, Headerl        IST);        /* Now specify which file to upload */curl_easy_setopt (curl, Curlopt_readdata, HD_SRC);        Curl_easy_setopt (Curl, Curlopt_infilesize_large, (curl_off_t) fsize); /* Now run off and does what ' ve been told! */Res = CUrl_easy_perform (curl);                  /* Check for errors */if (res! = CURLE_OK) fprintf (stderr, "Curl_easy_perform () failed:%s\n",        Curl_easy_strerror (res));        /* Clean up the FTP commands list */Curl_slist_free_all (headerlist);      /* Always cleanup */curl_easy_cleanup (curl); } fclose (HD_SRC);      /* Close the local file */Curl_global_cleanup ();    return 0; }
external calling Method:
Downloadfilefromserver ();
When you do, you can find that the program saves "Tftps.cpp" on the server side to local. 3. Implement File Uploador directly on the code;
/*upload*/#define LOCAL_FILE "D:/TMP/CURL.C" #define UPLOAD_FILE_AS "uploading.txt" #define REMOTE_URL "Tftp://19 2.168.1.110:69/uploading.txt "#define RENAME_FILE_TO" renamed.txt "static size_t read_callback (void *ptr, size_t size,  size_t nmemb, void *stream) {curl_off_t nread; /* In real-world cases, this would probably get this data differently as this fread () stuff are exactly what the Librar  Y already would do by default internally */size_t Retcode = Fread (ptr, size, nmemb, (file*) stream);  Nread = (curl_off_t) retcode;  fprintf (stderr, "* * * We read%" curl_format_curl_off_t "bytes from file\n", nread); return retcode;}      int Tftpfileupload () {CURL *curl;      Curlcode Res;      FILE *hd_src;      struct stat file_info;      curl_off_t fsize;      struct Curl_slist *headerlist=null;      static const char buf_1 [] = upload_file_as;      static const char buf_2 [] = rename_file_to; /* Get the file size of the local file */if (stat (local_filE, &file_info)) {printf ("Couldnt open '%s ':%s\n", Local_file, Strerror (errno));      return 1;      } fsize = (curl_off_t) file_info.st_size;      printf ("Local File size:%" curl_format_curl_off_t "bytes.\n", fsize);      /* Get a file * of the same file */hd_src = fopen (Local_file, "RB");      /* In Windows, this would init the Winsock stuff */Curl_global_init (Curl_global_all);      /* Get a curl handle */curl = Curl_easy_init (); if (curl) {/* We want to use our own read function */curl_easy_setopt (curl, curlopt_readfunction, read_cal        Lback);        /* Enable uploading */curl_easy_setopt (curl, Curlopt_upload, 1L);        /* Specify Target */curl_easy_setopt (Curl,curlopt_url, Remote_url); /* Pass in so last of FTP commands to run after the transfer */curl_easy_setopt (curl, Curlopt_postquote, Headerl        IST); /* Now specify which file to upload */curl_easy_setopt (curl, Curlopt_readdata, HD_SRC);  /* Set the size of the file to upload (optional). If you give a *_large option must make sure that the type of the passed-in argument is a curl_off_ T. If you use Curlopt_infilesize (without _large), you must make sure so to pass in a type ' long ' argument.        */curl_easy_setopt (curl, Curlopt_infilesize_large, (curl_off_t) fsize); /* Now run off and does what ' ve been told!        */Res = curl_easy_perform (curl);                  /* Check for errors */if (res! = CURLE_OK) fprintf (stderr, "Curl_easy_perform () failed:%s\n",        Curl_easy_strerror (res));        /* Clean up the FTP commands list */Curl_slist_free_all (headerlist);      /* Always cleanup */curl_easy_cleanup (curl); } fclose (HD_SRC);      /* Close the local file */Curl_global_cleanup ();    return 0; }
Call method to
Tftpfileupload ();
OK, that's easy.


QT5 integrated Libcurl implementation of TFTP and FTP two methods: tftp file upload and download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.