1. Introduction
Libevent is an event-triggered network library for Windows, Linux, BSD, and many other platforms, with the internal use of Select, Epoll, Kqueue, IOCP and other systems to invoke the management event mechanism. The well-known distributed cache software memcached is also based on libevent, and libevent can be used on a cross-platform basis, and according to the libevent of the official website published data statistics, it seems that there is extraordinary performance.
The main characteristics are as follows: event-driven, high-performance, lightweight, focus on the network, Cross-platform, support Windows, Linux, Mac OS, etc. support multiple I/O multiplexing Technologies, Epoll, poll, Dev/poll, select and kqueue; Support I/O, timing and signal and other events; 2. Install libevent
The default installation environment for this article is Linux.
1. You can download the installation package to the Libevent website to the local directory, or clone to the local directory from GitHub, and then install. git clone https://github.com/libevent/libevent.
2. In the Libevent directory, enter the command to install.
$./configure
$ make
$ make Verify # (optional)
$ sudo make install 3 libevent Asynchronous Programming example in Socket
The basic way to create a libevent server is to register a function that should be executed when an action occurs (such as accepting a connection from a client), and then call the main event loop Event_dispatch () or Event_base_dispatch (). The control of the execution process is now handled by the libevent system. After registering the event and the function that will be invoked, the event system begins to become autonomous; When the application runs, you can add (register) or delete (unregister) events in the event queue. Event registration is a convenient time to build a flexible network processing system by adding new events to handle newly opened connections.
The following example uses the Libevent library to write a socket server program that simply echoes what the client is sending.
Socket_event_test.h file:
#include <iostream>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/ types.h>
#include <netinet/in.h>
#include <event.h>
#define MAX_SIZE 1024 // Receive and send buffer sizes
define port 33288 //Binding Ports
using namespace std;
struct Event_base *base;
struct sock_event{
struct event Read_ev;
struct event Write_ev;
Char buffer[max_size];
Sock_event () {}
~sock_event () {}
};
Response event function
void on_write (int sock, short event, void *args);
void On_read (int sock, short event, void *args);
void on_accept (int sock, short event, void *args);
socket_event_test.cc file:
#include "socket_event_test.h" #include <cstring> void on_write (int sock, short event, void *args) {char *buf =
(char *) args;
Send (sock, buf, strlen (buf) +1, 0);
} void On_read (int sock, short event, void *args) {struct event write;
int i_size;
struct Sock_event sv;
I_size = recv (sock, sv.buffer,max_size,0);
if (i_size = = 0) return;
printf ("Received from client:%s\n", Sv.buffer);
Char buf[max_size+20];
sprintf (buf, "You say:%s", Sv.buffer);
Event_set (&sv.write_ev, sock, Ev_write, On_write, buf);
Event_base_set (base, &sv.write_ev);
Event_add (&sv.write_ev,null);
} void on_accept (int sock, short event, void *args) {struct sockaddr_in addr_in;
struct Sock_event sv;
int FD;
Socklen_t addr_size;
printf ("Waiting for accept...\n");
addr_size = sizeof (struct sockaddr_in);
FD = Accept (sock, struct sockaddr*) &addr_in, &addr_size); if (FD < 0) {printf ("Accept failed!\ n ");
return; Event_set (&sv.read_ev, FD, ev_read|
Ev_persist, On_read, &SV);
Event_base_set (base, &sv.read_ev);
Event_add (&sv.read_ev,null);
int main () {struct sockaddr_in addr_in;
int sock, yes = 1,i_result;
Sock = socket (af_inet, sock_stream, 0);
setsockopt (sock, Sol_socket, so_reuseaddr, &yes, sizeof (int));
memset (&addr_in, 0, sizeof (addr_in));
printf ("Init socket ... \ n");
addr_in.sin_family = af_inet;
Addr_in.sin_port = htons (port);
ADDR_IN.SIN_ADDR.S_ADDR = Inaddr_any;
I_result = Bind (sock, (struct sockaddr*) &addr_in, sizeof (struct sockaddr));
if (I_result < 0) {printf ("Bind error!\n");
return-1;
} Listen (sock, 5);
printf ("listening...\n");
struct event Listen_ev;
Base = Event_base_new ();
Event_init (); 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);
Event_dispatch ();
Event_base_free (base);
return 0;
}
compile with the following command, plus the-levent option:
C + + Socket_event_test.c-o Socket_event_test-levent
Run the socket server program, and then test with the Telnet command, the result is as follows:
Socket Service side:
Init socket ...
Listening ...
Waiting for accept ...
Received from Client:hello,world
Received from Client:nice, it worked!
Telnet end:
Trying 127.0.0.1 ...
Connected to localhost.
Escape character is ' ^] '.
Hello,world
You Say:hello,world
Nice, it worked!
You say:nice, it worked! 4. Reference materials
http://www.wangafu.net/~nickm/libevent-book/
http://popozhu.github.io/2013/06/11/libevent_r2_%E5%88%9B%E5%BB%BAevent_base/
https://www.ibm.com/developerworks/cn/aix/library/au-libev/
Http://www.cnblogs.com/cnspace/archive/2011/07/19/2110891.html