网络库Libevent学习之安装与示例

Libevent是一个用C语言编写的、轻量级的开源高性能事件通知库,主要有以下几个亮点:事件驱动( event-driven),高性能;轻量级,专注于网络。Libevent包括事件管理、缓存管理、DNS、HTTP、缓存事件几大部分。

事件管理包括各种IO(socket)、定时器、信号等事件;缓存管理是指evbuffer功能;DNS是libevent提供的一个异步DNS查询功能;HTTP是libevent的一个轻量级http实现,包括服务器和客户端。

一、 Libevent安装

[root@fedora Workspace]# uname -r
4.17.5-100.fc27.x86_64
[root@fedora Workspace]# dnf list installed | grep libevent
libevent.x86_64                            2.0.22-6.fc27               @anaconda
[root@fedora Workspace]# dnf install libevent-devel
[root@fedora Workspace]# dnf list installed | grep libevent
libevent.x86_64                            2.0.22-6.fc27               @anaconda
libevent-devel.x86_64                      2.0.22-6.fc27               @fedora  
[root@fedora Workspace]# whereis libevent
libevent: /usr/lib64/libevent.so
[root@fedora Workspace]# ls -al /usr/lib64 | grep libevent
lrwxrwxrwx.   1 root root       21 Aug 10  2017 libevent-2.0.so.5 -> libevent-2.0.so.5.1.9
-rwxr-xr-x.   1 root root   303016 Aug 10  2017 libevent-2.0.so.5.1.9
lrwxrwxrwx.   1 root root       26 Aug 10  2017 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9
-rwxr-xr-x.   1 root root   185952 Aug 10  2017 libevent_core-2.0.so.5.1.9
lrwxrwxrwx    1 root root       26 Aug 10  2017 libevent_core.so -> libevent_core-2.0.so.5.1.9
lrwxrwxrwx.   1 root root       27 Aug 10  2017 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x.   1 root root   132328 Aug 10  2017 libevent_extra-2.0.so.5.1.9
lrwxrwxrwx    1 root root       27 Aug 10  2017 libevent_extra.so -> libevent_extra-2.0.so.5.1.9
lrwxrwxrwx.   1 root root       29 Aug 10  2017 libevent_openssl-2.0.so.5 -> libevent_openssl-2.0.so.5.1.9
-rwxr-xr-x.   1 root root    27752 Aug 10  2017 libevent_openssl-2.0.so.5.1.9
lrwxrwxrwx    1 root root       29 Aug 10  2017 libevent_openssl.so -> libevent_openssl-2.0.so.5.1.9
lrwxrwxrwx.   1 root root       30 Aug 10  2017 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.9
-rwxr-xr-x.   1 root root    10944 Aug 10  2017 libevent_pthreads-2.0.so.5.1.9
lrwxrwxrwx    1 root root       30 Aug 10  2017 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.9
lrwxrwxrwx    1 root root       21 Aug 10  2017 libevent.so -> libevent-2.0.so.5.1.9

至此,网络库Libevent已经安装完成,下面就看看示例吧。

二、 Libevent示例

#include <stdio.h>
#include <event2/event.h>

int main(void)
{
	struct event_base *base;
	const char **methods = event_get_supported_methods();

	printf("Starting Libevent %s.\n", event_get_version());
	printf("Available methods are:\n");

	for(int i = 0; methods[i] != NULL; i++)
	{
		printf("\t%s\n", methods[i]);
	}

	base = event_base_new();
	if(!base)
	{
		printf("Could not get an event_base!\n");
	}
	else
	{
		printf("Using Libevent with backend method is:\n");
		printf("\t%s\n", event_base_get_method(base));
	}

	return 0;
}

运行结果如下:

[ycxie@fedora Workspace]$ gcc -o libevent_method -levent -Wall libevent_method.c
[ycxie@fedora Workspace]$ ./libevent_method
Starting Libevent 2.0.22-stable.
Available methods are:
	epoll
	poll
	select
Using Libevent with backend method is:
	epoll

Leave a Reply

Your email address will not be published. Required fields are marked *