首先把libevhtp放入工程中,其中包括evhtp.c, evhtp.h, evhtp-config.h, evthr.c, evthr.h, htparse.c, htparse.h。
建立test.cpp,编写测试代码,直接用了别人的:http://blog.csdn.net/peta/article/details/16991605
#include<iostream>
using namespace std;
#include "libevhtp/evhtp.h"
void testcb(evhtp_request_t *req, void *a)
{
evbuffer_add_reference(req->buffer_out, "foobar", 6, NULL, NULL);
evhtp_send_reply(req, EVHTP_RES_OK);
}
int main(int argc, const char *argcv[])
{
evbase_t *evbase = event_base_new();
evhtp_t *htp = evhtp_new(evbase, NULL);
evhtp_set_cb(htp, "/", testcb, NULL);
evhtp_use_threads(htp, NULL, 4, NULL);
evhtp_bind_socket(htp, "0.0.0.0", 8080, 1024);
event_base_loop(evbase, 0);
return 0;
}
首先编译libevhtp库:
gcc -c evhtp.c evthr.c htparse.c -levent -lpthread
然后编译test.cpp:
g++ -o test test.cpp ./libevhtp/*.o -levent -pthread