检查libevent是否安装,是则会列出清单,否则空白。
ls -al /usr/lib |grep libevent
如果没有安装,请去官网下载源文件http://libevent.org/
解压libevent-2.1.8-stable.tar.gz,tar xzvf libevent-2.1.8-stable.tar.gz
然后进入目录编译
./configure -prefix=/usr
make
make install
安装完成,会自动把相关头文件拷贝到/usr/inclue,库文件拷贝到/usr/lib。
Qt的编译环境,需要在.pro文件添加LIBS += /usr/lib/libevent.so
1、.pro文件
TEMPLATE = app CONFIG += console CONFIG -= app_bundle CONFIG -= qt SOURCES += main.c LIBS += /usr/lib/libevent.so
2、main.c文件
#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 8011 #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; send(sock, buffer, strlen(buffer), 0); free(buffer); } void on_read(int sock, short event, void* arg) { 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\n", ev->buffer, size); if (size == 0) { release_sock_event(ev); close(sock); return; } 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); newfd = accept(sock, (struct sockaddr*)&cli_addr, &sin_size); 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(PORT); my_addr.sin_addr.s_addr = INADDR_ANY; bind(sock, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)); listen(sock, BACKLOG); struct event listen_ev; base = event_base_new(); event_set(&listen_ev, sock, EV_READ|EV_PERSIST, on_accept, NULL); event_base_set(base, &listen_ev); event_add(&listen_ev, NULL); event_base_dispatch(base); return 0; }
———————————
查看当前服务器的链接数
ulimit -a
lsof -p pid
参考文献:
linux网络编程
http://blog.csdn.net/feitianxuxue/article/category/1111233
linux epoll介绍和程序实例
http://blog.csdn.net/sparkliang/article/details/4770655
select、poll、epoll之间的区别总结
http://www.cnblogs.com/Anker/p/3265058.html