I have been thinking of putting this out for quite some time. In future this will find its home on stackoverflow (libcurl documentation) which still is in proposal state. This is very high level and only provides the basic guideline. There are many options and cases that are better documented on libcurl website.
Check https://curl.haxx.se/libcurl/c/libcurl-tutorial.html which provides many examples. Documentation for libcurl also provides small code snippets to use for each API. For some they even provide high level steps just like I mentioned below.
An example of usual socket use is
- create socket (socket)
- fill in the address (getaddrinfo etc)
- connect (connect)
- send full request with HTTP options (write)
- select on open sockets (select)
- process response (read, process and then close or repeat write/select/read sequence)
libcurl: For Single Transfer (blocking)
- create easy interface (curl_easy_init)
- configure easy interface with address and other HTTP options e.g. headers, compression, user agent etc (curl_easy_setopt with CURLOPT_* to specify options)
- provide callback to curl which will be invoked for various events e.g. when connection is established so POST data could be sent, read response, show progress etc.
- connect, select, read, write (curl_easy_perform which will invoke registered callbacks)
- close (curl_easy_cleanup) or reuse by resetting particular options
libcurl: For multiple Transfers (non-blocking)
- create a multi-interface (curl_multi_init) and configure (curl_mult_setopt)
- for each request create easy interface (curl_easy_init) and configure them (curl_easy_setopt) just like Single Transfer including callbacks.
- Register each easy interface to the multi-interface (curl_multi_add_handle). This is similar to doing FDSET before select call
- connect, select, read/write (curl_multi_perform which will invoke callbacks for each registered easy interface)
- cleanup (curl_multi_remove_handle for each closed connection)
- close (curl_easy_cleanup for each closed connection) or reuse by resetting particular options
- At the end use curl_multi_cleanup
libcurl: For applications with other sockets
- Perform all operation like multiple-transfers including registering with multi interface (curl_multi_add_handle).
- Instead of curl_multi_perform use curl_multi_fdset which fills in fdset objects to include sockets from each registered easy interface. Your application can use these fdset objects in its select call.
- When application’s select return success, first process your own sockets. Then call curl_mutli_perform. Make sure to call curl_multi_perform each time select returns (success or failure) otherwise timeouts specified to curl interfaces will not work.
- Perform cleanup or reuse as discussed in multiple-transfers above.
That’s all folks.