libevent:windows环境配置+QT使用

目录

libevent是什么

编译

QT使用

测试代码


  • libevent是什么

Fast portable non-blocking network programming with Libevent

http://www.wangafu.net/~nickm/libevent-book/TOC.html

这篇文档讲的很清楚,尤其是Chapter 1: A tiny introduction to asynchronous IO.,基本上讲清楚了大多数的内容

  • 编译

环境:

1、win10 64位
2、vs2015专业版(nmake)/Cmake
3、libevent 2.1.11(官方下载链接)
4、libevent解压的文件夹目录为:“E:\libevent\libevent-2.1.11-stable”

 参考文章: vs2015编译方式和cmake编译方式.

  • QT使用

根据编译方式不同,配置QT环境.以下使用VS2015方式

创建项目libevent_test.

新建libevent目录下创建libevent.pri文件,并在libevent_test.pro文件中添加

QT       +=  network

INCLUDEPATH += $$PWD/libevent  #通过路径添加子文件内容
include($$PWD/libevent/libevent.pri) #添加子文件夹所需pri文件

将编译生成的文件放置于libevent目录下

E:\libevent\libevent-2.1.11-stable\include下                        event2文件夹及其内文件

E:\libevent\libevent-2.1.11-stable\WIN32-Code\nmake下   evconfig-private.h文件和event2文件夹及其内文件

E:\libevent\libevent-2.1.11-stable下                                     libevent.lib,libevent_core.liblibevent_extras.lib文件

 libevent.pri文件添加

INCLUDEPATH += -L$$PWD
LIBS += -ladvapi32 -lws2_32 -lwsock32
LIBS += -L$$PWD/ -llibevent -llibevent_core -llibevent_extras
  • 测试代码

 main.cpp

#include 
#include 
#include 
#include 
#include "include/event2/listener.h"
#include "include/event2/buffer.h"
#include "include/event2/bufferevent.h"
#include "include/event2/event.h"
#include "include/event2/event_compat.h"
#pragma comment(lib, "ws2_32.lib")

static  void echo_read_cb(struct bufferevent *bev,void *ctx)
{
    struct evbuffer *input = bufferevent_get_input(bev);
    struct evbuffer *output = bufferevent_get_output(bev);

    evbuffer_add_buffer(output,input);
    qDebug() << " port  read " ;
}

static  void echo_event_cb(struct bufferevent *bev,short events,void *ctx)
{
    if(events & BEV_EVENT_ERROR)
        qDebug()<< "error form bufferevent";
    if(events & (BEV_EVENT_EOF | BEV_EVENT_ERROR))
        bufferevent_free(bev);
}

static  void accept_conn_cb(struct evconnlistener *listener,evutil_socket_t fd,struct sockaddr *address,int socklen,void *ctx)
{
    struct  event_base *base = evconnlistener_get_base(listener);
    struct bufferevent *bev = bufferevent_socket_new(base,fd,BEV_OPT_CLOSE_ON_FREE);

    bufferevent_setcb(bev,echo_read_cb,NULL,echo_event_cb,NULL);
    bufferevent_enable(bev,EV_READ|EV_WRITE);
    qDebug() << " port  connect ";
}

static  void accept_error_cb(struct evconnlistener *listener,void *ctx)
{
    struct event_base *base = evconnlistener_get_base(listener);
    event_base_loopexit(base,NULL);
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    WORD sockVerson = MAKEWORD(2, 2);
    WSADATA wsaData;
    if (WSAStartup(sockVerson, &wsaData))
    {
        qDebug() << "init WinSock fail";
        return 1;
    }

    qDebug() << "init WinSock yes";
    struct event_base *base;
    struct evconnlistener *listener;
    struct sockaddr_in sin;

    int port = 9876;

    base = event_base_new();

    memset(&sin,0,sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = htonl(0);
    sin.sin_port = htons(port);

    listener = evconnlistener_new_bind(base,accept_conn_cb,NULL,
                                       LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,-1,(struct sockaddr*)&sin,sizeof(sin));
    if(!listener)
    {
        qDebug()<<"could't create listener";
        return 1;
    }
    evconnlistener_set_error_cb(listener,accept_error_cb);
    event_base_dispatch(base);

    qDebug()<<"thread quit";

    return a.exec();
}

验证连接

libevent:windows环境配置+QT使用_第1张图片

telnet 连接 

libevent:windows环境配置+QT使用_第2张图片

连接成功

libevent:windows环境配置+QT使用_第3张图片

你可能感兴趣的:(qt,开发语言,libevent)