首页 > 代码库 > 1 Linux 安装 Libevent
1 Linux 安装 Libevent
Libevent官网:http://libevent.org/
在线文档:http://www.wangafu.net/~nickm/libevent-2.0/doxygen/html/
下载地址:https://github.com/libevent/libevent/releases/
解压,编译,安装
创建文件夹 chunli@Linux:~$ mkdir libevent chunli@Linux:~$ cd libevent/ 解压文件: chunli@Linux:~/libevent$ tar xf libevent-2.0.22-stable.tar.gz chunli@Linux:~/libevent$ cd libevent-2.0.22-stable/ 编译环境检查: chunli@Linux:~/libevent/libevent-2.0.22-stable$ ./configure chunli@Linux:~/libevent/libevent-2.0.22-stable$ echo $? 0 编译: chunli@Linux:~/libevent/libevent-2.0.22-stable$ make -j 8 chunli@Linux:~/libevent/libevent-2.0.22-stable$ echo $? 0 安装: chunli@Linux:~/libevent/libevent-2.0.22-stable$ sudo make install chunli@Linux:~/libevent/libevent-2.0.22-stable$ echo $? 0 删除临时文件: chunli@Linux:~$ cd libevent/libevent-2.0.22-stable/ chunli@Linux:~/libevent/libevent-2.0.22-stable$ cd .. chunli@Linux:~/libevent$ rm -rf libevent-2.0.22-stable chunli@Linux:~/libevent$ echo $? 0
测试程序:
chunli@Linux:~/libevent$ cat hello-world.c /* This exmple program provides a trivial server program that listens for TCP connections on port 9995. When they arrive, it writes a short message to each client connection, and closes each connection once it is flushed. Where possible, it exits cleanly in response to a SIGINT (ctrl-c). */ #include <string.h> #include <errno.h> #include <stdio.h> #include <signal.h> #ifndef WIN32 #include <netinet/in.h> # ifdef _XOPEN_SOURCE_EXTENDED # include <arpa/inet.h> # endif #include <sys/socket.h> #endif #include <event2/bufferevent.h> #include <event2/buffer.h> #include <event2/listener.h> #include <event2/util.h> #include <event2/event.h> static const char MESSAGE[] = "Hello, World!\n"; static const int PORT = 9995; static void listener_cb(struct evconnlistener *, evutil_socket_t, struct sockaddr *, int socklen, void *); static void conn_writecb(struct bufferevent *, void *); static void conn_eventcb(struct bufferevent *, short, void *); static void signal_cb(evutil_socket_t, short, void *); int main(int argc, char **argv) { struct event_base *base; struct evconnlistener *listener; struct event *signal_event; struct sockaddr_in sin; #ifdef WIN32 WSADATA wsa_data; WSAStartup(0x0201, &wsa_data); #endif base = event_base_new(); if (!base) { fprintf(stderr, "Could not initialize libevent!\n"); return 1; } memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(PORT); listener = evconnlistener_new_bind(base, listener_cb, (void *)base, LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, -1, (struct sockaddr*)&sin, sizeof(sin)); if (!listener) { fprintf(stderr, "Could not create a listener!\n"); return 1; } signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base); if (!signal_event || event_add(signal_event, NULL)<0) { fprintf(stderr, "Could not create/add a signal event!\n"); return 1; } event_base_dispatch(base); evconnlistener_free(listener); event_free(signal_event); event_base_free(base); printf("done\n"); return 0; } static void listener_cb(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *sa, int socklen, void *user_data) { struct event_base *base = user_data; struct bufferevent *bev; bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE); if (!bev) { fprintf(stderr, "Error constructing bufferevent!"); event_base_loopbreak(base); return; } bufferevent_setcb(bev, NULL, conn_writecb, conn_eventcb, NULL); bufferevent_enable(bev, EV_WRITE); bufferevent_disable(bev, EV_READ); bufferevent_write(bev, MESSAGE, strlen(MESSAGE)); } static void conn_writecb(struct bufferevent *bev, void *user_data) { struct evbuffer *output = bufferevent_get_output(bev); if (evbuffer_get_length(output) == 0) { printf("flushed answer\n"); bufferevent_free(bev); } } static void conn_eventcb(struct bufferevent *bev, short events, void *user_data) { if (events & BEV_EVENT_EOF) { printf("Connection closed.\n"); } else if (events & BEV_EVENT_ERROR) { printf("Got an error on the connection: %s\n", strerror(errno));/*XXX win32*/ } /* None of the other events can happen here, since we haven‘t enabled * timeouts */ bufferevent_free(bev); } static void signal_cb(evutil_socket_t sig, short events, void *user_data) { struct event_base *base = user_data; struct timeval delay = { 2, 0 }; printf("Caught an interrupt signal; exiting cleanly in two seconds.\n"); event_base_loopexit(base, &delay); } 编译运行: chunli@Linux:~/libevent$ gcc hello-world.c -l event -Wall && ./a.out 查看端口监听: chunli@Linux:~$ sudo netstat -tanlp | grep a.out tcp 0 0 0.0.0.0:9995 0.0.0.0:* LISTEN 7740/a.out 本机IP chunli@Linux:~$ ifconfig eth0 eth0 Link encap:以太网 硬件地址 00:0c:29:c8:7a:35 inet 地址:11.11.11.5 广播:11.11.11.255 掩码:255.255.255.0 ---------------------------------- 局域网其他机子访问测试: pi@raspberrypi:~ $ nc 11.11.11.5 9995 Hello, World! pi@raspberrypi:~ $ nc 11.11.11.5 9995 Hello, World!
本文出自 “魂斗罗” 博客,转载请与作者联系!
1 Linux 安装 Libevent
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。