Source code Analysis of Second Life (60) introduction of multi-protocol file transfer Library Libcurl

Source: Internet
Author: User
Tags http post
Network development is fast, more and more network transfer protocols appear, no matter what kind of developers face these protocols are difficult work, often in a program has a variety of different protocol requirements. Like the web today, if you don't use the HTTP protocol, you seem to be lagging behind. Of course, the HTTP protocol is more popular and universal, as long as the Internet can transmit data, this environment for the applicability of the application is very good. Although the HTTP protocol is relatively simple, but let you develop it from scratch, still need a relatively long time, then there is no faster way. The answer is yes, here is a network protocol to compare the whole, but also a practical libcurl library. This library implements protocols such as FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, Ldaps, and file, and also supports SSL authentication, HTTP post,http put,ftp upload, HTTP upload, agent, COOKIES, user password, file renewal and other functions.   Libcurl libraries are also libraries that support the execution of different platforms. such as Solaris, NetBSD, FreeBSD, OpenBSD, Darwin, HPUX, IRIX, AIX, Tru64, Linux, UnixWare, HURD, Windows, Amiga, OS/2, BeOs, M AC os X, Ultrix, QNX, OpenVMS, RISC os, Novell NetWare, DOS, and so on.   Start learning how to use the Libcurl library to construct a simple network client program. The function Curl_global_init () function is called first to initialize the library function, but the function argument is passed in, and the arguments are as follows: curl_global_win32  This is the initialization of the WIN32 platform content. curl_global_ssl  This is the ability to initialize SSL. Another macro is Curl_global_all, which is a combination of two values above.   In fact, call the function Curl_easy_init () to initialize a handle, which is invoked as follows: Easyhandle = Curl_easy_init ();   Then call the function curl_easy_setopt to set the network address. as follows: Curl_easy_setopt (handle, Curlopt_url, "http://domain.com/"); So that you can set up the network address, then return toThe data is how to pass to the customer's application. This is the case, first write a callback function, and then set the callback function to the Libcurl library, and then specify what the data format to receive, when the Libcurl library returns data will be callback set function. As follows: Define callback functions: size_t write_data (void *buffer, size_t size, size_t nmemb, void *userp); Set callback function: Curl_easy_setopt (Easyhandle, curlopt_writefunction, Write_data); Sets the data format that the callback function receives: curl_easy_setopt (Easyhandle, Curlopt_writedata, &internal_struct);   Through the above settings, you can receive data to the callback function inside, then how to command the Libcurl library to start to download data or upload data. The following describes the function Curl_easy_perform, this function is connected to the remote Web site, began to do protocol-related command operations, the invocation of the examples are as follows: Success = Curl_easy_perform (Easyhandle);   Through the above series of calls, you can connect to the HTTP server, and download the corresponding data, and upload to the callback function. When you are finished using this library, do not forget to call Function Curl_global_cleanup () to do the cleanup work.

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.