ubuntu下安装libevent(libevent-2.0.22-stable),Qt下运行

1、下载

http://libevent.org/ 目前最新版本libevent-2.0.22-stable.tar.gz

2、安装

解压;tar  -zxvf  libevent-2.0.22-stable.tar.gz

进入目录;

./configure --prefix=/usr         配置目录

make

make install

3.验证

root@jq-virtual-machine:/mywork/jqproject/libevent-2.0.22-stable# ls -al /usr/lib |grep libevent
lrwxrwxrwx   1 root root           21  9月 26 15:18 libevent-2.0.so.5 -> libevent-2.0.so.5.1.9
-rw-r--r--   1 root root       281504 12月 12  2011 libevent-2.0.so.5.1.4
-rwxr-xr-x   1 root root       827693  9月 26 15:18 libevent-2.0.so.5.1.9
-rw-r--r--   1 root root      1159932  9月 26 15:18 libevent.a
lrwxrwxrwx   1 root root           26  9月 26 15:18 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9
-rwxr-xr-x   1 root root       503577  9月 26 15:18 libevent_core-2.0.so.5.1.9
-rw-r--r--   1 root root       731100  9月 26 15:18 libevent_core.a
-rwxr-xr-x   1 root root          979  9月 26 15:18 libevent_core.la
lrwxrwxrwx   1 root root           26  9月 26 15:18 libevent_core.so -> libevent_core-2.0.so.5.1.9
lrwxrwxrwx   1 root root           27  9月 26 15:18 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x   1 root root       341944  9月 26 15:18 libevent_extra-2.0.so.5.1.9
-rw-r--r--   1 root root       428904  9月 26 15:18 libevent_extra.a
-rwxr-xr-x   1 root root          986  9月 26 15:18 libevent_extra.la
lrwxrwxrwx   1 root root           27  9月 26 15:18 libevent_extra.so -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x   1 root root          944  9月 26 15:18 libevent.la
lrwxrwxrwx   1 root root           30  9月 26 15:18 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.9
-rwxr-xr-x   1 root root        19118  9月 26 15:18 libevent_pthreads-2.0.so.5.1.9
-rw-r--r--   1 root root        12182  9月 26 15:18 libevent_pthreads.a
-rwxr-xr-x   1 root root         1007  9月 26 15:18 libevent_pthreads.la
lrwxrwxrwx   1 root root           30  9月 26 15:18 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.9
lrwxrwxrwx   1 root root           21  9月 26 15:18 libevent.so -> libevent-2.0.so.5.1.9

4、Qt下运行实例

a、新建一个基于控制台的程序
b、.pro文件中右键->添加库->系统库,找到libevent库安装路径,我的在/usr/lib下,添加三个,最终效果如下:
unix:!macx: LIBS += -levent
unix:!macx: LIBS += -levent_core
unix:!macx: LIBS += -levent_extra
c、main.cpp文件内容:
#include <QCoreApplication>
#include <event2/event.h>
#include <event2/event-config.h>
#include <event2/event_struct.h>


#include <QTextStream>


void timeout_cb(int sock, short event, void *arg)
{
    static int i=0;
    i++;
    QTextStream cin(stdin, QIODevice::ReadOnly);
    QTextStream cout(stdout, QIODevice::WriteOnly);
    QTextStream cerr(stderr, QIODevice::WriteOnly);
    QString str =QString("current nuber is: %1").arg(i);
    cout <<str<<endl;
    cout.flush();//或者加cout.flush()才能输出
    struct timeval tv;
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    // 重新添加定时事件(定时事件触发后默认自动删除)
    event_add((struct event*)arg, &tv);
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    struct event timeout;
    struct event_base *base;


    struct timeval tv;
    int flags;
    // 初始化
    /* Initalize the event library */
    base = event_base_new();
    /* Initalize one event */
    event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);
     tv.tv_sec = 1;
     tv.tv_usec = 0;
     // 添加定时事件
     event_add(&timeout, &tv);
     // 事件循环
     event_base_dispatch(base);
    return a.exec();
}

运行效果图:



最近在开发im服务器 需要大并发链接 QT默认的是使用select模型的 这种轮询方式非常慢 在高并发连接 我们需要epoll才能发挥linux服务器的性能 而且使用简单 整个服务端代码架构无需修改 直接可以使用 只要在 main文件添加: int main int argc char argv[] { #ifdef Q OS LINUX QCoreApplication::setEventDispatcher new EventDispatcherLibEvent ; qInstallMessageHandler customMessageHandler ; #endif QCoreApplication a argc argv ; auto ser new ConfigServer; ser >startServer ; return a exec ; } 在 pro文件添加 linux{ LIBS + levent core SOURCES + common eventdispatcher libevent eventdispatcher libevent cpp common eventdispatcher libevent eventdispatcher libevent config cpp common eventdispatcher libevent eventdispatcher libevent p cpp common eventdispatcher libevent socknot p cpp common eventdispatcher libevent tco eventfd cpp common eventdispatcher libevent tco pipe cpp common eventdispatcher libevent tco cpp common eventdispatcher libevent timers p cpp HEADERS + common eventdispatcher libevent common h common eventdispatcher libevent eventdispatcher libevent h common eventdispatcher libevent eventdispatcher libevent config h common eventdispatcher libevent eventdispatcher libevent config p h common eventdispatcher libevent eventdispatcher libevent p h common eventdispatcher libevent libevent2 emul h common eventdispatcher libevent qt4compat h common eventdispatcher libevent tco h common eventdispatcher libevent wsainit h } 可以直接跨平台了使用了 csdn博客:http: blog csdn net rushroom">最近在开发im服务器 需要大并发链接 QT默认的是使用select模型的 这种轮询方式非常慢 在高并发连接 我们需要epoll才能发挥linux服务器的性能 而且使用简单 整个服务端代码架构无需修改 直接可以使用 只要在 main文件添加: [更多]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值