libevent和libcurl的一些学习
来源:互联网 发布:php开发手册pdf 编辑:程序博客网 时间:2024/05/06 04:17
libcurl是一个用于传递数据的库;libevent则是提供服务器相应操作的库。
网上关于libcurl的介绍很多,主要介绍了如何用它下载网页等功能,另外网上也有许多相关php代码的实例。libevent的相关资源比较少。
一下是我实现的两个简单的小例子,一个利用libcurl发送数据,一个利用libevent去接收。
libcurl.c 发送数据
1 #include<curl/curl.h>
2 #include<stdio.h>
3
int
main(
void
){
4
char
*url=
"http://127.0.0.1:8080/test"
;
5
char
mypost[]=
"asdfasdfasdfsdf"
;
6
// mypost[4]='\0';
7
// CURLcode return_code;
8
// return_code=curl_global_init(CURL_GLOBAL_WIN32);
9 CURL *curl=curl_easy_init();
10
if
(curl==NULL){
11
perror
(
"get a easy heandle failed.\n"
);
12 curl_easy_cleanup(curl);
13 curl_global_cleanup();
14
return
-1;
15 }
16 curl_easy_setopt(curl,CURLOPT_HEADER,1);
17 curl_easy_setopt(curl,CURLOPT_URL,url);
18 curl_easy_setopt(curl,CURLOPT_POST,1);
19 curl_easy_setopt(curl,CURLOPT_POSTFIELDS,(
void
*)mypost);
20 curl_easy_perform(curl);
21 curl_easy_cleanup(curl);
22
return
0;
23 }
libevent.c 接收数据
1 #include<stdio.h>
2 #include<evhttp.h>
3
void
test_handle(
struct
evhttp_request *r,
void
*u){
4 u_char *buf;
5 buf=EVBUFFER_DATA(r->input_buffer);
6 evhttp_send_reply(r,200,
"read ok!"
,NULL);
7
printf
(
"the host is: %s \n"
,r->remote_host);
8
printf
(
"the data is:\n%s\n"
,buf);
9
return
;
10 }
11
int
main(
void
){
12
char
*api_uri=
"http://127.0.0.1"
;
13
struct
event_base *base;
14
struct
evhttp *http;
15 base =event_base_new();
16 http=evhttp_new(base);
17
if
(!http){
18
perror
(
"evhttp_new() failed"
);
19
return
-1;
20 }
21
if
(evhttp_bind_socket(http,
"0.0.0.0"
,8080)){
22
perror
(
"bind_sock() failed"
);
23
return
-1;
24 }
25
//evhttp_set_cb(http,"/",test_handle,NULL);
26 evhttp_set_gencb(http,test_handle,NULL);
27 event_base_dispatch(base);
28 evhttp_free(http);
29 event_base_free(base);
30
return
0;
31 }
关于这两个库的比较好的学习地址:
http://curl.haxx.se/
http://monkey.org/~provos/libevent/
http://newblog.csdn.net/jgood/article/details/4787670
- libevent和libcurl的一些学习
- libevent和libcurl的一些学习
- libevent和libcurl的一些学习
- libevent学习之一:libevent源码的特点和结构
- libevent的一些知识
- Libcurl的一些基本介绍
- 一些关于libevent的使用
- libevent学习的资料
- libevent和基于libevent的网络编程
- libcurl库的使用的一些心得
- libevent学习之三:简单的服务器和客户端
- libevent源码学习----io多路复用的封装和使用
- 关于libcurl库使用的一些问题
- 【Networking】libevent的学习笔记
- libevent的学习与实践
- libcurl学习
- libcurl 学习
- Libevent源码分析-----与event相关的一些函数和操作
- sharepoint 2010 用stsadm命令行备份和还原网站
- 进程间通信
- 解决OBJC_CLASS_$_MBProgressHUD无法引用的问题
- libevent源码深度剖析
- C#实现的图片复杂验证码
- libevent和libcurl的一些学习
- SQL SERVER 存储过程 语法
- Memcached源码拆分:Libevent_Thread
- Introduction to Face Detection and Face Recognition:
- HTML、XHTML、CSS基础教程学习笔记(Day 3)
- Apple 微软 Google 都在竞购一家创业公司
- opennebula context iso 的生成过程及其主要功能
- asp.net中利用Jquery+Ajax+Json实现无刷新分页
- 组件笔记(二)