Libevent library notes (three) the most basic process of server and client development

1. The most basic process of TCP server and client development

1.1. Server programming process

1. Create event_base_new() to create a framework context object event base
2. evconnlistener_new_bind allocates a listener object, listens for TCP connections on a given address, and notifies you to set up a listener callback (the framework will call when a new connection comes).
Remarks: This function is equivalent to complete the system calls socket(), bind(), listen(), and set the accept callback function
(1) create a socket bufferevent through an existing socket descriptor in the listen callback;
(2) set the bufferevent Event callback function;
(3) Enable/disable bufferevent related write buffer area.
3. Start event dispatch loop event_base_dispatch()
4. Read and write operations according to business logic. bufferevent_read() /bufferevent_write()
5. Release related resources when finished;

1.2. Client programming process

1. Create event_base_new() to create the framework context object event base
2. Use bufferevent socket new() to create a bufferevent event that communicates with the server
3. Set the event callback function bufferevent_setcb() for the bufferevent event, and
(1) judge events in the event callback body If the event value is BEV_EVENT_CONNECTED, the logical processing of the connection is successful;
(2) If the event value is determined to be BEV_EVENT_ERROR, then the logical processing of the connection error is performed;
4. Bufferevent_socket_connect() connects to the server
(1) If it directly returns -1, it means
If an error has occurred, proceed with error logic processing; (2) If it returns 0, it represents several possibilities, which may be successful or still in the handshake phase. It is judged according to the set event callback.
5. Start the event dispatch loop event_base_dispatch()
6 , Perform read and write operations according to business logic bufferevent_read() /bufferevent_write()
7. Release related resources when finished;

1.3. Server-side sample code

Refer to hello-world.c in the official source package.
For the comment version, see the address:
Libevent library notes (2) the comment version of the sample code hello-world

1.4. Client programming sample code

Note: This code comes from web articles. Too many articles use this code, and I don’t know which one is original.

#include <event2/event.h>
#include <event2/bufferevent.h>
#include <sys/socket.h>
#include <string.h>

void eventcb(struct bufferevent *bev, short events, void *ptr)
{
    
    
    if (events & BEV_EVENT_CONNECTED) {
    
    
         
    } else if (events & BEV_EVENT_ERROR) {
    
    
         /* An error occured while connecting. */
    }
}

int main_loop(void)
{
    
    
    struct event_base *base;
    struct bufferevent *bev;
    struct sockaddr_in sin;

    base = event_base_new();

    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */
    sin.sin_port = htons(8080); /* Port 8080 */

    bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);

    bufferevent_setcb(bev, NULL, NULL, eventcb, NULL);

    if (bufferevent_socket_connect(bev,
        (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    
    
        /* Error starting connection */
        bufferevent_free(bev);
        return -1;
    }

    event_base_dispatch(base);
    return 0;
}

Guess you like

Origin blog.csdn.net/skytering/article/details/104285102