Initial Implementation of the TFP upload/download function in libcurl

Source: Internet
Author: User
Tags fread ftp server url


The objective of this learning note is to use libcurl to upload and download FTP files.

1. Introduction to libcurlde

Libcurl is a free and easy-to-use library that uses URLs for file transfer ., Libcurl currently supports dict, file, FTP, ftps, Gopher, HTTP, https, IMAP, IMAPs, LDAP, LDAPS, POP3, pop3s, rtmp, RTSP, SCP, SFTP, SMTP, smtps, telnet and TFTP protocol. Libcurl also supports HTTPS certificate authorization, http post, http put, FTP upload, HTTP basic form upload, proxy, cookies, and user authentication.

Libcurl is a lightweight library that can run on multiple platforms after compilation, including Solaris, NetBSD, FreeBSD, OpenBSD, Darwin, HPUX, Irix, Aix, Tru64, Linux, unixware, hurd, windows, amiga, OS/2, BEOs, Mac OS x, Ultrix, QNX, OpenVMS, risc OS, Novell Netware, DOS, etc.

 

Ii. libcurl Interface

Libcurl is divided into simple and complex interfaces. A simple interface is synchronous, efficient, and fast. This excuse is mainly used for file transmission. A large number of applications use this method. The complex elder sister is asynchronous, so I will not study it too much here.

 

3. libcurl simple interface Workflow

1. Use curl_easy_init () in libcurl to initialize a simple file transfer session and obtain a handle

2. You can use curl_easy_setopt () to set all option parameters. The most important of these parameters is the URL. You may also need to set some callback functions, these callback functions will be called when the conditions are met.

3. When all the above work is done, you can use curl_easy_perform () to notify libcurl for file transfer, this function returns a value only when the entire transfer process is completed or fails.

4. After you perform Step 4, you can use curl_easy_getinfo () to obtain the transmitted information.

5. Finally, you can use curl_easy_cleanup () to end the file session.

The following describes the five steps to compile an instance for testing.

 

Iv. Introduction to some functional functions

1. curl * curl_easy_init ();

This function must be called first, and a curl * type handle will be returned. This function will be used with curl_easy_cleanup. If an error occurs during initialization, the function returns a null value;

2. Void curl_easy_cleanup (curl * handle)

This function must be called at last. Its parameter should be the return value of curl_easy_init;

3. curlcode curl_easy_setopt (curl * handle, curloption option, parameter );

From the perspective of the name, this function knows that some parameters are set. Setting different parameters will change the function of libcurl. Please read the instructions carefully, once the setting method is incorrect, the error will be fatal.

The usage of specific parameters is described in detail on this URL.

Http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

4. curlcode curl_easy_perform (curl * easy_handle );

As we mentioned earlier, this function should be used after init and setopt. Once executed, this function will notify libcurl to execute all the methods we set earlier, it is worth noting that when a handle is added to a multi handle, the handle parameter cannot be called by the function perform.

V. instance demonstration

1. Here is a simple example:

# Include <stdio. h> # include <curl/curl. h> int main (INT argc, char ** argv) {curl * pcurlhandle = NULL; curlcode errorcode = 0;/* 1. initialize a pcurlhandle */pcurlhandle = curl_easy_init (); If (pcurlhandle) {/* 2. set the URL of pcurlhandle */curl_easy_setopt (pcurlhandle, curlopt_url, "http://blog.csdn.net/jxnu_xiaobing");/* 3. because the above URL has been redirected, set it to curlopt_followlocation */curl_easy_setopt (pcurlhandle, curlopt_followlocat Ion, 1l);/* 4. Start executing the above opt */errorcode = curl_easy_perform (pcurlhandle); If (errorcode! = Curle_ OK) {printf ("curl_easy_perform () failed \ n") ;}} else {printf ("curl_easy_init () failed \ n");}/* 5. close pcurlhandle */curl_easy_cleanup (pcurlhandle); Return 0 ;}


2. The above example is a simple example of accessing a webpage. Next we will continue to learn an FTP upload example.

# Include <stdio. h> # include <string. h> # include <curl/curl. h> # include <sys/types. h> # include <sys/STAT. h> # include <fcntl. h> # include <errno. h> # include <unistd. h> # definelocal_file "/tmp/xiaobing.txt" # defineremote_url "ftp: // container: 21/xiaobing.txt" static partition (void * PTR, size_t size, size_t nmemb, void * Stream) {curl_off_t nread;/* in real-world cases, this wowould probablyget this data dif Ferently as this fread () stuff is exactly what thelibrary already wowould 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 main (INT argc, char ** argv) {curl * pcurlhandle; curlcode res; file * hd_src; struct stat file_info; curl_off_t FS Ize;/* 1. get the size of the file to be uploaded */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);/* 2. get the descriptor */hd_src = fopen (local_file, "rb") of the file to be uploaded;/* 3. initialize all possible calls */curl_global_init (curl_global_all);/* 4. create a curlhandle */pcurlhandle = curl_easy_ini T (); If (curl) {/* 5. set a callback function */curl_easy_setopt (pcurlhandle, curlopt_readfunction, read_callback);/* 6. enable upload flag */curl_easy_setopt (pcurlhandle, curlopt_upload, 1l);/* 7. specify the FTP server URL */curl_easy_setopt (pcurlhandle, curlopt_url, remote_url);/* 8. specify the file to be uploaded */curl_easy_setopt (pcurlhandle, curlopt_readdata, hd_src);/* 9. set the size of the file to be uploaded. Note: When the parameter is curlopt_infilesize_large, the size type should be curl_off_t. When the parameter is curlopt_infilesize, the size should be of the long type */curl_easy_setopt (pcurlhandle, curlopt_infilesize_large, (curl_off_t) fsize);/* 10. start to execute the method we set above */RES = curl_easy_perform (pcurlhandle);/* 11. check whether execution is successful */If (res! = Curle_ OK) fprintf (stderr, "curl_easy_perform () failed: % s \ n", curl_easy_strerror (RES);/* 12. disable pcurlhandle */curl_easy_cleanup (pcurlhandle);} fclose (hd_src);/* close the local file * // * 13. clear all possible calls */curl_global_cleanup (); Return 0 ;}

The above program will be able to upload files through FTP


3. Finally, let's look at a TFP download instance:

/*************************************** **************************************** * ******* File Name: tfpcurldownload. c ** function: curl test program ** Author: http://blog.csdn.net/King_BingGe ** Creation Time: September 29, 2014 ** last modification time: September 29, 2014 ** details: no *************************************** **************************************** * *******/# include <stdio. h> # include <string. h> # include <curl/curl. h> # include <sys/types. h> # In Clude <sys/STAT. h> # include <fcntl. h> # include <errno. h> # include <unistd. h> # define remote_url "ftp: // 172.30.26.247: 21/xiao.txt" typedef struct ftpfile {const char * filename; file * stream;} cftpfile; static size_t write_callback (void * PTR, size_t size, size_t nmemb, void * stream) {cftpfile * out = (cftpfile *) stream; If (Out &&! Out-> stream) {out-> stream = fopen (out-> filename, "WB"); If (! Out-> stream) {printf ("write_callback failed \ n"); Return-1 ;}return fwrite (PTR, size, nmemb, out-> stream );} int main (INT argc, char ** argv) {curl * pcurlhandle; curlcode res; curl_off_t fsize; cftpfile ftpfile = {"/tmp/xiaoxiao.txt", null};/* 3. initialize all possible calls */curl_global_init (curl_global_all);/* 4. create a curlhandle */pcurlhandle = curl_easy_init (); If (pcurlhandle) {/* 5. set a callback function */curl_easy_se Topt (pcurlhandle, curlopt_writefunction, write_callback);/* 7. specify the FTP server URL */curl_easy_setopt (pcurlhandle, curlopt_url, remote_url);/* 8. file to be uploaded */curl_easy_setopt (pcurlhandle, curlopt_userpwd, "cdn_ftp: 123456");/* 9. specify the file to be uploaded */curl_easy_setopt (pcurlhandle, curlopt_writedata, & ftpfile);/* 10. set the size of the file to be downloaded. Note: When the parameter is curlopt_infilesize_large, the size type should be curl_off_t. When the parameter is curlopt_infi Lesize should be of the long type */curl_easy_setopt (pcurlhandle, curlopt_infilesize_large, (curl_off_t) fsize);/* 11. start to execute the method we set above */RES = curl_easy_perform (pcurlhandle);/* 12. check whether execution is successful */If (res! = Curle_ OK) fprintf (stderr, "curl_easy_perform () failed: % s \ n", curl_easy_strerror (RES);/* 13. disable pcurlhandle */curl_easy_cleanup (pcurlhandle);} else {printf ("curl_easy_init failed \ n");}/* 14. clear all possible calls */curl_global_cleanup (); Return 0 ;}




Initial Implementation of the TFP upload/download function in libcurl

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.