有这样的事?_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 有这样的事?

有这样的事?

 2014/5/18 13:23:11  aitanjupt  博客园  我要评论(0)
  • 摘要:#include<event2/event.h>#include<event2/buffer.h>#include<event2/http.h>#include<Winsock2.h>#include<stdlib.h>#include<stdio.h>intinit_win_socket(){WSADATAwsaData;if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
  • 标签:

#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;
}

  • 相关文章
发表评论
用户名: 匿名