I/O复用-epoll

sever.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/fcntl.h>

#define MAX_EVENT_NUM   1024
#define BUFF_SIZE   20

//设置非阻塞
int setnoblock(int fd)
{
    int oldoption;
    int newoption;
    oldoption = fcntl(fd,F_GETFL);
    newoption = oldoption|O_NONBLOCK;
    fcntl(fd,F_SETFL,newoption);
    return oldoption;
}

//添加fd至epoll队列,并设置lt或et模式
void addpoll(int epollfd,int fd,int flag)
{
    epoll_event event;
    event.data.fd = fd;
    event.events = EPOLLIN;

    if(flag)
    {
        event.events = EPOLLIN|EPOLLET;
    }

    epoll_ctl(epollfd,EPOLL_CTL_ADD,fd,&event);
    setnoblock(fd);
}

int main(int argc,char* argv[])
{
    if(argc <= 2)
    {
        printf("Usage:%s IP PORT\n",argv[0]);
        return -1;
    }

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

    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(argv[2]);
    inet_pton(AF_INET,argv[1],&server.sin_addr);
    if(bind(sockfd,(struct sockaddr*)&server,sizeof(server)) < 0)
    {
        perror("bind");
        close(sockfd);
        return -1;
    }

    listen(sockfd,10);

    //创建epoll
    epoll_event events[MAX_EVENT_NUM];
    int epollfd;
    epollfd = epoll_create(BUFF_SIZE);
    if(epollfd < 0)
    {
        perror("epoll_create");
        close(sockfd);
        return -1;
    }

    addpoll(epollfd,sockfd,1);

    char buf[1024];
    int connfd;
    int num;
    int i = 0;
    while(1)
    {
        memset(buf,0,sizeof(buf));
        num = epoll_wait(epollfd,events,MAX_EVENT_NUM,-1);
        if(num < 0)
        {
            perror("epoll_wait");
            break;
        }

        //lt模式
        for(i=0;i<num,i++)
        {
            if(events[i].data.fd == sockfd)
            {
                struct sockaddr_in client;
                size_t len = sizeof(client);
                connfd = accept(sockfd,(struct sockaddr *)&client,&len);
                if(connfd < 0)
                {
                    perror("accept");
                    break;
                }
                //lt模式
                addpoll(epollfd,connfd,0);
                //et模式
                //addpoll(epollfd,connfd,1);
            }
            if(events[i].events & EPOLLIN)
            {
                recv(connfd,buf,1000,0);
                perror("recv");
                printf("buf = %s\n",buf);
            }
        }

    }

    close(epollfd);
    close(sockfd);

    return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值