libevent 写了一个简单地web服务器

来源:互联网 发布:腾讯围棋 mac 编辑:程序博客网 时间:2024/05/16 16:05
<span style="font-size:18px;">void DateDeal{    }void httpd_handler(struct evhttp_request *req, void *arg) {     const char *uri;     uri = evhttp_request_uri(req);     char *post_data = (char *) EVBUFFER_DATA(req->input_buffer);              string result=DataDeal(uri,post_data);  //url 和数据的处理函数             cout<<"result==="<<result.c_str()<<endl;     //HTTP header     evhttp_add_header(req->output_headers, "Server", MYHTTPD_SIGNATURE);     evhttp_add_header(req->output_headers, "Content-Type", "text/plain; charset=UTF-8");     evhttp_add_header(req->output_headers, "Connection", "close");      struct evbuffer *buf;     buf = evbuffer_new();     evbuffer_add_printf(buf, "%s\n",result.c_str());     evhttp_send_reply(req, HTTP_OK, "OK", buf);     evbuffer_free(buf);}int main(){     event_init();    //http server    struct evhttp *httpd;    httpd = evhttp_start(g_JsServerIp, g_JsServerPt);    printf("ip===%s\nport=%d\n",g_JsServerIp,g_JsServerPt);    printf("success\n");        //generic callback    int httpd_option_timeout = 120;    evhttp_set_timeout(httpd, httpd_option_timeout);    evhttp_set_gencb(httpd, httpd_handler, NULL);//处理post请求的http内容    //callback        event_dispatch();    evhttp_free(httpd);}</span>

0 0