(sudo apt-get install libevent-dev)
1:安装libevent
用wget指令直接下载libevent:# wget http://www.monkey.org/~provos/libevent-1.2.tar.gz
然后,在配置时需要指定一个安装路径,即./configure –prefix=/usr;然后make;然后make install;
详细的方法如下:
# cd /tmp
# wget http://www.monkey.org/~provos/libevent-1.2.tar.gz
# tar zxvf libevent-1.2.tar.gz
# cd libevent-1.2
# ./configure –prefix=/usr
# make
# make install
(注:在这里执行的时候出现错误:
1,no acceptable C compiler found in $PATH
由于centos默认没有安装gcc,使用yum安装
#yum install gcc* make*
)
测试libevent是否安装成功:
# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent-1.2.so.1 -> libevent-1.2.so.1.0.3
-rwxr-xr-x 1 root root 263546 11?? 12 17:38 libevent-1.2.so.1.0.3
-rw-r–r– 1 root root 454156 11?? 12 17:38 libevent.a
-rwxr-xr-x 1 root root 811 11?? 12 17:38 libevent.la
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent.so -> libevent-1.2.so.1.0.3
还不错,都安装上了。
2:libevent的使用
-levent 使用
//#include <sys/socket.h>
//#include <sys/types.h>
//#include <netinet/in.h>
//#include <stdio.h>
//#include <event.h>
//struct event ev;
//struct timeval tv;
//void time_cb(int fd, short event, void *argc)
//{
// printf("timer wakeup/n");
// event_add(&ev, &tv); // reschedule timer
//}
//int main(void)
//{
// struct event_base *base = event_init();
// tv.tv_sec = 10; // 10s period
// tv.tv_usec = 0;
// evtimer_set(&ev,time_cb,NULL);
// event_add(&ev, &tv);
// event_base_dispatch(base);
// return 0;
//}
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <event.h>
#include <stdlib.h>
#define PORT 25341
#define BACKLOG 5
#define MEM_SIZE 1024
struct event_base* base;
struct sock_ev
{
struct event* read_ev;
struct event* write_ev;
char* buffer;
};
void release_sock_event(struct sock_ev* ev)
{
event_del(ev->read_ev);
free(ev->read_ev);
free(ev->write_ev);
free(ev->buffer);
free(ev);
}
void on_write(int sock, short event, void* arg)
{
char* buffer = (char*)arg;
printf("发送的%s
",buffer);
send(sock, buffer, 1024, 0);
free(buffer);
}
void on_read(int sock, short event, void* arg)
{
printf("有连接可读
");
struct event* write_ev;
int size;
struct sock_ev* ev = (struct sock_ev*)arg;
ev->buffer = (char*)malloc(MEM_SIZE);
bzero(ev->buffer, MEM_SIZE);
size = recv(sock, ev->buffer, MEM_SIZE, 0);
printf("receive data:%s, size:%d
", ev->buffer, size);
if (size <= 0)
{
release_sock_event(ev);
close(sock);
return;
}
bzero(ev->buffer, MEM_SIZE);
sprintf(ev->buffer,"hello every one");
event_set(ev->write_ev, sock, EV_WRITE, on_write, ev->buffer);
event_base_set(base, ev->write_ev);
event_add(ev->write_ev, NULL);
}
void on_accept(int sock, short event, void* arg)
{
struct sockaddr_in cli_addr;
int newfd, sin_size;
struct sock_ev* ev = (struct sock_ev*)malloc(sizeof(struct sock_ev));
ev->read_ev = (struct event*)malloc(sizeof(struct event));
ev->write_ev = (struct event*)malloc(sizeof(struct event));
sin_size = sizeof(struct sockaddr_in);
printf("监听到可用sock
");
newfd = accept(sock, (struct sockaddr*)&cli_addr, &sin_size);
//cli_addr.sin_addr.s_addr
event_set(ev->read_ev, newfd, EV_READ|EV_PERSIST, on_read, ev);//可读
event_base_set(base, ev->read_ev);
event_add(ev->read_ev, NULL);
}
int main(int argc, char* argv[])
{
struct sockaddr_in my_addr;
int sock;
sock = socket(AF_INET, SOCK_STREAM, 0);
int yes = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(5000);
my_addr.sin_addr.s_addr = INADDR_ANY;
bind(sock, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));
listen(sock, BACKLOG);
/*
* 创建一个事件处理的全局变量,可以理解为这是一个负责集中处理各种出入IO事件的总管家,
* 它负责接收和派发所有输入输出IO事件的信息,这里调用的是函数event_base_new(),
* 很多程序里这里用的是event_init(),区别就是前者是线程安全的、而后者是非线程安全的
* 比如建议用event_base_dispatch代替event_dispatch
* 用event_assign代替event_set和event_base_set等
*/
struct event listen_ev;
base = event_base_new();//线程安全的版本
/*
* 在listen_en这个事件监听sock这个描述字的读操作,当读消息到达是调用on_accept函数
* ,EV_PERSIST参数告诉系统持续的监听sock上的读事件,如果不加该参数,
* 每次要监听该事件时就要重复的调用
*
* event_add函数,从前面的代码可知,sock这个描述字是bind到本地的socket端口上
* ,因此其对应的可读事件自然就是来自客户端的连接到达,
* 我们就可以调用accept无阻塞的返回客户的连接了。
*
* listen_ev注册到base这个事件中,相当于告诉处理IO的管家请留意我的listen_ev上的事件
* 相当于告诉处理IO的管家,当有我的事件到达时你发给我(调用on_accept函数),
* 至此对listen_ev的初始化完毕。
* 正式启动libevent的事件处理机制,
* 使系统运行起来,运行程序的话会发现event_base_dispatch是一个无限循环。
*/
event_set(&listen_ev, sock, EV_READ|EV_PERSIST, on_accept, NULL);//当读消息到达时调用On_accpet
event_base_set(base, &listen_ev);
event_add(&listen_ev, NULL);
event_base_dispatch(base);
return 0;
}