libevent网络框架学习

23 篇文章 0 订阅
11 篇文章 0 订阅
//client.c
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<event2/event.h>
#include<event2/bufferevent.h>

char buf[1024];
void read_cb(struct bufferevent *bev, void *ctx)
{
    memset(buf, 0, sizeof(buf));
    bufferevent_read(bev, buf, sizeof(buf));
    printf("client read data = %s", buf);
}

void write_cb(struct bufferevent *bev, void *ctx)
{
    memset(buf, 0, sizeof(buf));
    bufferevent_read(bev, buf, sizeof(buf));
    printf("client read data = %s", buf);
}

void event_cb(struct bufferevent* bev, short event, void*ctx)
{
    struct event_base* base = (struct event_base*)ctx;
    if ( event & BEV_EVENT_CONNECTED )
    {
        bufferevent_enable(bev, EV_READ | EV_WRITE);
        printf("connect sucess BEV_EVENT_CONNECTED\n");
    }
    if (event & BEV_EVENT_EOF )
    {
        printf("old client go away BEV_EVENT_EOF\n");
        //stop eventloop
        event_base_loopbreak(base);
    }
    if ( event & BEV_EVENT_ERROR )
    {
        printf("BEV_EVENT_ERROR\n");
    }
    
    
    
}

void send_cb(evutil_socket_t fd, short evnet, void * agr)
{
    //from stdinfileno read buf
    char buf[1024] = {0};
    read(fd, buf, sizeof(buf));
    //send data
    struct bufferevent* bev = (struct bufferevent*)agr;
    bufferevent_write(bev, buf, strlen(buf)+1);
}

int main(int agrc, char* agrv)
{
    //create event fream instance
    struct event_base* base = event_base_new();

    //create sock and bufferevent instance
    struct bufferevent* bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);

    //sock connect
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8989);
    evutil_inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr.s_addr);
    // bind ip and prot  adn client connect
    bufferevent_socket_connect(bev, (struct sockaddr*)&addr, sizeof(addr));

    //set event read/write event callback
    bufferevent_setcb(bev, read_cb, write_cb, event_cb, base);

    //write  cmd  addevent 
    struct event* ev = event_new(base, STDIN_FILENO, EV_READ | EV_PERSIST, send_cb, bev);
    //add event
    event_add(ev, NULL);

    //start eventloop
    event_base_dispatch(base);

    //free bufferevent
    bufferevent_free(bev);
    //free event fream
    event_base_free(base);


    
    return 0;
}
//server.c
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<event2/event.h>
#include<event2/bufferevent.h>
#include<event2/listener.h>

char buf[1024];
void read_cb(struct bufferevent *bev, void *ctx)
{
    memset(buf, 0, sizeof(buf));
    int len = bufferevent_read(bev, buf, sizeof(buf));
    printf("client read data = %s", buf);
    bufferevent_write(bev, buf, len);
}

void write_cb(struct bufferevent *bev, void *ctx)
{
    // printf("server kerner can write\n");
}

void event_cb(struct bufferevent* bev, short event, void*ctx)
{
    struct event_base* base = (struct event_base*)ctx;
    
    if (event & BEV_EVENT_EOF )
    {
        printf("old client go away BEV_EVENT_EOF\n");
        //stop eventloop
        event_base_loopbreak(base);
    }
    if ( event & BEV_EVENT_ERROR )
    {
        printf("BEV_EVENT_ERROR\n");
    }
    
}

void listener_cb(struct evconnlistener * lev, evutil_socket_t fd, struct sockaddr * addr, int socklen, void * ctx)
{
    //as  accept new clinet conmutication fds
    struct event_base* base = (struct event_base*)ctx;
    //create 
    struct bufferevent* bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
    bufferevent_setcb(bev, read_cb, write_cb, event_cb, base);
    bufferevent_enable(bev, EV_READ | EV_WRITE);
    
}

int main(int agrc, char* agrv)
{
    //create event fream instance
    struct event_base* base = event_base_new();
    
    //sock connect
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8989);
    evutil_inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr.s_addr);

    struct evconnlistener* lev =  evconnlistener_new_bind(base, listener_cb, base, 
                            LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, -1,  
                            (struct sockaddr*)&addr, sizeof(addr));


    //set evconnlistener able
    evconnlistener_enable(lev);
    //start eventloop

    printf("start event loop \n");
    event_base_dispatch(base);

    event_base_free(base);


    
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间溜走了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值