simple callback function

Callback function is hard to trace, but sometimes it is very useful. Especially when you are designing libraries. Callback function is like asking your user to gives you a function name, and you will call that function under certain condition.

For example, you write a callback timer. It allows you to specified the duration and what function to call, and the function will be callback accordingly. “Run myfunction() every 10 seconds for 5 times”

Or you can create a function directory, passing a list of function name and ask the library to callback accordingly. “Callback success() if success, callback fail() if failed.”

Lets look at a simple function pointer example

void cbfunc()
{
    printf("called");
}

int main ()
{
     /* function pointer */
    void (*callback)(void);

    /* point to your callback function */
    callback=(void *)cbfunc;
    
   /* perform callback */
   callback();

   return 0;
}

How to pass argument to callback function?
Observered that function pointer to implement callback takes in void *, which indicates that it can takes in any type of variable including structure. Therefore you can pass in multiple arguments by structure.

typedef struct _myst
{
    int a;
    char b[10];
}myst;

void cbfunc(myst *mt)
{
    fprintf(stdout,"called %d %s.",mt->a,mt->b);
}

int main()
{

    /* func pointer */
    void (*callback)(void *);

    //param
    myst m;
    m.a=10;
    strcpy(m.b,"123");
    
    /* point to callback function */ 
    callback = (void*)cbfunc;
    
    /* perform callback and pass in the param */
    callback(&m);

    return 0;

}

libcurl – HTTP,ftp,ssh,telnet,ldap client API

curl is a command line tool for transferring files with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS and FILE. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos…), file transfer resume, proxy tunneling and a busload of other useful tricks.

Quote from http://curl.haxx.se/

Advantages of using libcurl:

  • curl is open source.
  • libcurl API support multiple bindings.
  • libcurl is cross platform that supports windows and *nix.
  • API doc of libcurl are easy to follow, it also provides source code examples.
  • You do not need to read up RFC to code for HTTP post or ftp client, you just need to download libcurl-dev and try it out.

    Let me show you one of the simple example source code from curl official site.

    /*****************************************************************************
     *                                  _   _ ____  _
     *  Project                     ___| | | |  _ \| |
     *                             / __| | | | |_) | |
     *                            | (__| |_| |  _ <| |___
     *                             \___|\___/|_| \_\_____|
     *
     * $Id: simple.c,v 1.6 2004/08/23 14:22:52 bagder Exp $
     */
    
    #include &lt;stdio.h&gt;
    #include &lt;curl/curl.h&gt;
    
    int main(void)
    {
      CURL *curl;
      CURLcode res;
    
      curl = curl_easy_init();
      if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
        res = curl_easy_perform(curl);
    
        /* always cleanup */
        curl_easy_cleanup(curl);
      }
      return 0;
    }
    

    Check out the libcurl tutorial and the sample source codes.