Quick Premier on migration to Curl for a socket programmer

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

  1. create socket (socket)
  2. fill in the address (getaddrinfo etc)
  3. connect (connect)
  4. send full request with HTTP options (write)
  5. select on open sockets (select)
  6. process response (read, process and then close or repeat write/select/read sequence)

libcurl: For Single Transfer (blocking)

  1. create easy interface (curl_easy_init)
  2. configure easy interface with address and other HTTP options e.g. headers, compression, user agent etc (curl_easy_setopt with CURLOPT_* to specify options)
  3. 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.
  4. connect, select, read, write (curl_easy_perform which will invoke registered callbacks)
  5. close (curl_easy_cleanup) or reuse by resetting particular options

libcurl: For multiple Transfers (non-blocking)

  1. create a multi-interface (curl_multi_init) and configure (curl_mult_setopt)
  2. for each request create easy interface (curl_easy_init) and configure them (curl_easy_setopt) just like Single Transfer including callbacks.
  3. Register each easy interface to the multi-interface (curl_multi_add_handle). This is similar to doing FDSET before select call
  4. connect, select, read/write (curl_multi_perform which will invoke callbacks for each registered easy interface)
  5. cleanup (curl_multi_remove_handle for each closed connection)
  6. close (curl_easy_cleanup for each closed connection) or reuse by resetting particular options
  7. At the end use curl_multi_cleanup

libcurl: For applications with other sockets

  1. Perform all operation like multiple-transfers including registering with multi interface (curl_multi_add_handle).
  2. 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.
  3. 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.
  4. Perform cleanup or reuse as discussed in multiple-transfers above.

That’s all folks.