Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1461438
  • 博文数量: 181
  • 博客积分: 3308
  • 博客等级: 中校
  • 技术积分: 2227
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-03 12:03
个人简介

我是zoro

文章分类

全部博文(181)

文章存档

2015年(1)

2013年(35)

2012年(39)

2011年(50)

2010年(56)

分类: LINUX

2013-08-22 13:57:36

libevhtp,并没有拼写错误,是一个在libevent基础开发的高性能、高易用的http server开源库。
也属偶然, 我发现它也是正好我将evhttp打成evhtp。

经过了解发现关于evhtp的资料还是挺少的,下面链接是找到的两篇关于libevhtp安装及使用的文章。
http://blog.t41.cn/index.php/archives/574
感谢前人的分享。


我下本文主要是记录我对libevhtp使用,希望能帮到像我这样对libevhtp有兴趣的朋友。

操作系统:CentOS-6.2-x86_64-minimal

1. 自动安装依赖库

点击(此处)折叠或打开

  1. #工具
  2. yum -y install gcc gcc-c++ autoconf automake make
  3. #依赖库
  4. yum -y install openssl-devel
2. 创建测试目录


点击(此处)折叠或打开

  1. sudo mkdir -p /opt/local

3. 编译安装cmake(libevhtp要求版本大于2.8,自动安装版本不符合要求)
版本:cmake-2.8.11.2.tar.gz
下载地址:
默认安装即可。

点击(此处)折叠或打开

  1. tar xf cmake-2.8.11.2.tar.gz
  2. cd cmake-2.8.11.2
  3. ./bootstrap
  4. make
  5. make install

4. 编译libevent
个人原因只使用静态库。

点击(此处)折叠或打开

  1. tar xf libevent-2.0.21-stable.tar.gz
  2. cd libevent-2.0.21-stable
  3. ./configure --disable-shared --prefix=/opt/local
  4. make
  5. make install


5. 编译libevhtp
版本:1.2.6
下载地址:

点击(此处)折叠或打开

  1. tar xf libevhtp-1.2.6.tar.gz
  2. cd libevhtp-1.2.6
  3. cmake -DCMAKE_PREFIX_PATH=/opt/local -DCMAKE_INSTALL_PREFIX=/opt/local .
  4. make
  5. make install


4. 使用测试

点击(此处)折叠或打开

  1. // file name: htp_test.c
  2. //

  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdint.h>
  7. #include <errno.h>
  8.  
  9. #include <evhtp.h>
  10.  
  11. void
  12. testcb(evhtp_request_t * req, void * a);
  13.  
  14. void
  15. testcb(evhtp_request_t * req, void * a) {
  16.   evbuffer_add_reference(req->buffer_out, "foobar", 6, NULL, NULL);
  17.   evhtp_send_reply(req, EVHTP_RES_OK);
  18. }
  19.  
  20. int main (int argc, const char * argv[])
  21. {
  22.   evbase_t *evbase = event_base_new();
  23.   evhtp_t *htp = evhtp_new(evbase, NULL);
  24.  
  25.   evhtp_set_cb(htp, "/", testcb, NULL); /* 设置回调函数 */
  26.   evhtp_use_threads(htp, NULL, 4, NULL); /* 设置4个线程 */
  27.  
  28.   /* 监听本地所有IP的8080端口, backlog为1024 */
  29.   evhtp_bind_socket(htp, "0.0.0.0", 8080, 1024);
  30.  
  31.   /* 进入循环、监听连接,http server开始工作 */
  32.   event_base_loop(evbase, 0);
  33.  
  34.   return 0;
  35. }


编译:
gcc -o test -I/opt/local/include -L/opt/local/lib htp_test.c  /opt/local/lib/libevhtp.a -levent -levent_openssl -levent_pthreads -lssl -lcrypto -levhtp -ldl -lrt


阅读(7332) | 评论(0) | 转发(0) |
1

上一篇:用 OProfile 彻底了解性能

下一篇:安装Scrapy

给主人留下些什么吧!~~