按照书上 实现I/O复用

main函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <signal.h>
#include <event.h>
#include <time.h>

void sig_fun(int fd, short ev, void *arg)
{
    if( ev & EV_SIGNAL)
    {
	printf("recv:%d\n",fd);
    }
    if( ev & EV_TIMEOUT)
    {
	printf("sig time out\n");
    }
}

void time_cb(int fd, short ev, void *arg)
{
    printf("time out\n");
}

int main()
{
    struct event_base *base = event_init(); //创建libevent实例
    assert( base != NULL);

    struct timeval sig_tv = {3,0};
    struct event *sig_ev = event_new(base,SIGINT,EV_SIGNAL|EV_PERSIST,sig_fun,NULL);
   // struct event *sig_ev = evsignal_new(base,SIGINT,sig_fun,NULL);//定义事件
    assert( sig_ev != NULL);
    event_add(sig_ev,&sig_tv);//添加事件

    struct event * tv_ev = event_new(base,-1,EV_TIMEOUT,time_cb,NULL);
//    struct event *tv_ev = evtimer_new(base,time_cb,NULL); //定时器
    struct timeval tv = {5,0};

    event_add(tv_ev,&tv);

    event_base_dispatch(base); //事件循环,调select poll epoll

    event_free(tv_ev);
    event_free(sig_ev);
    event_base_free(base);


}

io.c

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

void recv_cb(int fd, short ev, void *arg)
{
    if( ev & EV_READ)
    {
	char buff[128] = {0};
	int n = recv(fd,buff,127,0);
	if( n <= 0)
	{
	    //
	    printf("one client over\n");
	    return ;
	}

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

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

	int c = accept(fd,(struct sockdaddr*)&caddr,&len);
	if( c < 0 )
	{
	    return ;
	}
	
	struct event_base * base = (struct event_base*)arg;
	struct event *c_ev = event_new(base,c,EV_READ|EV_PERSIST,recv_cb,NULL);
	assert( c_ev != NULL);
	event_add(c_ev,NULL);
    }
}

int create_socket();

int main()
{
    int sockfd = create_socket();
    assert( sockfd != -1);

    struct event_base *base = event_init(); //创建实例
    assert( base != NULL);

    struct event *sock_ev = event_new(base,sockfd,EV_READ|EV_PERSIST,accept_cb,base);	 assert( sock_ev != NULL);
    event_add(sock_ev,NULL);

    event_base_dispatch(base); //shijianxunhuan
    event_free(sock_ev);
    event_base_free(base);

}

int create_socket()
{
    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("172.0.0.1");

    int res = bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
    if( res == -1)
    {
	return -1;
    }
    listen(sockfd,5);
    
    while(1)
    {
	struct sockaddr_in cli;
	int len = sizeof(cli);
	int c = accept(sockfd, (struct sockaddr*)&cli, &len);
	if ( c < 0)
	{
	    printf("error\n");
	    return 0;
	    continue;
	}

	
    }
    return sockfd;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值