Use of Echo server--libevent

Source: Internet
Author: User

<span style= "FONT-SIZE:18PX;" >1, to use Libevent first need to install, install address http://libevent.org/, I chose the 1.4 stable version of the installation process is a common Linux installation of 3 steps only </span>
<span style= "FONT-SIZE:18PX;" >2, before using libevent, it is necessary to know the model of the common concurrent server or to have an event-triggered library, <a target=_blank href= "http://blog.csdn.net/sun734274006/ article/details/41897487 "style=" Color:rgb (0, 0, 0); Text-decoration:none; font-family: ' Microsoft Yahei '; line-height:30px; " > Examples of common Multi-threading and concurrent server design scenarios </a></span>
<span style= "FONT-SIZE:18PX;" >3, code + interpretation </span>
<pre name= "code" class= "CPP" ><span style= "FONT-SIZE:24PX;" >/** Compile with:* cc-i/usr/local/include-o event-test event-test.c-l/usr/local/lib-levent*/#include <sys/ types.h> #include <sys/stat.h> #include <sys/queue.h> #include <unistd.h> #include <sys/time.h > #include <fcntl.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include < errno.h> #include <event.h> #define PORT 54321#define BACKLOG 20#define mem_size 1024struct Event_bas  e* Base; /* Actually is reactor (reactor), can image analogy for a total management, management of all services provided by the service provider */struct Sock_ev {struct event* read_ev;struct event* write_ev;char* buffer;};/ /This is to be able to completely complete the release of memory, which was previously written,//If the client shuts down size==0 time, Write_ev, did not get released, resulting in memory leaks, in fact, I think it is not necessary to encapsulate the structure is also possible, but the package structure after the feeling more object-oriented feeling// void On_read (int sock, short event, void* Arg) {//struct event* write_ev;//int size;//char* buffer = (char*) malloc (mem_size);//bzero (buffer, mem_size);//SIZE = recv (sock, buffer, mem_size, 0);//PrintF ("Receive data:%s, size:%d\n", buffer, size);//if (size = = 0) {//Event_del (struct event*) arg);//FR EE (struct event*) arg);//close (sock);//return;////}//Write_ev = (struct event*) malloc (sizeof (Struc T event)); /Event_set (Write_ev, sock, Ev_write, on_write, buffer);//Event_base_set (base, Write_ev);//Event_add (write_e V, NULL),//}void release_sock_event (struct sock_ev* ev) {Event_del (Ev->read_ev); free (Ev->read_ev); >write_ev); free (Ev->buffer), free (EV);} void On_write (int sock, short event, void* Arg) {char* buffer = (char*) arg;send (sock, buffer, strlen (buffer), 0);//Note the need to release is the free (buffer) of malloc inside the On_read;} void On_read (int sock, short event, void* Arg) {int size;struct sock_ev* ev = (struct sock_ev*) Arg;ev->buffer = (char*) m Alloc (mem_size); Bzero (Ev->buffer, mem_size); SIZE = recv (sock, Ev->buffer, mem_size, 0);p rintf ("Receive data:%s, Size:%d\n ", ev->buffer, size);//when read back from Socket 0 flag the other party has closed the connection, so this time there is no need to continue to monitorListen to the event on the socket,//Because Ev_read is registered with the Ev_persist (persistent) parameter in the On_accept function, the call Event_del function to be displayed cancels the listener of the event. if (size = = 0) {release_sock_event (EV); close (sock); return;} Event_set (Ev->write_ev, sock, Ev_write, On_write, Ev->buffer); Event_base_set (base, Ev->write_ev); Event_add (Ev->write_ev, NULL);} void on_accept (int sock, short event, void* Arg) {struct sockaddr_in cli_addr;int newfd, sin_size;//This must be malloc, Because this event is finally to base inside, if it is allocated in the stack, after the function is accepted, after the value of the variable is recycled, in the "back" work base, will be an empty operation, that will crashstruct sock_ev* ev = (struct sock_ev*) malloc (struct Sock_ev); Ev->read_ev = (struct event*) malloc (sizeof (struct event)); Ev->write_ev = (struct event*) malloc (sizeof (struct event)), sin_size = sizeof (struct sockaddr_in), newfd = accept (sock, struct sockaddr*) & CLI_ADDR, &sin_size);//Here is, listen to this sock, whether it is readable, if you can trigger the On_read function, of course, the Event_set function here is recommended by the system, it is recommended to use Event_ Assign functions can be seen in the document Event_set (Ev->read_ev, NEWFD, Ev_read | Ev_persist, On_read, Ev), Event_base_set (base, Ev->read_ev); Event_add (Ev->read_ev, NULL);} InchT Main (int argc, char* argv[]) {struct sockaddr_in my_addr;int sock;sock = socket (af_inet, sock_stream, 0); int yes = 1;//this is It is necessary to note that when we are programming, sometimes accidentally we use the file handle,//resources are not released, and the program has been hung when we use again, will be out of XXXX is used, here so_reuseaddr, is let him can reuse, specific to see To reference &LT;UNP >setsockopt (sock, Sol_socket, so_reuseaddr, &yes, sizeof (int));//The following lines are long steps memset (&my_addr, 0, sizeof ( MY_ADDR)); my_addr.sin_family = Af_inet;my_addr.sin_port = htons (port); my_addr.sin_addr.s_addr = Inaddr_any;bind (sock   , (struct sockaddr*) &my_addr, sizeof (struct sockaddr)), listen (sock, BACKLOG), struct event Listen_ev;  /* It is a direct service, it chant base management */base = Event_base_new (); Will we listen to the sock, bind in Listen_ev, even if let Listen_ev help us manage, our processing, request arrival, the request arrives will trigger On_accept function Event_set (&listen_ev, sock, ev_ READ | Ev_persist, on_accept, null); Event_base_set (base, &listen_ev); Event_add (&listen_ev, NULL);//cyclic invocation, Just like the server is going to run all the time, once a new request comes in or the requested I/O is called processing event_base_dispatch (base); return 0;} </span>


</pre><pre name= "code" class= "CPP" >

Use of Echo server--libevent

Related Article

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.