码迷,mamicode.com
首页 > 其他好文 > 详细

有这样的事?

时间:2014-05-18 18:57:26      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:c   tar   http   int   a   get   

#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/http.h>
#include <Winsock2.h>
#include <stdlib.h>
#include <stdio.h>

int init_win_socket()
{
    WSADATA wsaData;
    if(WSAStartup(MAKEWORD(2,2) , &wsaData) != 0)
    {
        return -1;
    }
    return 0;
}

void generic_handler(struct evhttp_request *req, void *arg)
{
    struct evbuffer *buf = evbuffer_new();
    if(!buf)
    {
        puts("failed to create response buffer \n");
        return;
    }

    evbuffer_add_printf(buf, "Server Responsed. Requested: %s\n", evhttp_request_get_uri(req));
    evhttp_send_reply(req, HTTP_OK, "OK", buf);
    evbuffer_free(buf);
}

int main(int argc, char* argv[])
{
#ifdef WIN32
    init_win_socket();
#endif

    short          http_port = 8081;
    char          *http_addr = "127.0.0.1";

    struct event_base * base = event_base_new();

    struct evhttp * http_server = evhttp_new(base);
    if(!http_server)
    {
        return -1;
    }

    int ret = evhttp_bind_socket(http_server,http_addr,http_port);
    if(ret!=0)
    {
        return -1;
    }

    evhttp_set_gencb(http_server, generic_handler, NULL);

    printf("http server start OK! \n");

    event_base_dispatch(base);

    evhttp_free(http_server);

    WSACleanup();
    return 0;
}

有这样的事?,布布扣,bubuko.com

有这样的事?

标签:c   tar   http   int   a   get   

原文地址:http://www.cnblogs.com/wgp13x/p/3733867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!