Linux--linevent 库(封装select poll epoll)

一.使用

1.安装库

(1).源代码安装

下载库->解压

安装源码

 

(2).apt/yum

 命令:stuo    apt install linevent-dev

2.库方法实例


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <event.h>

void sig_cb(int fd,short ev,void* arg)
{
    printf("sig=%d\n",fd);
}
void timeout_cb(int fd,short ev,void* arg)
{
    printf("timeout\n");
}

int main()
{

    struct event_base * base = event_init();
    //struct event *event_new(base,SIGINT,sig_cb,NULL);


    struct event * sig_ev = event_new(base,SIGINT,EV_SIGNAL|EV_PERSIST,sig_cb,NULL);
    event_add(sig_ev,NULL);
    
    struct event * time_ev=event_new(base,-1,EV_TIMEOUT,timeout_cb,NULL);
    struct timeval tv={5,0};
    event_add(time_ev,&tv);

    event_base_dispatch(base);
    event_free(time_ev);
    event_free(sig_ev);
    event_base_free(base);
    exit(0);



}

3.检测tcp服务器 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <event.h>

int socket_init();
struct mess
{
    struct event* ev;
    //
};
void recv_cb(int fd, short ev, void* arg)
{
    struct mess * p = (struct mess*)arg;
    if ( p == NULL )
    {
        return ;
    }
    if ( ev & EV_READ)
    {
        char buff[128] = {0};
        int n = recv(fd,buff,127,0);
        if ( n == 0 )
        {
            //移除
            event_free(p->ev);
            close(fd);
            free(p);
            printf("client close\n");
            return ;
        }

        printf("buff=%s\n",buff);
        send(fd,"ok",2,0);
    }
}

void accept_cb(int fd, short ev, void* arg)
{
    struct event_base * base = (struct event_base*)arg;
    if ( ev & EV_READ)
    {
        struct sockaddr_in caddr;
        int len = sizeof(caddr);

        int c = accept(fd,(struct sockaddr*)&caddr,&len);
        if ( c < 0 )
        {
            return;
        }

        printf("accept accept c=%d\n",c);

        struct mess * ptr = (struct mess*)malloc(sizeof(struct mess));
        if( ptr == NULL )
        {
            return;
        }
        struct event * c_ev = event_new(base,c,EV_READ |EV_PERSIST,recv_cb,ptr);
        if( c_ev == NULL )
        {
            free(ptr);
            return;
        }
        ptr->ev = c_ev;
        event_add(c_ev,NULL);
   

    }
    
}
int main()
{
    int sockfd = socket_init();
    if ( sockfd == -1 )
    {
        exit(1);
    }

    struct event_base * base = event_init();
    if ( base == NULL )
    {
        exit(1);
    }

    struct event * sock_ev = event_new(base,sockfd,EV_READ|EV_PERSIST,accept_cb,base);
    if ( sock_ev == NULL )
    {
        exit(1);
    }
    event_add(sock_ev,NULL);

    event_base_dispatch(base);

    event_free(sock_ev);
    event_base_free(base);

    exit(0);



}
int socket_init()
{
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if ( sockfd == -1 )
    {
        return -1;
    }

    struct sockaddr_in saddr;
    memset(&saddr,0,sizeof(saddr));
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(6000);
    saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    int res = bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
    if ( res == -1 )
    {
        printf("bind err\n");
        return -1;
    }

    if( listen(sockfd,5) == -1)
    {
        return -1;
    }

    return sockfd;
}

二.Reactor模式工作流程

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

果蛋蛋

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

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

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

打赏作者

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

抵扣说明:

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

余额充值