LibCurl编程

2.1 LibCurl编程流程

在基于LibCurl的程序里,主要采用callback function (回调函数)的形式完成传输任务,用户在启动传输前设置好各类参数和回调函数,当满足条件时libcurl将调用用户的回调函数实现特定功能。下面是利用libcurl完成传输任务的流程:

1.       调用curl_global_init()初始化libcurl

2.       调用 curl_easy_init()函数得到 easy interface型指针

3.       调用curl_easy_setopt设置传输选项

4.       根据curl_easy_setopt设置的传输选项,实现回调函数以完成用户特定任务

5.       调用curl_easy_perform()函数完成传输任务

6.       调用curl_easy_cleanup()释放内存

在整过过程中设置curl_easy_setopt()参数是最关键的,几乎所有的libcurl程序都要使用它。

2.2 重要函数

1.CURLcode curl_global_init(long flags);

描述:
这个函数只能用一次。(其实在调用curl_global_cleanup 函数后仍然可再用)
如果这个函数在curl_easy_init函数调用时还没调用,它讲由libcurl库自动完成。
参数:flags
CURL_GLOBAL_ALL                      //初始化所有的可能的调用。
CURL_GLOBAL_SSL                      //初始化支持 安全套接字层。
CURL_GLOBAL_WIN32            //初始化win32套接字库。
CURL_GLOBAL_NOTHING         //没有额外的初始化。

2 void curl_global_cleanup(void);
描述:在结束libcurl使用的时候,用来对curl_global_init做的工作清理。类似于close的函数。
3 char *curl_version( );
描述: 打印当前libcurl库的版本。
4 CURL *curl_easy_init( );
描述:
curl_easy_init用来初始化一个CURL的指针(有些像返回FILE类型的指针一样). 相应的在调用结束时要用curl_easy_cleanup函数清理.
一般curl_easy_init意味着一个会话的开始. 它的返回值一般都用在easy系列的函数中.
5 void curl_easy_cleanup(CURL *handle);
描述:
这个调用用来结束一个会话.与curl_easy_init配合着用.
参数:
CURL类型的指针.
6 CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter);
描述: 这个函数最重要了.几乎所有的curl 程序都要频繁的使用它.它告诉curl库.程序将有如何的行为. 比如要查看一个网页的html代码等.(这个函数有些像ioctl函数)参数:
1 CURL类型的指针
2 各种CURLoption类型的选项.(都在curl.h库里有定义,man 也可以查看到)
3 parameter 这个参数 既可以是个函数的指针,也可以是某个对象的指针,也可以是个long型的变量.它用什么这取决于第二个参数.
CURLoption 这个参数的取值很多.具体的可以查看man手册.
7 CURLcode curl_easy_perform(CURL *handle);描述:这个函数在初始化CURL类型的指针 以及curl_easy_setopt完成后调用. 就像字面的意思所说perform就像是个舞台.让我们设置的
option 运作起来.参数:
CURL类型的指针.

3.3 curl_easy_setopt函数介绍

本节主要介绍curl_easy_setopt中跟http相关的参数。注意本节的阐述都是以libcurl作为主体,其它为客体来阐述的。

1.     CURLOPT_URL
设置访问URL

2.       CURLOPT_WRITEFUNCTION,CURLOPT_WRITEDATA
回调函数原型为:size_t function( void *ptr, size_t size, size_t nmemb, void *stream); 函数将在libcurl接收到数据后被调用,因此函数多做数据保存的功能,如处理下载文件。CURLOPT_WRITEDATA 用于表明CURLOPT_WRITEFUNCTION函数中的stream指针的来源。

3.      CURLOPT_HEADERFUNCTION,CURLOPT_HEADERDATA
回调函数原型为 size_t function( void *ptr, size_t size,size_t nmemb, void *stream); libcurl一旦接收到http 头部数据后将调用该函数。CURLOPT_WRITEDATA 传递指针给libcurl,该指针表明CURLOPT_HEADERFUNCTION 函数的stream指针的来源。

4.       CURLOPT_READFUNCTION CURLOPT_READDATA
libCurl需要读取数据传递给远程主机时将调用CURLOPT_READFUNCTION指定的函数,函数原型是:size_t function(void *ptr, size_t size, size_t nmemb,void *stream). CURLOPT_READDATA 表明CURLOPT_READFUNCTION函数原型中的stream指针来源。

5.       CURLOPT_NOPROGRESS,CURLOPT_PROGRESSFUNCTION,CURLOPT_PROGRESSDATA
跟数据传输进度相关的参数。CURLOPT_PROGRESSFUNCTION 指定的函数正常情况下每秒被libcurl调用一次,为了使CURLOPT_PROGRESSFUNCTION被调用,CURLOPT_NOPROGRESS必须被设置为false,CURLOPT_PROGRESSDATA指定的参数将作为CURLOPT_PROGRESSFUNCTION指定函数的第一个参数

6.       CURLOPT_TIMEOUT,CURLOPT_CONNECTIONTIMEOUT:
CURLOPT_TIMEOUT 由于设置传输时间,CURLOPT_CONNECTIONTIMEOUT 设置连接等待时间

7.       CURLOPT_FOLLOWLOCATION
设置重定位URL

CURLOPT_RANGE: CURLOPT_RESUME_FROM:
断点续传相关设置。CURLOPT_RANGE 指定char *参数传递给libcurl,用于指明http域的RANGE头域,例如:
表示头500个字节:bytes=0-499
表示第二个500字节:bytes=500-999
表示最后500个字节:bytes=-500
表示500字节以后的范围:bytes=500-
第一个和最后一个字节:bytes=0-0,-1

同时指定几个范围:bytes=500-600,601-999
    CURLOPT_RESUME_FROM 传递一个long参数给libcurl,指定你希望开始传递的

偏移量。

3.4 curl_easy_perform 函数说明(error 状态码)

该函数完成curl_easy_setopt指定的所有选项,本节重点介绍curl_easy_perform的返回值。返回0意味一切ok,非0代表错误发生。主要错误码说明:
1.    CURLE_OK
    任务完成一切都好

2     CURLE_UNSUPPORTED_PROTOCOL

    不支持的协议,由URL的头部指定

3     CURLE_COULDNT_CONNECT

    不能连接到remote 主机或者代理

4     CURLE_REMOTE_ACCESS_DENIED

    访问被拒绝

5     CURLE_HTTP_RETURNED_ERROR

    Http返回错误

6           CURLE_READ_ERROR
读本地文件错误

3.1 获取html网页

view plaincopy to clipboardprint?
01.#include <stdio.h>;  
02.#include <stdlib.h>;  
03.#include <unistd.h>;  
04. 
05.#include <curl/curl.h>;  
06.#include <curl/types.h>;  
07.#include <curl/easy.h>;  
08. 
09.FILE *fp;  //定义FILE类型指针  
10. 
11.size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)  //这个函数是为了符合CURLOPT_WRITEFUNCTION, 而构造的  
12.{  
13.    int written = fwrite(ptr, size, nmemb, (FILE *)fp);  
14.    return written;  
15.}  
16. 
17.int main(int argc, char *argv[])  
18.{  
19.    CURL *curl;  
20. 
21.    curl_global_init(CURL_GLOBAL_ALL);    
22.    curl=curl_easy_init();  
23.    curl_easy_setopt(curl, CURLOPT_URL, argv[1]);    
24. 
25.    if((fp=fopen(argv[1],"w"))==NULL)  
26.    {  
27.        curl_easy_cleanup(curl);  
28.        exit(1);  
29.    }  
30.    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);  //CURLOPT_WRITEFUNCTION 将后继的动作交给write_data函数处理  
31.    curl_easy_perform(curl);  
32.    curl_easy_cleanup(curl);  
33.    exit(0);  
34.} 
#include <stdio.h>;
#include <stdlib.h>;
#include <unistd.h>;

#include <curl/curl.h>;
#include <curl/types.h>;
#include <curl/easy.h>;

FILE *fp;  //定义FILE类型指针

size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)  //这个函数是为了符合CURLOPT_WRITEFUNCTION, 而构造的
{
    int written = fwrite(ptr, size, nmemb, (FILE *)fp);
    return written;
}

int main(int argc, char *argv[])
{
    CURL *curl;

    curl_global_init(CURL_GLOBAL_ALL); 
    curl=curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, argv[1]); 

    if((fp=fopen(argv[1],"w"))==NULL)
    {
        curl_easy_cleanup(curl);
        exit(1);
    }
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);  //CURLOPT_WRITEFUNCTION 将后继的动作交给write_data函数处理
    curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    exit(0);

把获得的html代码存入相应的文件中.看下面一个例子

view plaincopy to clipboardprint?
01.#include <stdio.h>;  
02.#include <stdlib.h>;  
03.#include <unistd.h>;  
04. 
05.#include <curl/curl.h>;  
06.#include <curl/types.h>;  
07.#include <curl/easy.h>;  
08. 
09.FILE *fp;  //定义FILE类型指针  
10. 
11.size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)  //这个函数是为了符合CURLOPT_WRITEFUNCTION, 而构造的  
12.{  
13.    int written = fwrite(ptr, size, nmemb, (FILE *)fp);  
14.    return written;  
15.}  
16. 
17.int main(int argc, char *argv[])  
18.{  
19.    CURL *curl;  
20. 
21.    curl_global_init(CURL_GLOBAL_ALL);    
22.    curl=curl_easy_init();  
23.    curl_easy_setopt(curl, CURLOPT_URL, argv[1]);    
24. 
25.    if((fp=fopen(argv[1],"w"))==NULL)  
26.    {  
27.        curl_easy_cleanup(curl);  
28.        exit(1);  
29.    }  
30.    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);  //CURLOPT_WRITEFUNCTION 将后继的动作交给write_data函数处理  
31.    curl_easy_perform(curl);  
32.    curl_easy_cleanup(curl);  
33.    exit(0);  
34.} 
#include <stdio.h>;
#include <stdlib.h>;
#include <unistd.h>;

#include <curl/curl.h>;
#include <curl/types.h>;
#include <curl/easy.h>;

FILE *fp;  //定义FILE类型指针

size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)  //这个函数是为了符合CURLOPT_WRITEFUNCTION, 而构造的
{
    int written = fwrite(ptr, size, nmemb, (FILE *)fp);
    return written;
}

int main(int argc, char *argv[])
{
    CURL *curl;

    curl_global_init(CURL_GLOBAL_ALL); 
    curl=curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, argv[1]); 

    if((fp=fopen(argv[1],"w"))==NULL)
    {
        curl_easy_cleanup(curl);
        exit(1);
    }
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);  //CURLOPT_WRITEFUNCTION 将后继的动作交给write_data函数处理
    curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    exit(0);
}

从ftp站点下载文件的例子.

view plaincopy to clipboardprint?
01.#include <stdio.h>;  
02.#include <curl/curl.h>;  
03.#include <curl/types.h>;  
04.#include <curl/easy.h>;  
05. 
06.struct FtpFile   //定义一个结构为了传递给my_fwrite函数.可用curl_easy_setopt的CURLOPT_WRITEDATA选项传递  
07.{  
08.        char *filename;  
09.        FILE *stream;  
10.};  
11. 
12.int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)  
13.{  
14.        struct FtpFile *out=(struct FtpFile *)stream;  // stream指针其实就是 指向struct FtpFile ftpfile的  
15.        if(out && !out->stream)  
16.        {  
17.                out->;stream=fopen(out->filename, "wb"); //没有这个流的话就创建一个 名字是out->filename.  
18.                if(!out->stream)  
19.                return -1;  
20.        }  
21.        return fwrite(buffer, size, nmemb, out->stream);  
22.}  
23. 
24.int main(int argc, char *argv[])  
25.{  
26.        CURL *curl;  
27.        CURLcode res;  
28.        struct FtpFile ftpfile={argv[2],NULL};  //初始化一个FtpFile结构   
29.        curl_global_init(CURL_GLOBAL_DEFAULT);  
30. 
31.        curl = curl_easy_init();  
32.        if(curl)  
33.        {  
34.                curl_easy_setopt(curl, CURLOPT_URL,argv[1]);  
35.                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);  
36.                curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);  //给相关函数的第四个参数 传递一个结构体的指针  
37.                curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);  //CURLOPT_VERBOSE 这个选项很常用 用来在屏幕上显示对服务器相关操作返回的信息  
38. 
39.                res = curl_easy_perform(curl);  
40.                curl_easy_cleanup(curl);  
41. 
42.                if(CURLE_OK != res)  
43.                        fprintf(stderr, "curl told us %d/n", res);  
44.        }  
45.        if(ftpfile.stream)  
46.        fclose(ftpfile.stream);  
47.        curl_global_cleanup();  
48. 
49.        return 0;  
50.} 
#include <stdio.h>;
#include <curl/curl.h>;
#include <curl/types.h>;
#include <curl/easy.h>;

struct FtpFile   //定义一个结构为了传递给my_fwrite函数.可用curl_easy_setopt的CURLOPT_WRITEDATA选项传递
{
        char *filename;
        FILE *stream;
};

int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
        struct FtpFile *out=(struct FtpFile *)stream;  // stream指针其实就是 指向struct FtpFile ftpfile的
        if(out && !out->stream)
        {
                out->;stream=fopen(out->filename, "wb"); //没有这个流的话就创建一个 名字是out->filename.
                if(!out->stream)
                return -1;
        }
        return fwrite(buffer, size, nmemb, out->stream);
}

int main(int argc, char *argv[])
{
        CURL *curl;
        CURLcode res;
        struct FtpFile ftpfile={argv[2],NULL};  //初始化一个FtpFile结构
        curl_global_init(CURL_GLOBAL_DEFAULT);

        curl = curl_easy_init();
        if(curl)
        {
                curl_easy_setopt(curl, CURLOPT_URL,argv[1]);
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
                curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);  //给相关函数的第四个参数 传递一个结构体的指针
                curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);  //CURLOPT_VERBOSE 这个选项很常用 用来在屏幕上显示对服务器相关操作返回的信息

                res = curl_easy_perform(curl);
                curl_easy_cleanup(curl);

                if(CURLE_OK != res)
                        fprintf(stderr, "curl told us %d/n", res);
        }
        if(ftpfile.stream)
        fclose(ftpfile.stream);
        curl_global_cleanup();

        return 0;
}

 进度条实例??显示文件下载进度

view plaincopy to clipboardprint?
01.// 采用CURLOPT_NOPROGRESS, CURLOPT_PROGRESSFUNCTION    CURLOPT_PROGRESSDATA 实现文件传输进度提示功能  
02. 
03.//函数采用了gtk库,故编译时需指定gtk库  
04. 
05.//函数启动专门的线程用于显示gtk 进度条bar 
06.
07.#include <stdio.h> 
08.
09.#include <gtk/gtk.h> 
10.
11.#include <curl/curl.h> 
12.
13.#include <curl/types.h> /* new for v7 */ 
14.
15.#include <curl/easy.h> /* new for v7 */  
16. 
17. 
18.GtkWidget *Bar;  
19. 
20.这个函数是为了符合CURLOPT_WRITEFUNCTION而构造的  
21. 
22.//完成数据保存功能  
23. 
24.size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)  
25. 
26.{  
27. 
28.  return fwrite(ptr, size, nmemb, stream);  
29. 
30.}  
31. 
32.//这个函数是为了符合CURLOPT_READFUNCTION而构造的  
33. 
34.//数据上传时使用  
35. 
36.size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)  
37. 
38.{  
39. 
40.  return fread(ptr, size, nmemb, stream);  
41. 
42.}  
43. 
44.//这个函数是为了符合CURLOPT_PROGRESSFUNCTION而构造的  
45. 
46.//显示文件传输进度,t代表文件大小,d代表传输已经完成部分  
47. 
48.int my_progress_func(GtkWidget *bar,  
49. 
50.                     double t, /* dltotal */ 
51. 
52.                     double d, /* dlnow */ 
53. 
54.                     double ultotal,  
55. 
56.                     double ulnow)  
57. 
58.{  
59. 
60./*  printf("%d / %d (%g %%)/n", d, t, d*100.0/t);*/ 
61. 
62.  gdk_threads_enter();  
63. 
64.  gtk_progress_set_value(GTK_PROGRESS(bar), d*100.0/t);  
65. 
66.  gdk_threads_leave();  
67. 
68.  return 0;  
69. 
70.}  
71. 
72. 
73.void *my_thread(void *ptr)  
74. 
75.{  
76. 
77.  CURL *curl;  
78. 
79.  CURLcode res;  
80. 
81.  FILE *outfile;  
82. 
83.  gchar *url = ptr;  
84. 
85. 
86.  curl = curl_easy_init();  
87. 
88.  if(curl)  
89. 
90.  {  
91. 
92.    outfile = fopen("test.curl", "w");  
93. 
94. 
95.    curl_easy_setopt(curl, CURLOPT_URL, url);  
96. 
97.    curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);  
98. 
99.    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);  
100. 
101.    curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);  
102. 
103.    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);  
104. 
105.    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);  
106. 
107.    curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);  
108. 
109. 
110.    res = curl_easy_perform(curl);  
111. 
112. 
113.    fclose(outfile);  
114. 
115.    /* always cleanup */ 
116. 
117.    curl_easy_cleanup(curl);  
118. 
119.  }  
120. 
121. 
122.  return NULL;  
123. 
124.}  
125. 
126. 
127.int main(int argc, char **argv)  
128. 
129.{  
130. 
131.  GtkWidget *Window, *Frame, *Frame2;  
132. 
133.  GtkAdjustment *adj;  
134. 
135. 
136.  /* Must initialize libcurl before any threads are started */ 
137. 
138.  curl_global_init(CURL_GLOBAL_ALL);  
139. 
140. 
141.  /* Init thread */ 
142. 
143.  g_thread_init(NULL);  
144. 
145. 
146.  gtk_init(&argc, &argv);  
147. 
148.  Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);  
149. 
150.  Frame = gtk_frame_new(NULL);  
151. 
152.  gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT);  
153. 
154.  gtk_container_add(GTK_CONTAINER(Window), Frame);  
155. 
156.  Frame2 = gtk_frame_new(NULL);  
157. 
158.  gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN);  
159. 
160.  gtk_container_add(GTK_CONTAINER(Frame), Frame2);  
161. 
162.  gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5);  
163. 
164.  adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);  
165. 
166.  Bar = gtk_progress_bar_new_with_adjustment(adj);  
167. 
168.  gtk_container_add(GTK_CONTAINER(Frame2), Bar);  
169. 
170.  gtk_widget_show_all(Window);  
171. 
172. 
173.  if (!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0)  
174. 
175.    g_warning("can't create the thread");  
176. 
177. 
178.  gdk_threads_enter();  
179. 
180.  gtk_main();  
181. 
182.  gdk_threads_leave();  
183. 
184.  return 0;  
185. 
186.}  
187.   
188. 
189.编译export PKG_CONFIG_PATH=/usr/lib/pkgconfig/  
190.gcc progress.c –o progress ` pkg-config --libs –cflags gtk+-2..0` -lcurl –lgthread-2.0  
191. 
192../ progress  http://software.sky-union.cn/index.asp   
193. 
194.   
195. 
196.  
// 采用CURLOPT_NOPROGRESS, CURLOPT_PROGRESSFUNCTION    CURLOPT_PROGRESSDATA 实现文件传输进度提示功能

//函数采用了gtk库,故编译时需指定gtk库

//函数启动专门的线程用于显示gtk 进度条bar

#include <stdio.h>

#include <gtk/gtk.h>

#include <curl/curl.h>

#include <curl/types.h> /* new for v7 */

#include <curl/easy.h> /* new for v7 */


GtkWidget *Bar;

这个函数是为了符合CURLOPT_WRITEFUNCTION而构造的

//完成数据保存功能

size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)

{

  return fwrite(ptr, size, nmemb, stream);

}

//这个函数是为了符合CURLOPT_READFUNCTION而构造的

//数据上传时使用

size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)

{

  return fread(ptr, size, nmemb, stream);

}

//这个函数是为了符合CURLOPT_PROGRESSFUNCTION而构造的

//显示文件传输进度,t代表文件大小,d代表传输已经完成部分

int my_progress_func(GtkWidget *bar,

                     double t, /* dltotal */

                     double d, /* dlnow */

                     double ultotal,

                     double ulnow)

{

/*  printf("%d / %d (%g %%)/n", d, t, d*100.0/t);*/

  gdk_threads_enter();

  gtk_progress_set_value(GTK_PROGRESS(bar), d*100.0/t);

  gdk_threads_leave();

  return 0;

}


void *my_thread(void *ptr)

{

  CURL *curl;

  CURLcode res;

  FILE *outfile;

  gchar *url = ptr;


  curl = curl_easy_init();

  if(curl)

  {

    outfile = fopen("test.curl", "w");


    curl_easy_setopt(curl, CURLOPT_URL, url);

    curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);

    curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);

    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);

    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);

    curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);


    res = curl_easy_perform(curl);


    fclose(outfile);

    /* always cleanup */

    curl_easy_cleanup(curl);

  }


  return NULL;

}


int main(int argc, char **argv)

{

  GtkWidget *Window, *Frame, *Frame2;

  GtkAdjustment *adj;


  /* Must initialize libcurl before any threads are started */

  curl_global_init(CURL_GLOBAL_ALL);


  /* Init thread */

  g_thread_init(NULL);


  gtk_init(&argc, &argv);

  Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  Frame = gtk_frame_new(NULL);

  gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT);

  gtk_container_add(GTK_CONTAINER(Window), Frame);

  Frame2 = gtk_frame_new(NULL);

  gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN);

  gtk_container_add(GTK_CONTAINER(Frame), Frame2);

  gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5);

  adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);

  Bar = gtk_progress_bar_new_with_adjustment(adj);

  gtk_container_add(GTK_CONTAINER(Frame2), Bar);

  gtk_widget_show_all(Window);


  if (!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0)

    g_warning("can't create the thread");


  gdk_threads_enter();

  gtk_main();

  gdk_threads_leave();

  return 0;

}
 

编译export PKG_CONFIG_PATH=/usr/lib/pkgconfig/
gcc progress.c –o progress ` pkg-config --libs –cflags gtk+-2..0` -lcurl –lgthread-2.0

./ progress  http://software.sky-union.cn/index.asp

 

 
 

断点续传实例

view plaincopy to clipboardprint?
01.断点续传实例  
02. 
03.//采用CURLOPT_RESUME_FROM_LARGE 实现文件断点续传功能  
04. 
05.#include <stdlib.h>  
06. 
07.#include <stdio.h>  
08. 
09.#include <sys/stat.h>  
10. 
11. 
12.#include <curl/curl.h>  
13. 
14.//这个函数为CURLOPT_HEADERFUNCTION参数构造  
15. 
16./* 从http头部获取文件size*/ 
17. 
18.size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void *stream) {  
19. 
20.       int r;  
21. 
22.       long len = 0;  
23. 
24. 
25.       /* _snscanf() is Win32 specific */ 
26. 
27.       // r = _snscanf(ptr, size * nmemb, "Content-Length: %ld/n", &len);  
28. 
29. r = sscanf(ptr, "Content-Length: %ld/n", &len);  
30. 
31.       if (r) /* Microsoft: we don't read the specs */ 
32. 
33.              *((long *) stream) = len;  
34. 
35. 
36.       return size * nmemb;  
37. 
38.}  
39. 
40. 
41./* 保存下载文件 */ 
42. 
43.size_t wirtefunc(void *ptr, size_t size, size_t nmemb, void *stream)  
44. 
45.{  
46. 
47.        return fwrite(ptr, size, nmemb, stream);  
48. 
49.}  
50. 
51. 
52./*读取上传文件 */ 
53. 
54.size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)  
55. 
56.{  
57. 
58.       FILE *f = stream;  
59. 
60.       size_t n;  
61. 
62. 
63.       if (ferror(f))  
64. 
65.              return CURL_READFUNC_ABORT;  
66. 
67. 
68.       n = fread(ptr, size, nmemb, f) * size;  
69. 
70. 
71.       return n;  
72. 
73.}  
74. 
75. 
76.// 下载 或者上传文件函数  
77. 
78.int download(CURL *curlhandle, const char * remotepath, const char * localpath,  
79. 
80.           long timeout, long tries)  
81. 
82.{  
83. 
84.       FILE *f;  
85. 
86.       curl_off_t local_file_len = -1 ;  
87. 
88.       long filesize =0 ;  
89. 
90.         
91. 
92.       CURLcode r = CURLE_GOT_NOTHING;  
93. 
94.       int c;  
95. 
96.  struct stat file_info;  
97. 
98.  int use_resume = 0;  
99. 
100.  /* 得到本地文件大小 */ 
101. 
102.  //if(access(localpath,F_OK) ==0)  
103. 
104.   
105. 
106.    if(stat(localpath, &file_info) == 0)   
107. 
108.     {  
109. 
110.        local_file_len =  file_info.st_size;  
111. 
112.        use_resume  = 1;  
113. 
114.      }  
115. 
116.  //采用追加方式打开文件,便于实现文件断点续传工作  
117. 
118.       f = fopen(localpath, "ab+");   
119. 
120.       if (f == NULL) {  
121. 
122.              perror(NULL);  
123. 
124.              return 0;  
125. 
126.       }  
127. 
128. 
129.       //curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);  
130. 
131. 
132.       curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);  
133. 
134. 
135.              curl_easy_setopt(curlhandle, CURLOPT_CONNECTTIMEOUT, timeout);  // 设置连接超时,单位秒  
136. 
137.       //设置http 头部处理函数  
138. 
139.       curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);  
140. 
141.       curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &filesize);  
142. 
143. // 设置文件续传的位置给libcurl  
144. 
145.       curl_easy_setopt(curlhandle, CURLOPT_RESUME_FROM_LARGE, use_resume?local_file_len:0);  
146. 
147. 
148.       curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, f);  
149. 
150.       curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, wirtefunc);  
151. 
152. 
153.       //curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);  
154. 
155.       //curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);  
156. 
157.       curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 1L);  
158. 
159.       curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);  
160. 
161.   
162. 
163.   
164. 
165.  r = curl_easy_perform(curlhandle);  
166. 
167.         
168. 
169. 
170.       fclose(f);  
171. 
172. 
173.       if (r == CURLE_OK)  
174. 
175.              return 1;  
176. 
177.       else {  
178. 
179.              fprintf(stderr, "%s/n", curl_easy_strerror(r));  
180. 
181.              return 0;  
182. 
183.       }  
184. 
185.}  
186. 
187. 
188.int main(int c, char **argv) {  
189. 
190.       CURL *curlhandle = NULL;  
191. 
192. 
193.       curl_global_init(CURL_GLOBAL_ALL);  
194. 
195.       curlhandle = curl_easy_init();  
196. 
197. 
198.       //download(curlhandle, "ftp://user:pass@host/path/file", "C://file", 0, 3);  
199. 
200.  download(curlhandle , "http://software.sky-union.cn/index.asp","/work/index.asp",1,3);  
201. 
202.       curl_easy_cleanup(curlhandle);  
203. 
204.       curl_global_cleanup();  
205. 
206. 
207.       return 0;  
208. 
209.}  
210.   
211. 
212.编译gcc  resume.c  -o resume –lcurl  
213. 
214../ resume 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ljob2006/archive/2009/07/29/4390612.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值