Libevent基本应用实例--专为懒人定制
2014-12-05 10:43
766 查看
本段代码不保证可编译,甚至不保证正确性,仅仅给出使用的简单示例,懒人各取所需:
#define MAX_ARG_LENGTH 1024 /** * Http server callback */ void http_handle(struct evhttp_request *request,void *data){ if( EVHTTP_REQ_GET == request->type){ (char*)data = strdup(request->uri); } if( EVHTTP_REQ_POST == request->type){ size_t size = EVBUFFER_LENGTH(request->input_buffer); if(size > 0){ memcpy((char*)data, EVBUFFER_DATA(request->input_buffer),size); } } struct evkeyvalq *param; evhttp_parse_query(uri, param); const char *user = evhttp_find_header(param, "user"); const char *information = evhttp_find_header(param, "information"); // do something with the information like parse to JSON and so on. struct evbuffer *response = evbuffer_new(); evhttp_add_header(req->output_headers, "Content-Type", "text/plain"); evhttp_add_header(req->output_headers, "Connection", "keep-alive"); evhttp_add_header(req->output_headers, "Cache-Control", "no-cache"); evhttp_add_header(req->output_headers, "author", "zhousy"); evbuffer_add_printf(response,"information=%s",information); evhttp_send_reply(req, HTTP_OK, "OK", response); // evhttp_send_reply(req, HTTP_BADREQUEST, "BAD request", response); evhttp_clear_headers(param); evbuffer_free(response); } /** * Http client callback */ void client_handler(struct evhttp_request request, void *data){ //const char *content_length = evhttp_find_header(request->input_headers, "Content-Length"); size_t size = EVBUFFER_LENGTH(request->input_buffer); if(size > 0){ const char *content = EVBUFFER_DATA(request->input_buffer); } evhttp_connection_free((evhttp_connection*)data); } int main(int argc, char* argv[]){ struct event_base *base; struct evhttp *http; struct event *timer_event; struct event *IO_event; struct event *SIG_event; char *ip = argv[1]; int port = atoi(argv[2]); char *data[MAX_ARG_LENGTH]; base = event_base_new(); http = evhttp_new(base); if( -1 == evhttp_bind_socket(http, ip , port)){ exit(1); } else{ evhttp_set_gencb(http, http_handle, (void*)data); } evhttp_set_timeout(http,120); /** * This is the sample show how to add timer event using Libevent */ struct timeval timeout; timeout.tv_sec = 1; timeout.tv_usec = 10; evtimer_set(timer_event, timer_event_handler, (void*)data); event_base_set(base, timer_event); evtimer_add(timer_event, &timeout); /** * This is the sample show how to add IO event using Libevent */ //int file = open(argv[3],O_RDWR | O_CREATE | O_TRUNC, S_IRWXU|S_IWGRP|S_IROTH); //fcntl(file,F_SETFL,O_NONBLOCK); int socket = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_addr.s_addr = hton(inet_aton("127.0.0.1")); addr.sin_port = htons(80); if(connect( socket,(sockaddr*) &addr, sizeof(addr)) < 0){ exit(1); } event_set(IO_event, socket, EV_READ | EV_PERSIST, IO_event_handler, (void*)data); event_base_set(base,IO_event); event_add(IO_event,NULL); /** * This is the sample show how to add signal event using Libevent */ //SIG_event = evsignal_new(base, SIGUSR1, SIG_event_handler,NULL); //evsignal_add(SIG_event,NULL); //signal_set(SIG_event, SIGCHLD, SIG_event_handler, (void*)data); evsignal_set(SIG_event, SIGCHLD, SIG_event_handler, (void*)data); event_base_set(base,SIG_event); while(true){ struct timeval run_interval_time; run_interval_time.tv_sec = 0; run_interval_time.tv_usec = 1000; event_base_loopexit(base,&run_interval_time); event_base_dispatch(base); // do something else when exit from dispatch; } /** * This is the sample show how to build client request using Libevent * */ struct evhttp_connection *client; struct evhttp_request *request; client = evhttp_connection_new("127.0.0.1",80); if(client){ evhttp_connection_set_base(base,client); //set connection timeout evhttp_connection_set_timeout(client, 5); //set response handler request = evhttp_request_new(client_handler, (void*)client); //set request headers evhttp_add_header(request->output_headers, "Host", "127.0.0.1"); evhttp_add_header(request->output_headers, "Content-Length", "0"); //set request type and send request evhttp_make_request(client, request,EVHTTP_REQ_GET, request_str->str); } evtimer_del(timer_event); signal_del(SIG_event); evhttp_free(http); event_base_free(base); }
相关文章推荐
- XMLHttpRequest 的基本实例Ajax的应用
- VC++ Datagrid应用实例详解系列(1) - 基本功能
- 顺序栈,C++基本实例应用
- mysql基本应用(实例)
- android 蓝牙锁应用开发实例(二)客户端基本页面
- 从基本应用实例快速步入XML编程世界
- Spring---AOP基本概念以及Advice5种类型的通知注解应用实例
- 基于事件的开源网络库—libevent:应用实例
- 关于接口的基本应用实例讲解
- JavaEE Spring框架学习笔记(AOP基本应用实例)
- ARM—Linux基本数据类型综合应用实例
- 基于事件的开源网络库—libevent:应用实例
- Java Web 应用实例 servlet实现oracle 基本增删改查
- xml基本操作和保存配置文件应用实例
- xml基本操作和保存配置文件应用实例
- 一些vc的基本知识,创建应用实例的步骤
- 一些vc的基本知识,创建应用实例的步骤
- xml基本操作和保存配置文件应用实例
- 实例解析iOS中音乐播放器应用开发的基本要点
- JavaWeb应用实例:用servlet实现oracle 基本增删改查