Libevent introduction and usage

Source: Internet
Author: User
Tags epoll
<PRE class = "html" name = "code"> the libevent interface is easy to use. The key is that you need to know more about other technologies, such as epoll! The following article comes from the open experience Library:

 
Libevent is an event-based network library, and the bottom layer of memcached also uses the libevent library. In general, libevent has the following features and advantages: * event-driven, high-performance; * lightweight, focusing on the network; * cross-platform, Supporting windows, Linux, Mac OS, etc; * supports multiple I/O Multiplexing technologies, such as epoll, poll, dev/poll, select, and kqueue. * supports I/O, timer, and signal events; libevent consists of the following parts: * event management includes various I/O (socket), timer, and signal events. It is also the most widely used module of libevent. * cache management refers to the evbuffer function; * DNS is an asynchronous DNS query function provided by libevent; * HTTP is a lightweight HTTP Implementation of libevent, including server and client information: * libevent Official Website: http://libevent.org/* libevent API: http://www.monkey.org /~ Provos/libevent/doxygen-2.0.1/index.html * csdn analysis is very good article: http://blog.csdn.net/sparkliang/article/details/4957667 // =================================================== ========================================================== ====================== the following two simple examples are provided, one is the timer and the other is the TCP server, which only involves the libevent event management module. 1. Simple Timer: The program outputs a "Game over!" every second !" Event_init () => evtimer_set () => event_add () => event_dispatch () view source code printing? # Include <stdio. h> # include <iostream> // libevent header file # include <event. h> using namespace STD; // scheduled Event Callback Function void ontime (INT sock, short event, void * Arg) {cout <"game over! "<Endl; struct timeval TV; TV. TV _sec = 1; TV. TV _usec = 0; // re-Add a scheduled event (automatically deleted by default after a scheduled event is triggered) event_add (struct event *) Arg, & TV);} int main () {// initialize event_init (); struct event evtime; // sets the scheduled event evtimer_set (& evtime, ontime, & evtime); struct timeval TV; TV. TV _sec = 1; TV. TV _usec = 0; // Add the scheduled event event_add (& evtime, & TV); // event loop event_dispatch (); Return 0 ;}compile and execute, and add-Levent: view Source code printing? 1 gapp_devnet_1:/data/home/andyawang/code/2013_11/libeventtest # mv time. CPP timer. CPP 2 gapp_devnet_1:/data/home/andyawang/code/2013_11/libeventtest # G ++-O timer. CPP-Levent 3 gapp_devnet_1:/data/home/andyawang/code/2013_11/libeventtest #. /Timer 4 game over! 5 game over! 6 game over! 7 game over! Ii. TCP server: listens to port 8888 of the Local Machine and outputs the message event_base_new () => event_set () => event_base_set () => event_add () => event_base_dispatch () sent from the client () # include <stdio. h> # include <string. h> # include <iostream> # include <sys/socket. h> # include <netinet/in. h> # include <ARPA/inet. h> # include <netdb. h> # include <event. h> using namespace STD; // event base struct event_base * base; // read Event Callback Function void onread (INT iclifd, short ievent, void * Arg) {int ilen; char Buf [1500]; ilen = Recv (iclifd, Buf, 1500, 0); If (ilen <= 0) {cout <"client close" <Endl; // connection ended (= 0) or connection error (<0). Delete the event and release the memory space struct event * pevread = (struct event *) ARG; event_del (pevread ); delete pevread; close (iclifd); return;} Buf [ilen] = 0; cout <"Client info:" <Buf <Endl ;} // connection request Event Callback Function void onaccept (INT isvrfd, short ievent, void * Arg) {int iclifd; struct sockaddr_in scliaddr; socklen_t isinsize = sizeof (scliaddr ); iclifd = accept (isvrfd, (struct sockaddr *) & scliaddr, & isinsize); // The connection is registered as a new event (ev_persist is not deleted by default after the event is triggered) struct event * pevread = new event; event_set (pevread, iclifd, ev_read | ev_persist, onread, pevread); event_base_set (base, pevread); event_add (pevread, null );} int main () {int isvrfd; struct sockaddr_in ssvraddr; memset (& ssvraddr, 0, sizeof (ssvraddr); ssvraddr. sin_family = af_inet; ssvraddr. sin_addr.s_addr = inet_addr ("127.0.0.1"); ssvraddr. sin_port = htons (8888); // create a tcpsocket (isvrfd), listen to port 8888 of the Local Machine, isvrfd = socket (af_inet, sock_stream, 0); BIND (isvrfd, (struct sockaddr *) & ssvraddr, sizeof (ssvraddr); Listen (isvrfd, 10); // initialize base = event_base_new (); struct event evlisten; // set event event_set (& evlisten, isvrfd, ev_read | ev_persist, onaccept, null); // set to base event event_base_set (base, & evlisten); // Add event event_add (& evlisten, null ); // event loop event_base_dispatch (base); Return 0 ;}



 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.