vc使用libevent网络库Word文档格式.docx

上传人:b****1 文档编号:1005438 上传时间:2023-04-30 格式:DOCX 页数:25 大小:541KB
下载 相关 举报
vc使用libevent网络库Word文档格式.docx_第1页
第1页 / 共25页
vc使用libevent网络库Word文档格式.docx_第2页
第2页 / 共25页
vc使用libevent网络库Word文档格式.docx_第3页
第3页 / 共25页
vc使用libevent网络库Word文档格式.docx_第4页
第4页 / 共25页
vc使用libevent网络库Word文档格式.docx_第5页
第5页 / 共25页
vc使用libevent网络库Word文档格式.docx_第6页
第6页 / 共25页
vc使用libevent网络库Word文档格式.docx_第7页
第7页 / 共25页
vc使用libevent网络库Word文档格式.docx_第8页
第8页 / 共25页
vc使用libevent网络库Word文档格式.docx_第9页
第9页 / 共25页
vc使用libevent网络库Word文档格式.docx_第10页
第10页 / 共25页
vc使用libevent网络库Word文档格式.docx_第11页
第11页 / 共25页
vc使用libevent网络库Word文档格式.docx_第12页
第12页 / 共25页
vc使用libevent网络库Word文档格式.docx_第13页
第13页 / 共25页
vc使用libevent网络库Word文档格式.docx_第14页
第14页 / 共25页
vc使用libevent网络库Word文档格式.docx_第15页
第15页 / 共25页
vc使用libevent网络库Word文档格式.docx_第16页
第16页 / 共25页
vc使用libevent网络库Word文档格式.docx_第17页
第17页 / 共25页
vc使用libevent网络库Word文档格式.docx_第18页
第18页 / 共25页
vc使用libevent网络库Word文档格式.docx_第19页
第19页 / 共25页
vc使用libevent网络库Word文档格式.docx_第20页
第20页 / 共25页
亲,该文档总共25页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

vc使用libevent网络库Word文档格式.docx

《vc使用libevent网络库Word文档格式.docx》由会员分享,可在线阅读,更多相关《vc使用libevent网络库Word文档格式.docx(25页珍藏版)》请在冰点文库上搜索。

vc使用libevent网络库Word文档格式.docx

使用

3.1 

创建Win32Console工程

创建一个最简单的Win32Console(中文叫Win32控制台)工程,默认设置即可。

3.2 

准备libevent库

在工程目录下分别创建inc和lib目录。

把libevent解压目录下的所有.h文件复制到inc目录。

把libevent解压目录下的include下的event2目录复制到inc目录。

把libevent解压目录下的WIN32-Code下的event2目录和tree.h复制到inc目录。

把libevent解压目录下编译生成的3个.lib文件(libevent.lib、libevent_core.lib和libevent_extras.lib)复制到lib目录。

3.3 

添加代码

修改stdafx.h文件,内容如下:

#pragmaonce

#defineWIN32_LEAN_AND_MEAN

#include<

stdio.h>

tchar.h>

windows.h>

winsock2.h>

event.h>

evhttp.h>

接着,修改跟工程同名的.cpp文件(初始内容就一个main函数),内容如下:

#include"

stdafx.h"

voidroot_handler(structevhttp_request*req,void*arg)

{

structevbuffer 

*buf=evbuffer_new();

if(!

buf)

{

puts("

failedtocreateresponsebuffer"

);

return;

}

evbuffer_add_printf(buf,"

Hello:

%s\n"

evhttp_request_uri(req));

evhttp_send_reply(req,HTTP_OK,"

OK"

buf);

}

voidgeneric_handler(structevhttp_request*req,void*arg)

Requested:

int_tmain(intargc,_TCHAR*argv[])

structevhttp 

*httpd;

WSADATA 

wsaData;

DWORDRet;

if((Ret=WSAStartup(MAKEWORD(2,2),&

wsaData))!

=0)

printf("

WSAStartupfailedwitherror%d\n"

Ret);

return-1;

event_init();

httpd=evhttp_start("

0.0.0.0"

80);

httpd)return1;

evhttp_set_cb(httpd,"

/"

root_handler,NULL);

evhttp_set_gencb(httpd,generic_handler,NULL);

httpdserverstartOK!

\n"

event_dispatch();

evhttp_free(httpd);

WSACleanup();

return0;

3.4 

修改工程设置

在常规设置中字符集设置为MBCS。

在C++常规项中,添加附加包含目录:

inc;

inc/event2。

在C++代码生成中,修改运行时库为MTd(Release模式下,设置为MT)。

在链接器常规项中,添加附加目录:

lib。

在链接器输入项中,添加依赖项:

ws2_32.libwsock32.liblibevent.liblibevent_core.liblibevent_extras.lib。

在链接器输入项中,添加忽略指定库:

libcmt.lib(Release模式下空着就行)。

3.5 

编译&

测试

编译吧,骚年。

如果一切顺利,运行起来,会在命令行窗口中显示“httpdserverstartOK!

”。

要是运气不济,可以到代码中找到httpd=evhttp_start("

这里的80是指程序监听的端口,很可能跟本地其他程序冲突了。

如果是这样,就改个其他的值吧(比如8080)。

打开浏览器,在地址栏输入“http:

//localhost/hello,libevent.”,如果浏览器显示“Requested:

/hello,libevent.”即代表RP爆表了(如果是其他,那就继续自力更生吧)。

3.6 

代码下载

按照libevent的文档,使用VC的nmake-fMakefile.nmake即可编译32位release模式。

因为项目中要求编译64位的版本,需要在Makefile.nmake中添加一个LIBFLAGS选项/MACHINE:

X64

如果要加调试信息,可以在CFLAGS中加入/Zi,32位加调试选项是CFLAGS中加/ZI,当然要调整优化选项/Ox

要正确运行"

nmake-fMakefile.nmake"

,请走

"

microsoftvisualstudio 

->

visualtools->

visualstudiocommandprompt"

x64走

visualstudiox64win64commandprompt"

至于

visualstudiox64win64commandprompt"

visualstudiox64crosstoolscommandprompt"

的区别/困惑,看

或得些许启发

  一、编译生成Libevent2.0.10静态链接库。

  

  1、修改“D:

\libevent-2.0.10-stable\event_iocp.c”、“D:

\libevent-2.0.10-stable\evthread_win32.c”、“D:

\libevent-2.0.10-stable\listener.c”三个文件,在文件开头分别加上一行:

#define_WIN32_WINNT0x0500

  2、鼠标点击Windows左下角的【开始】-【所有程序】,找到【MicrosoftVisualStudio2005】,执行下图中的脚本:

  3、按照下图中的方法编译Libevent2.0.10:

  4、生成的“libevent.lib”、“libevent_core.lib”、“libevent_extras.lib”三个文件就是我们需要的Libevent静态链接库。

  二、利用Libevent静态链接库,实现一个简单的HTTPWeb服务器程序

  1、打开VisualStudio2005,新建一个项目

  2、选择在“d:

\test”目录内创建一个“Win32控制台应用程序”

  3、按照下图进行选择

  4、创建完成项目后,会自动生成“d:

\test\httpd\”目录,在该目录内创建一个“httpd.c”文件,内容如下:

viewplaincopytoclipboardprint?

1.#include 

<

 

2. 

3.#define 

WIN32_LEAN_AND_MEAN 

4.#include 

5.#include 

6. 

7.#include 

8.#include 

9. 

10.void 

root_handler(struct 

evhttp_request 

*req, 

void 

*arg) 

11.{ 

12. 

struct 

evbuffer 

*buf 

evbuffer_new();

13. 

if(!

buf){ 

14. 

puts("

failed 

to 

create 

response 

buffer"

15. 

return;

16. 

17. 

18. 

evbuffer_add_printf(buf, 

%s\n"

 

evhttp_request_uri(req));

19. 

evhttp_send_reply(req, 

HTTP_OK, 

buf);

20.} 

21. 

22.void 

generic_handler(struct 

23.{ 

24. 

25. 

26. 

27. 

28. 

29. 

30. 

31. 

32.} 

33. 

34.int 

main(int 

argc, 

wchar_t* 

argv[]) 

35.{ 

36. 

evhttp 

*httpd;

37. 

38. 

WSADATA 

wsaData;

39. 

DWORD 

Ret;

40. 

if 

((Ret 

WSAStartup(MAKEWORD(2, 

2), 

&

wsaData)) 

!

0) 

41. 

printf("

WSAStartup 

with 

error 

%d\n"

Ret);

42. 

return 

-1;

43. 

44. 

45. 

event_init();

46. 

47. 

httpd 

evhttp_start("

8505);

48. 

httpd){ 

49. 

1;

50. 

51. 

52. 

evhttp_set_cb(httpd, 

root_handler, 

NULL);

53. 

evhttp_set_gencb(httpd, 

generic_handler, 

54. 

55. 

server 

start 

OK!

56. 

57. 

event_dispatch();

58. 

59. 

evhttp_free(httpd);

60. 

61. 

WSACleanup();

62. 

0;

63.} 

 5、回到VisualStudio2005,在左侧的【源文件】中选择【添加】-【现有项】,将上一步创建的“httpd.c”文件添加进来。

  6、在【解决方案“httpd”】上点击鼠标右键,选择【属性】

  7、将【配置】改为“Release”

  8、将“D:

\libevent-2.0.10-stable\include”整个目录复制到“D:

\test\httpd\include”;

将“D:

\libevent-2.0.10-stable\WIN32-Code”目录内的“tree.h”文件和“event2”子目录,复制到“D:

\test\httpd\include\”内;

\libevent-2.0.10-stable\”目录内的所有“*.h”头文件复制到“D:

\test\httpd\include\”内。

可以在Windows左下角的【开始】-【运行】中输入“cmd”回车,在命令行窗口中执行以下命令,完成复制过程。

mkdirD:

\test\httpd\include\

xcopy/E/H/RD:

\libevent-2.0.10-stable\include\*D:

\libevent-2.0.10-stable\WIN32-Code\*D:

\libevent-2.0.10-stable\*.hD:

  9、回到VisualStudio2005,在左侧菜单中【解决方案“httpd”】下面一行【httpd】上点击鼠标邮件,选择【属性】,对每项内容进行修改。

下图中红框内的数据即为修改后的数据。

  注:

下图中,附加依赖项填写:

ws2_32.libwsock32.liblibevent.liblibevent_core.liblibevent_extras.lib

  忽略特定库填写:

libc.lib;

msvcrt.lib;

libcd.lib;

libcmtd.lib;

msvcrtd.lib

1.

  10、完成设置后,在【解决方案“httpd”】上点击鼠标右键,选择【生成解决方案】。

如果是重新编译,可以选择【重新生成解决方案】。

生成成功后,“d:

\test\httpd\Release”目录内的“httpd.exe”即为生成的可执行文件。

  11、双击“httpd.exe”运行后,打开浏览器,访问“http:

//127.0.0.1:

8505/”,则可以看到以下信息:

一个简单的HTTPWebServer输出的内容。

  12、如果你觉得像DOS程序一样的“httpd.exe”可执行文件图标不好看、没有显示版本信息,那么,你可以按照下图步骤,添加ICO图标文件。

  13、添加版本信息

  14、大功告成,下面显示的是带有自定义图标、版本信息的“httpd.exe”可执行程序。

libevent在windows平台静态库冲突问题

发表于2013年11月25日由comi

1.编译参数问题

  libevent直接提供了Makefile.nmake文件。

通过visualstudio提供的开发人员命令提示工具,通过命令nmake-fMakefile.nmake就可以很方便生成需要的lib文件。

导入到我们正在开发的工程中。

  将工程的运行库设置成多线程调试DLL(/MDd)(设置成其他选项也会有类似的问题),编译时会出类似如下warning:

LINK:

warningLNK4098:

默认库“LIBCMT”与其他库的使用冲突;

请使用/NODEFAULTLIB:

library

  解决方案:

如果运行时库设置成运行库设置成多线程调试DLL(/MDd),修改Makefile.nmake文件(注意test文件夹里面还有一个),找到编译参数/Ox,改成/Od/MDd/Zi。

保存,重新编译,记得编译前用nmake-fMakefile.nmakeclean清理之前生成的结果。

  上面用到的编译参数请参阅

2.本博转载了libevent的参考手册的翻译。

里面很多socket相关的例子。

但是在visualstiduo下面无法直接使用。

比如SOCK_STREAM,0)返回了-1。

细细查看libevent提供的例子后发现。

libevent对windows下异步套接字初始化没有做封装。

需要我们在启动socket相关操作前自己对其初始化。

示例代码如下:

#ifdefWIN32

WORDwVersionRequested;

WSADATAwsaData;

wVersionRequested=MAKEWORD(2,2);

WSAStartup(wVersionRequested,&

wsaData);

#endif

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 临时分类 > 批量上传

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2