summaryrefslogtreecommitdiff
path: root/README.libcurl
blob: ccec7615005f9d60d17a442def65e075b84e8e43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
                         _ _ _                     _ 
                        | (_) |__   ___ _   _ _ __| |
                        | | | '_ \ / __| | | | '__| |
                        | | | |_) | (__| |_| | |  | |
                        |_|_|_.__/ \___|\__,_|_|  |_|


                     How To Use Libcurl In Your Program:
                                 (by Ralph Beckmann <rabe@uni-paderborn.de>)

NOTE: If you plan to use libcurl.a in Threads under Linux, do not use the old
gcc-2.7.x because the function 'gethostbyname' seems not to be thread-safe,
that is to say an unavoidable SEGMENTATION FAULT might occur.


1. a) In a C-Program:
      #include "curl.h"
   
   b) In a C++-Program:
      extern "C" {
      #include "curl.h"
      }
 
2. char *url="http://www.domain.com";
   curl_urlget (URGTAG_URL, url, 
          URGTAG_FLAGS, CONF_NOPROGRESS,
          URGTAG_ERRORBUFFER, errorBuffer,
          URGTAG_WRITEFUNCTION, (size_t (*)(void *, int, int, FILE
*))handle_data,
          URGTAG_TIMEOUT, 30,         /* or anything You want             */
   ...
          URGTAG_DONE);

3. size_t handle_data (const void *ptr, size_t size, size_t nitems,
                       FILE *stream)
   {
      (void)stream;                   /* stop complaining using g++ -Wall */
      if ((int)nitems <= 0) {
         return (size_t)0;
      }
      fprintf(stdout, (char *)ptr);   /* or do anything else with it      */
      return nitems;
   }

4. Compile Your Program with  -I$(CURL_DIR)/include

5. Link Your Program together with  $(CURL_DIR)/lib/libcurl.a

                     Small Example of How To Use libcurl

----------------------------------------------------------------------
/* Full example that uses libcurl.a to fetch web pages.                    */
/* curlthreads.c                                                           */
/* - Test-Program by Ralph Beckmann for using curl in POSIX-Threads        */
/* Change *url1 and *url2 to textual long and slow non-FRAMESET websites!  */
/* 
   1. Compile with gcc or g++ as $(CC): 
       $(CC) -c -Wall -pedantic curlthreads.c -I$(CURL_DIR)/include

   2. Link with:
      - Linux:
        $(CC) -o curlthreads curlthreads.o $(CURL_DIR)/lib/libcurl.a -lpthread
-lm 
      - Solaris:
        $(CC) -o curlthreads curlthreads.o $(CURL_DIR)/lib/libcurl.a -lpthread
-lm -lsocket -lnsl
*/

#include <pthread.h> 
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#include "curl.h"
}
#else
#include "curl.h"
#endif

size_t storedata (const void *ptr, size_t size, size_t nitems, FILE *stream) {
  (void)ptr; (void)stream;            /* just to stop g++ -Wall complaining */
  fprintf(stdout, "Thread #%i reads %i Bytes.\n", 
                   (int)pthread_self(), (int)(nitems*size));
  return (nitems);
}

void *urlfetcher(void *url) {
  curl_urlget (URGTAG_URL,            url, 
               URGTAG_FLAGS,          CONF_NOPROGRESS | CONF_FAILONERROR,
               URGTAG_WRITEFUNCTION,  (size_t (*)(void *, int, int, FILE
*))storedata,
               URGTAG_DONE);
  return NULL; 
}

int main(void) {
  char *url1="www.sun.com";  
  char *url2="www.microsoft.com";

  pthread_t thread_id1, thread_id2;
  pthread_create(&thread_id1, NULL, urlfetcher, (void *)url1);
  pthread_create(&thread_id2, NULL, urlfetcher, (void *)url2);
  pthread_join(thread_id1, NULL);
  pthread_join(thread_id2, NULL);
 
  fprintf(stdout, "Ready.\n");

  return 0;
}