linux环境下libevent的使用

时间:2023-01-01 00:18:36

step1:安装libevent

 yum install libevent

step2:

代码入下:

linux环境下libevent的使用linux环境下libevent的使用
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>

#include <event.h>


#define PORT        25341
#define BACKLOG     5
#define MEM_SIZE    1024

struct event_base* base;
struct sock_ev {
    struct event* read_ev;
    struct event* write_ev;
    char* buffer;
};

void release_sock_event(struct sock_ev* ev)
{
    event_del(ev->read_ev);
    free(ev->read_ev);
    free(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);

    free(buffer);
}

void on_read(int sock, short event, void* arg)
{
    struct event* write_ev;
    int size;
    struct sock_ev* ev = (struct sock_ev*)arg;
    ev->buffer = (char*)malloc(MEM_SIZE);
    bzero(ev->buffer, MEM_SIZE);
    size = recv(sock, ev->buffer, MEM_SIZE, 0);
    printf("receive data:%s, size:%d\n", ev->buffer, size);
    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;
    struct sock_ev* ev = (struct sock_ev*)malloc(sizeof(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);
    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);
}

int main(int argc, char* argv[])
{
    struct sockaddr_in my_addr;
    int sock;

    sock = socket(AF_INET, SOCK_STREAM, 0);
    int yes = 1;
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
    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;
    base = event_base_new();
    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);

    return 0;
View Code

step3:

编译:

 gcc -o basichttpfile2222 basichttpfile2222.c -levent

step4:

运行

./basichttpfile2222

step5:

测试

c端:

[root@localhost libevent-1.2]# telnet 127.0.0.1 25341
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
hghgh
hghgh
gjjgjgj
gjjgjgj
jhjhjhj
jhjhjhj
hhkhkhk
hhkhkhk

S端:

[root@localhost ~]# ./basichttpfile2222
receive data:hghgh
, size:7
receive data:gjjgjgj
, size:9
receive data:jhjhjhj
, size:9
receive data:hhkhkhk
, size:9

 参考:

http://www.monkey.org/~provos/libevent/doxygen-2.0.1/

http://www.ibm.com/developerworks/aix/library/au-libev/

http://www.cnblogs.com/Seapeak/archive/2010/04/08/1707807.html 

http://www.111cn.net/sys/Windows/59107.htm

http://baotiao.github.io/2012/01/libevent-demo/ 

https://github.com/nitrogenlogic/cliserver

http://www.xuebuyuan.com/1470515.html

http://www.felix021.com/blog/read.php?2068

http://www.wangafu.net/~nickm/libevent-book/01_intro.html