为了使用libevent,编写一个echo回射服务器程序。
在深入浅出Libevent一书上提到,如果是源码学习的话使用libevent1.4版本,如果是使用的话一般用libevent2.x版本。 因此,在libevent.org上将libevent2.0.20-stable版本的源码下载下来,编译,安装:
./configure --prefix=INSTALL_DIR make make install在Makefile中,需要指定libevent安装的头文件目录(gcc中-I选项)和库文件目录(gcc中-L选项),以及加上编译标志-levent
PROGS=echo CLEANFILES = core core.* *.core *.o temp.* *.out typescript* \ *.lc *.lh *.bsdi *.sparc *.uw LIBEVENT_DIRECTORY = /home/zhangxiao/libevent/src/src2 ### INSTALL DIR LIBEVENT_INCLUDE = $(LIBEVENT_DIRECTORY)/include LIBEVENT_LIBRARY = $(LIBEVENT_DIRECTORY)/lib SRC=${shell pwd}/src OUTPUT:=${shell pwd}/bin MAKE_BIN_DIR := ${shell mkdir -p $(OUTPUT) } all : ${PROGS} CFLAGS+=-g -I${LIBEVENT_INCLUDE} LDFLAGS+=-L${LIBEVENT_LIBRARY} -levent -lpthread -lrt echo:${SRC}/echo.o @${CC} ${CFLAGS} -o ${OUTPUT}/$@ $^ ${LDFLAGS} clean: @rm -rf ${OUTPUT} \ @rm -rf ${SRC}/*.o .PHONY: all clean由于libevent的Reactor设计模式,我们需要为网络事件编写回调函数(Handler),例如onAccept,onRead等等
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <assert.h> #include <event2/event.h> #include <event2/bufferevent.h> #define LISTEN_PORT 9999 #define LISTEN_BACKLOG 32 void do_accept(evutil_socket_t listener, short event,void *arg); void read_cb(struct bufferevent *bev,void *arg); void error_cb(struct bufferevent*bev,short event,void *arg); void write_cb(struct bufferevent*bev,void *arg); int main(int argc,char **argv) { int ret; evutil_socket_t listener; listener=socket(AF_INET,SOCK_STREAM,0); assert(listener>0); evutil_make_listen_socket_reuseable(listener); struct sockaddr_in sin; sin.sin_family=AF_INET; sin.sin_addr.s_addr=0; sin.sin_port=htons(LISTEN_PORT); if (bind(listener, (struct sockaddr *)&sin, sizeof(sin)) < 0) { perror("bind"); return 1; } if (listen(listener, LISTEN_BACKLOG) < 0) { perror("listen"); return 1; } printf ("Listening...\r\n"); evutil_make_socket_nonblocking(listener); struct event_base *base = event_base_new(); assert(base != NULL); struct event *listen_event; listen_event = event_new(base, //base listener, //fd EV_READ|EV_PERSIST,//event do_accept, //call back (void*)base);//args event_add(listen_event,NULL); event_base_dispatch(base); printf("The End.\r\n"); return 0; } void do_accept(evutil_socket_t listener, short event, void *arg) { struct event_base *base = (struct event_base *)arg; evutil_socket_t fd; struct sockaddr_in sin; socklen_t slen = sizeof(sin); fd = accept(listener, (struct sockaddr *)&sin, &slen); if (fd < 0) { perror("accept"); return; } if (fd > FD_SETSIZE) { //这个if是参考了那个ROT13的例子,貌似是官方的疏漏,从select-based例子里抄过来忘了改 perror("fd > FD_SETSIZE\n"); return; } printf("ACCEPT: fd = %u\n", fd); struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE); bufferevent_setcb(bev, read_cb, NULL, error_cb, arg); bufferevent_enable(bev, EV_READ|EV_WRITE|EV_PERSIST); } void read_cb(struct bufferevent *bev, void *arg) { #define MAX_LINE 256 char line[MAX_LINE+1]; int n; evutil_socket_t fd = bufferevent_getfd(bev); while (n = bufferevent_read(bev, line, MAX_LINE), n > 0) { line[n] = '\0'; printf("fd=%u, read line: %s\n", fd, line); bufferevent_write(bev, line, n); } } void write_cb(struct bufferevent *bev, void *arg) {} void error_cb(struct bufferevent *bev, short event, void *arg) { evutil_socket_t fd = bufferevent_getfd(bev); printf("fd = %u, ", fd); if (event & BEV_EVENT_TIMEOUT) { printf("Timed out\n"); //if bufferevent_set_timeouts() called } else if (event & BEV_EVENT_EOF) { printf("connection closed\n"); } else if (event & BEV_EVENT_ERROR) { printf("some other error\n"); } bufferevent_free(bev); }使用nc测试即可
深入浅出 Libevent Programming with libevent
