libevent是一个事件触发的网络库,适用于windows、linux、bsd等多种平台,内部使用select、epoll、kqueue等系统调用管理事件机制。著名分布式缓存软件memcached也是libevent based,而且libevent在使用上可以做到跨平台,而且根据libevent官方网站上公布的数据统计,似乎也有着非凡的性能。

1、下载编译libevent

  下载当前最新的libevent稳定版本libevent-2.0.21-stable.tar.gz安装包,解压到某个固定目录。打开Visual Studio的Command Prompt终端,进入libevent-2.0.21-stable目录,输入如下指令编译libevent代码库:

nmake /f Makefile.nmake

2、收集libevent相关头文件和库文件

  libevent编译完成后,生成的相关静态库文件包括三个:libevent.lib libevent_core.lib libevent_extras.lib,可以单独建立一个lib文件夹存放;

  相关的头文件包括三个地方:(1)libevent-2.0.12-stable\include\*   (2)libevent-2.0.12-stable\WIN32-Code\*    (3)libevent-2.0.12-stable\*.h

把这些头文件都复制一下放到一个单独的include文件夹中方便管理。

3、在VS工程中指定libevent的include和lib文件夹

  有两个地方指定:(1)新建一个工程后,在工程的“配置属性”-->“C/C++”-->“常规”-->“附加包含目录”中给出include文件夹路径;(2)直接在VS“工具”-->“选项”-->“项目和解决方案”-->“VC++目录”中给出include和lib目录路径。

  相比而言,第一种方式我发现在代码中虽然编译不会出错,但是不能自动定位头文件和函数位置;第二种方式就能够清晰定位出libevent头文件和函数的位置。

4、设置工程“运行时库”属性

  在工程的“配置属性”-->“C/C++”-->“代码生成”-->“运行时库”中选择“多线程(/MT)”(这里需要与Libevent库选择相同的编译版本,如果Libevent是Debug版本静态库,则可选择/MD)。

5、设置工程依赖和忽略函数库

  在工程的“配置属性”-->“链接器”-->“输入”中指出“附加依赖项”包括:ws2_32.lib wsock32.lib libevent.lib libevent_core.lib libevent_extras.lib

  这一步的必要性在于,要使用libevent库中的函数,必须要把函数的定义库链接进来。其实,在代码中让编译器连接也是一样的,等价于在代码中添加如下语句:

#include <WinSock2.h>
#include "event.h" #pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "libevent.lib")
#pragma comment(lib, "libevent_core.lib")
#pragma comment(lib, "libevent_extras.lib")

6、测试代码

  来一段测试代码试一下:

 #include <stdio.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <evhttp.h>
void root_handler(struct evhttp_request *req, void *arg)
{
struct evbuffer *buf = evbuffer_new();
if(!buf)
{
puts("failed to create response buffer");
return;
}
evbuffer_add_printf(buf, "Hello: %s\n", evhttp_request_uri(req));
evhttp_send_reply(req, HTTP_OK, "OK", buf);
} void generic_handler(struct evhttp_request *req, void *arg)
{
struct evbuffer *buf = evbuffer_new();
if(!buf)
{
puts("failed to create response buffer");
return;
}
evbuffer_add_printf(buf, "Requested: %s\n", evhttp_request_uri(req));
evhttp_send_reply(req, HTTP_OK, "OK", buf);
} int main(int argc, wchar_t* argv[])
{
struct evhttp *httpd;
WSADATA wsaData;
DWORD Ret;
if ((Ret = WSAStartup(MAKEWORD(, ), &wsaData)) != )
{
printf("WSAStartup failed with error %d\n", Ret);
return -;
}
event_init();
httpd = evhttp_start("0.0.0.0", );
if(!httpd)
{
return ;
}
evhttp_set_cb(httpd, "/", root_handler, NULL);
evhttp_set_gencb(httpd, generic_handler, NULL);
printf("httpd server start OK!\n");
event_dispatch();
evhttp_free(httpd);
WSACleanup();
return ;
}

[原]网络库libevent在Visual Studio中的使用方法的更多相关文章

  1. Visual Studio 中快速创建方法 Generate a method in Visual Studio

    2020-04-04 https://docs.microsoft.com/en-us/visualstudio/ide/reference/generate-method?view=vs-2019 ...

  2. 在Visual Studio 中使用git——浏览版本库(七)

    在Visual Studio 中使用git--什么是Git(一) 在Visual Studio 中使用git--给Visual Studio安装 git插件(二) 在Visual Studio 中使用 ...

  3. 如何在 Visual Studio 中使用 Git 同步代码到 CodePlex

    开源社区不管在国内还是国外都很火热,微软也曾因为没有开源而倍受指责,但是随着 .Net framework.ASP.Net MVC等框架的逐渐开源,也让大家看到了微软开源的步伐.CodePlex 则是 ...

  4. [转]如何在 Visual Studio 中使用 Git 同步代码到 CodePlex

    本文转自:http://www.cnblogs.com/stg609/p/3673782.html 开源社区不管在国内还是国外都很火热,微软也曾因为没有开源而倍受指责,但是随着 .Net framew ...

  5. Visual Studio 中的 .NET Framework 类库

    Visual Studio 中的 .NET Framework 类库 .NET Framework 类库由命名空间组成.每个命名空间都包含可在程序中使用的类型:类.结构.枚举.委托和接口. 当您在 V ...

  6. 在 Visual Studio 中使用 Q# 进行量子编程

    1 量子计算机与量子编程 1.1 量子计算机 Quantum computing is computing using quantum-mechanical phenomena, such as su ...

  7. 利用Visual GDB在Visual Studio中进行Android开发

    转载请注明http://www.cnblogs.com/adong7639/p/4119467.html 无意中发现了Visual GDB这个工具,可以再Visual Studio中进行Android ...

  8. (15)Visual Studio中使用PCL项目加入WCF WebService参考

    原文 Visual Studio中使用PCL项目加入WCF WebService参考 Visual Studio中使用PCL项目加入WCF WebService参考 作者:Steven Chang 2 ...

  9. 如何:使用 Visual Studio 中的一键式发布来部署 Web 应用程序项目

    原文: 如何:使用 Visual Studio 中的一键式发布来部署 Web 应用程序项目 本主题介绍如何在以下产品中使用 一键式发布 发布(部署)Web 应用程序项目: Visual Studio ...

随机推荐

  1. iOS-Block两个界面传值

    先说一下思路: 首先,创建两个视图控制器,在第一个视图控制器中创建一个Label和一个Button,其中Label是为了显示第二个视图控制器传过来的字符串, Button是为了push到第二个界面. ...

  2. 原来DataTable的Distinct竟如此简单!

    DataView可以帮我们直接获取Distinct数据, DataTable dataTable;DataView dataView = dataTable.DefaultView; DataTabl ...

  3. 【题解】【BT】【Leetcode】Binary Tree Preorder/Inorder/Postorder (Iterative Solution)

    [Inorder Traversal] Given a binary tree, return the inorder traversal of its nodes' values. For exam ...

  4. jconsole远程查看jvm性能

    Java VisualVM -- JDK自带的强大性能监测工具 这里给出了另一种,可以通过jdk下bin目录下的jconsole.exe来观察java程序内存的使用及变化情况 一般远程调试可以用,性能 ...

  5. 跟开涛老师学shiro -- 身份验证

    身份验证,即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明. 在shiro中,用户需要提供principals (身份)和cre ...

  6. Java获得文件的创建时间(精确到秒)

    jni C/C++ 头文件:MyFileTime.h C/C++ code /* DO NOT EDIT THIS FILE - it is machine generated */#include ...

  7. bootstrap部分---网格系统;(几天没写博客了,为了潜心研究一下bootstrap)

    1工作原理: (1)行必须放置在 .container class 内,以便获得适当的对齐(alignment)和内边距(padding). (2)使用行来创建列的水平组. (3)内容应该放置在列内, ...

  8. ✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  9. leetcode 113 Path Sum II ----- java

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  10. poj2492 带权并查集

    题意:研究一种生物,有n个生物个体,发现有一些之间进行了交配,给出了这些关系,问是否有同性恋的bug出现. 用0\1表示某元素和其祖先元素的性别关系,0 为相同,1 为不同,用 mod2 实现累计处理 ...