libevent-2.1.9-beta.tar.gz 编译及使用

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 20:49   977   0

libevent-2.1.9-beta.tar.gz: 下载:https://github.com/libevent/libevent/releases

参考:https://blog.csdn.net/xiaoluer/article/details/56280681

下面是无openssl的 ------------- BEGIN 最终生成 libevent.lib;libevent_core.lib;libevent_extras.lib;

由此我们知道了这个脚本 E:\canDel\libevent-2.1.9-beta\Makefile.nmake 的真正用法:

全量编译:nmake /f Makefile.nmake [all] //all可以省略,默认是all
库编译: nmake /f Makefile.nmake static_libs
测试编译:nmake /f Makefile.nmake tests
清理输出:nmake /f Makefile.nmake clean

下面是无openssl的 ------------- END

下面是有openssl的 ------------- BEGIN 最终生成 libevent.lib;libevent_core.lib;libevent_extras.lib;libevent_openssl.lib;

命令: E:\canDel\libevent-2.1.9-beta\openssl -f Makefile.nmake


其中会出现如上图所示的失败的找不到.h的错误,更改源码中的路径即可。生成成功,如下图所示:

下面是有openssl的 ------------- END

下面是用VS来进行测试:添加 链接;libevent.lib;libevent_core.lib;libevent_extras.lib;libevent_openssl.lib;.

代码如下所示:

// test_libevent-2.1.9-beta.cpp : 定义控制台应用程序的入口点。
//



// LibeventTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>

#ifndef WIN32
#include <netinet/in.h>
# ifdef _XOPEN_SOURCE_EXTENDED
#  include <arpa/inet.h>
# endif
#include <sys/socket.h>
#endif

#include "event2/bufferevent.h"
#include "event2/buffer.h"
#include "event2/listener.h"
#include "event2/util.h"
#include "event2/event.h"

#include <WinSock2.h>

static const char MESSAGE[] = "Hello, World!\n";

static const int PORT = 9995;


static void conn_writecb(struct bufferevent *bev, void *user_data)
{
 struct evbuffer *output = bufferevent_get_output(bev);
 if (evbuffer_get_length(output) == 0)
 {
  printf("flushed answer\n");
  bufferevent_free(bev);
 }
}

static void conn_eventcb(struct bufferevent *bev, short events, void *user_data)
{
 if (events & BEV_EVENT_EOF)
 {
  printf("Connection closed.\n");
 }
 else if (events & BEV_EVENT_ERROR)
 {
  printf("Got an error on the connection: %s\n",
   strerror(errno));/*XXX win32*/
 }
 /* None of the other events can happen here, since we haven't enabled
 * timeouts */
 bufferevent_free(bev);
}

static void signal_cb(evutil_socket_t sig, short events, void *user_data)
{
 struct event_base *base = (struct event_base *)user_data;
 struct timeval delay = { 2, 0 };

 printf("Caught an interrupt signal; exiting cleanly in two seconds.\n");

 event_base_loopexit(base, &delay);
}

static void listener_cb(struct evconnlistener *listener, evutil_socket_t fd,
struct sockaddr *sa, int socklen, void *user_data)
{
 struct event_base *base = (struct event_base *)user_data;
 struct bufferevent *bev;

 bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
 if (!bev)
 {
  fprintf(stderr, "Error constructing bufferevent!");
  event_base_loopbreak(base);
  return;
 }
 bufferevent_setcb(bev, NULL, conn_writecb, conn_eventcb, NULL);
 bufferevent_enable(bev, EV_WRITE);
 bufferevent_disable(bev, EV_READ);

 bufferevent_write(bev, MESSAGE, strlen(MESSAGE));
}

int main(int argc, char **argv)
{
 struct event_base *base;
 struct evconnlistener *listener;
 struct event *signal_event;

 struct sockaddr_in sin;

#ifdef WIN32
 WSADATA wsa_data;
 WSAStartup(0x0201, &wsa_data);
#endif

 base = event_base_new();
 if (!base)
 {
  fprintf(stderr, "Could not initialize libevent!\n");
  return 1;
 }

 memset(&sin, 0, sizeof(sin));
 sin.sin_family = AF_INET;
 sin.sin_port = htons(PORT);

 listener = evconnlistener_new_bind(base, listener_cb, (void *)base,
  LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, -1,
  (struct sockaddr*)&sin,
  sizeof(sin));

 if (!listener)
 {
  fprintf(stderr, "Could not create a listener!\n");
  return 1;
 }

 signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);

 if (!signal_event || event_add(signal_event, NULL)<0)
 {
  fprintf(stderr, "Could not create/add a signal event!\n");
  return 1;
 }

 event_base_dispatch(base);

 evconnlistener_free(listener);
 event_free(signal_event);
 event_base_free(base);

 printf("done\n");
 return 0;
}

编译,出现如下错误,分别为图1 图2 图3,解决方法如下:

错误解决办法:
将E:\canDel\libevent-2.1.9-beta\WIN32-Code\nmake\event2\event-config.h复制到E:\canDel\test_libevent-2.1.9-beta\test_libevent-2.1.9-beta\libevent2.1.9include\event2

错误解决办法:
添加预处理定义:;_CRT_SECURE_NO_WARNINGS;

添加对库的引用:;ws2_32.lib;wsock32.lib;

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP