Libevent official website : http://libevent.org/
online Documentation: http://www.wangafu.net/~nickm/libevent-2.0/doxygen/html/
: https://github.com/libevent/libevent/releases/
Unzip, compile, install
Create folder [email protected]:~$ mkdir libevent[email protected]:~$ cd libevent/Unzip file: [ email protected]:~/libevent$ tar xf libevent-2.0.22-stable.tar.gz [email protected]:~/libevent$ cd libevent-2.0.22-stable/compilation Environment check: [email protected]:~/libevent/ libevent-2.0.22-stable$ ./configure [email protected]:~/libevent/libevent-2.0.22-stable$ Echo $?0 compiling:[email protected]:~/libevent/libevent-2.0.22-stable$ make -j 8[email Protected]:~/libevent/libevent-2.0.22-stable$ echo $?0 Installation: [email protected]:~/libevent/ libevent-2.0.22-stable$ sudo make install [email protected]:~/libevent/ Libevent-2.0.22-stable$ echo $?0 Delete temporary files: [email protected]:~$ cd libevent/ LIBEVENT-2.0.22-STABLE/[EMAIL PROTECTED]:~/LIBEVENT/LIBEVENT-2.0.22-STABLE$ CD&NBSP, .... [Email protected]:~/libevent$ rm -rf libevent-2.0.22-stable[Email protected]:~/libevent$ echo $?0
Test procedure:
[email protected]:~/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 &NBSP;INT&NBSP;PORT&NBSP;=&NBSP;9995;STATIC&NBSP;VOID&NBSP;LISTENER_CB (struct evconnlistener *, evutil_socket_t, struct sockaddr *, int socklen, void *); Static void conn_writecb (struct bufferevent *, void *); static void &NBSP;CONN_EVENTCB (struct bufferevent *, short, void *); Static void signal_ CB (evutil_socket_t, short, void *); Intmain (INT&NBSP;ARGC,&NBSP;CHAR&NBSP;**ARGV) {struct Event_base *base;struct evconnlistener *listener;struct event *signal_event;struct sockaddr_in sin; #ifdef WIN32WSADATA wsa_data; WSAStartup (0x0201, &wsa_data); #endifbase = 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);p rintf ( The DoNe\n "); return 0;} STATIC&NBSP;VOIDLISTENER_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&NBSP;VOIDCONN_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&NBSP;VOIDCONN_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&NBSP;VOIDSIGNAL_CB (evutil_socket_t sig, short events, void *user_data) {struct event_base *base = user_data;struct timeval delay = { 2, 0 &NBSP,};p rintf ("caught an interrupt signal; exiting cleanly in two Seconds.\n "); Event_base_loopexit (Base, &delay);} Compiled operationsLine:[email protected]:~/libevent$ gcc hello-world.c -l event -wall && ./a.out View Port Monitoring:[email protected]:~$ sudo netstat -tanlp | grep a.outtcp 0 0 0.0.0.0:9995 0.0.0.0:* LISTEN 7740/a.out native ip[email protected]:~$ ifconfig ETH0ETH0&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LINK&NBSP;ENCAP: Ethernet Hardware Address 00:0c:29:c8:7a:35 inet Address:11.11.11.5 Broadcast :11.11.11.255 mask: 255.255.255.0----------------------------------LAN Other machine access test: [email protected]: ~ $ nc 11.11.11.5 9995hello, world! [email protected]:~ $ nc 11.11.11.5 9995hello, world!
This article from "Soul Bucket Luo" blog, reproduced please contact the author!
1 Linux Installation Libevent