new
[libcurl.git] / README.libcurl
blobccec7615005f9d60d17a442def65e075b84e8e43
1                          _ _ _                     _ 
2                         | (_) |__   ___ _   _ _ __| |
3                         | | | '_ \ / __| | | | '__| |
4                         | | | |_) | (__| |_| | |  | |
5                         |_|_|_.__/ \___|\__,_|_|  |_|
8                      How To Use Libcurl In Your Program:
9                                  (by Ralph Beckmann <rabe@uni-paderborn.de>)
11 NOTE: If you plan to use libcurl.a in Threads under Linux, do not use the old
12 gcc-2.7.x because the function 'gethostbyname' seems not to be thread-safe,
13 that is to say an unavoidable SEGMENTATION FAULT might occur.
16 1. a) In a C-Program:
17       #include "curl.h"
18    
19    b) In a C++-Program:
20       extern "C" {
21       #include "curl.h"
22       }
24 2. char *url="http://www.domain.com";
25    curl_urlget (URGTAG_URL, url, 
26           URGTAG_FLAGS, CONF_NOPROGRESS,
27           URGTAG_ERRORBUFFER, errorBuffer,
28           URGTAG_WRITEFUNCTION, (size_t (*)(void *, int, int, FILE
29 *))handle_data,
30           URGTAG_TIMEOUT, 30,         /* or anything You want             */
31    ...
32           URGTAG_DONE);
34 3. size_t handle_data (const void *ptr, size_t size, size_t nitems,
35                        FILE *stream)
36    {
37       (void)stream;                   /* stop complaining using g++ -Wall */
38       if ((int)nitems <= 0) {
39          return (size_t)0;
40       }
41       fprintf(stdout, (char *)ptr);   /* or do anything else with it      */
42       return nitems;
43    }
45 4. Compile Your Program with  -I$(CURL_DIR)/include
47 5. Link Your Program together with  $(CURL_DIR)/lib/libcurl.a
49                      Small Example of How To Use libcurl
51 ----------------------------------------------------------------------
52 /* Full example that uses libcurl.a to fetch web pages.                    */
53 /* curlthreads.c                                                           */
54 /* - Test-Program by Ralph Beckmann for using curl in POSIX-Threads        */
55 /* Change *url1 and *url2 to textual long and slow non-FRAMESET websites!  */
56 /* 
57    1. Compile with gcc or g++ as $(CC): 
58        $(CC) -c -Wall -pedantic curlthreads.c -I$(CURL_DIR)/include
60    2. Link with:
61       - Linux:
62         $(CC) -o curlthreads curlthreads.o $(CURL_DIR)/lib/libcurl.a -lpthread
63 -lm 
64       - Solaris:
65         $(CC) -o curlthreads curlthreads.o $(CURL_DIR)/lib/libcurl.a -lpthread
66 -lm -lsocket -lnsl
69 #include <pthread.h> 
70 #include <stdio.h>
71 #ifdef __cplusplus
72 extern "C" {
73 #include "curl.h"
75 #else
76 #include "curl.h"
77 #endif
79 size_t storedata (const void *ptr, size_t size, size_t nitems, FILE *stream) {
80   (void)ptr; (void)stream;            /* just to stop g++ -Wall complaining */
81   fprintf(stdout, "Thread #%i reads %i Bytes.\n", 
82                    (int)pthread_self(), (int)(nitems*size));
83   return (nitems);
86 void *urlfetcher(void *url) {
87   curl_urlget (URGTAG_URL,            url, 
88                URGTAG_FLAGS,          CONF_NOPROGRESS | CONF_FAILONERROR,
89                URGTAG_WRITEFUNCTION,  (size_t (*)(void *, int, int, FILE
90 *))storedata,
91                URGTAG_DONE);
92   return NULL; 
95 int main(void) {
96   char *url1="www.sun.com";  
97   char *url2="www.microsoft.com";
99   pthread_t thread_id1, thread_id2;
100   pthread_create(&thread_id1, NULL, urlfetcher, (void *)url1);
101   pthread_create(&thread_id2, NULL, urlfetcher, (void *)url2);
102   pthread_join(thread_id1, NULL);
103   pthread_join(thread_id2, NULL);
105   fprintf(stdout, "Ready.\n");
107   return 0;