Statically compiled Libevent 2.0.10 on Windows and implements a simple HTTP server

Source: Internet
Author: User

Statically compiled Libevent 2.0.10 on Windows and implements a simple HTTP server large | Medium | Small [2011-3-30 08:40 | by Zhang Banquet] [Article Zhang Banquet this version: v1.0 last modified: 2011.03.30 reproduced Please specify the original link: http://blog.zyan.cc/libevent_windows/]

This article describes how to build the Libevent 2.0.10 static link library using Microsoft Visual Studio 2005 compilation in the Windows operating system, and implement a simple HTTP Web server program using the Libevent static link library: HTTPD.E Xe

Assuming that the installation path for Visual Studio 2005 is "D:\Program Files\Microsoft Visual Studio 8\", Libevent 2.0.10 the extracted path to "d:\ Libevent-2.0.10-stable ".


   first, compile build Libevent 2.0.10 static link library.
  
1, modify "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 "Three files, add one line at the beginning of the file:
#define _WIN32_WINNT 0x0500

2. Click "Start"-"All Programs" in the lower left corner of windows, locate "Microsoft Visual Studio 2005" and execute the script:

  


3, according to the method of compiling Libevent 2.0.10:

  


4, the generated "Libevent.lib", "Libevent_core.lib", "Libevent_extras.lib" Three files are we need to libevent static link library.

  


   second, the use of Libevent static link library, to implement a simple HTTP Web server program

1. Open Visual Studio 2005 and create a new project

  


2. Choose to create a "Win32 console Application" in the "D:\test" directory

  


3. Follow the selection

  


4, after the completion of the project, will automatically generate a "d:\test\httpd\" directory, in the directory to create a "httpd.c" file, the contents are as follows:
View Plainprint?
  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. Puts ("failed to create response buffer");
  12. return;
  13. }
  14. evbuffer_add_printf (buf, "Hello:%s\n", Evhttp_request_uri (req));
  15. Evhttp_send_reply (req, HTTP_OK, "OK", buf);
  16. }
  17. void Generic_handler (struct evhttp_request *req, void *arg)
  18. {
  19. struct Evbuffer *buf = evbuffer_new ();
  20. if (!buf) {
  21. Puts ("failed to create response buffer");
  22. return;
  23. }
  24. evbuffer_add_printf (BUF, "requested:%s\n", Evhttp_request_uri (req));
  25. Evhttp_send_reply (req, HTTP_OK, "OK", buf);
  26. }
  27. int main (int argc, wchar_t* argv[])
  28. {
  29. struct evhttp *httpd;
  30. Wsadata Wsadata;
  31. DWORD Ret;
  32. if (Ret = WSAStartup (Makeword (2, 2), &wsadata))! = 0) {
  33. printf ("WSAStartup failed with error%d\n", Ret);
  34. return-1;
  35. }
  36. Event_init ();
  37. httpd = Evhttp_start ("0.0.0.0", 8505);
  38. if (!httpd) {
  39. return 1;
  40. }
  41. EVHTTP_SET_CB (httpd, "/", Root_handler, NULL);
  42. EVHTTP_SET_GENCB (httpd, Generic_handler, NULL);
  43. printf ("httpd server start ok!\n");
  44. Event_dispatch ();
  45. Evhttp_free (httpd);
  46. WSACleanup ();
  47. return 0;
  48. }


5. Back to Visual Studio 2005, in the left "source files" Select "Add"-"Existing Item", add the "HTTPD.C" file created in the previous step.

  


6. Right-click on the "solution" httpd "" and select "Properties"

  


7. Change "config" to "Release"

  


8, copy "D:\libevent-2.0.10-stable\include" Entire directory to "D:\test\httpd\include"; "D:\libevent-2.0.10-stable\WIN32-Code" The "tree.h" file and the "Event2" subdirectory within the directory are copied to "D:\test\httpd\include\", and All "*.h" headers in the D:\libevent-2.0.10-stable\ directory are copied to "D:\test \httpd\include\ "inside. You can enter "cmd" in Start-run in the lower-left corner of Windows and execute the following command in the command-line window to complete the replication process.
mkdir D:\test\httpd\include\
xcopy/e/h/r d:\libevent-2.0.10-stable\include\* D:\test\httpd\include\
xcopy/e/h/r d:\libevent-2.0.10-stable\win32-code\* D:\test\httpd\include\
xcopy/e/h/r d:\libevent-2.0.10-stable\*.h D:\test\httpd\include\

  

  


9. Back to Visual Studio 2005, in the left menu "solution" httpd "" on the next line "httpd" click on the mouse message, select "Properties" to modify each content. The data in the red box is the modified data.

  

  

  

  


Note: The additional dependencies are filled in:
Ws2_32.lib wsock32.lib libevent.lib libevent_core.lib libevent_extras.lib
Ignore Specific library fills:
Libc.lib;msvcrt.lib;libcd.lib;libcmtd.lib;msvcrtd.lib
  


10. After the setup is complete, right-click on the "solution" httpd "" and select "Build Solution". If you are recompiling, you can choose to rebuild the solution. After the build succeeds, "Httpd.exe" in the "D:\test\httpd\Release" directory is the resulting executable file.

  


11, double-click "Httpd.exe" after running, open the browser, access "http://127.0.0.1:8505/", you can see the following information: A simple HTTP Web Server output content.

  


12, if you feel like DOS program "Httpd.exe" executable icon is not good-looking, not display version information, then you can follow the steps to add the ICO icon file.

  

  


13. Add version Information

  

  


14, finished, the following shows a custom icon, version information, "Httpd.exe" executable program.

  


   attached 1: compiled Libevent 2.0.10 static link library with httpd Visual Studio 2005 project source code download
Download file Click here to download the file

   Download 2:ico icon Maker Tool
Download file Click here to download the file

Statically compiled Libevent 2.0.10 on Windows and implements a simple HTTP server

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.