[Original] how to use the network library libevent in Visual Studio

Source: Internet
Author: User

Libevent is an event-triggered network library. It is applicable to Windows, Linux, BSD, and other platforms. It uses select, epoll, kqueue, and other systems to call the management event mechanism. The famous distributed cache software memcached is also libevent based, and libevent can be used across platforms. It also seems to have extraordinary performance according to the statistics published on the official libevent website.

1. Download and compile libevent

Download the latest libevent libevent-2.0.21-stable.tar.gz installation package, and decompress it to a fixed directory. Open the command prompt terminal of Visual Studio, enter the libevent-2.0.21-stable directory, enter the following command to compile the libevent code library:

nmake /f Makefile.nmake

2. Collect libevent header files and library files

After libevent is compiled, three static library files are generated:Libevent. Lib libevent_core.lib libevent_extras.lib, You can create a separate lib folder for storage;

Related header files include three places: (1) libevent-2.0.12-stable \ include \ * (2) libevent-2.0.12-stable \ * (3) WIN32-Code \ *. h

Copy these header files and put them in a separate include folder for convenient management.

3. Specify the libevent include and Lib folders in the vs project.

There are two places to specify: (1) After creating a project, in the project's "configuration properties" --> "C/C ++" --> "general" --> "add include directory", the include folder path is provided. (2) the include and lib directory paths are provided directly in vs "Tools"> "options"> "projects and Solutions"> "VC ++ directory.

In comparison, the first method is to find that, although the code compilation will not go wrong, the header file and function location cannot be automatically located. The second method is to clearly locate the libevent header file and function location.

4. Set the "RunTime Database" attribute of the project

Choose "configuration properties"> "C/C ++"> "code generation"> "Runtime Library" in the project and select "multithreading (/mt )".

5. Set Project dependencies and ignore function libraries

In the "configuration properties" --> "linker" --> "input" of the project, the "additional dependencies" include ws2_32.lib wsock32.lib libevent. Lib libevent_core.lib libevent_extras.lib.

In "ignore a specific library", the following methods are available: libc. Lib; msvcrt. Lib; libcd. Lib; libcmtd. Lib; msvcrtd. Lib

6. Test code

Try a piece of test code:

 1 #include <stdio.h> 2 #define WIN32_LEAN_AND_MEAN 3 #include <windows.h> 4 #include <winsock2.h> 5 #include <event.h> 6 #include <evhttp.h> 7 void root_handler(struct evhttp_request *req, void *arg) 8 { 9     struct evbuffer *buf = evbuffer_new();10     if(!buf)11     {12         puts("failed to create response buffer");13         return;14     }15     evbuffer_add_printf(buf, "Hello: %s\n", evhttp_request_uri(req));16     evhttp_send_reply(req, HTTP_OK, "OK", buf);17 }        18 19 void generic_handler(struct evhttp_request *req, void *arg)20 {21     struct evbuffer *buf = evbuffer_new();22     if(!buf)23     {24         puts("failed to create response buffer");25         return;26     }27     evbuffer_add_printf(buf, "Requested: %s\n", evhttp_request_uri(req));28     evhttp_send_reply(req, HTTP_OK, "OK", buf);29 }30 31 int main(int argc, wchar_t* argv[])32 {33     struct evhttp *httpd;    34     WSADATA wsaData;35     DWORD Ret;36     if ((Ret = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0)37     {38         printf("WSAStartup failed with error %d\n", Ret);39         return -1;40     }41     event_init();42     httpd = evhttp_start("0.0.0.0", 8505);43     if(!httpd)44     {45         return 1;46     }47     evhttp_set_cb(httpd, "/", root_handler, NULL);48     evhttp_set_gencb(httpd, generic_handler, NULL);49     printf("httpd server start OK!\n");50     event_dispatch();51     evhttp_free(httpd);52     WSACleanup();53     return 0;54 }
View code

 

[Original] how to use the network library libevent in Visual Studio

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.