Chinaunix首页 | 论坛 | 博客
  • 博客访问: 172421
  • 博文数量: 13
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 350
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-03 15:44
文章分类

全部博文(13)

文章存档

2015年(3)

2014年(10)

我的朋友

分类: LINUX

2014-04-27 04:58:53


点击(此处)折叠或打开

  1. #include "unp.h"
  2. #include <sys/epoll.h>

  3. int epoll_create(int);
  4. int epoll_ctl(int,int,int,struct epoll_event *);
  5. int epoll_wait(int,struct epoll_event *,int,int);

  6. int
  7. main(int argc, char **argv)
  8. {
  9.     int            i, listenfd, connfd, sockfd;
  10.     int            epfd;    //epoll instance file describer
  11.     int            nready;  //the nummber of event needed to be processed
  12.     ssize_t        n;
  13.     char        buf[MAXLINE];
  14.     socklen_t    clilen;
  15.     struct        epoll_event ev;                //ev for register event
  16.     struct        epoll_event events[20];        //event[] for transmite event needed to be processed
  17.     struct        sockaddr_in cliaddr,servaddr;
  18.     
  19.     listenfd = socket(AF_INET, SOCK_STREAM, 0);  //creat listen socket

  20.     bzero(&servaddr, sizeof(servaddr));
  21.     servaddr.sin_family     = AF_INET;
  22.     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  23.     servaddr.sin_port = htons(SERV_PORT);
  24.     
  25.     Bind(listenfd, (SA *)&servaddr,sizeof(servaddr));
  26.     
  27.     Listen(listenfd, LISTENQ);
  28.     
  29.     epfd=epoll_create(256);         //creat epoll instance,size is 256
  30.     ev.data.fd=listenfd;            //set ev users data fd as listen
  31.     ev.events=EPOLLIN|EPOLLET;      //set epoll event is fd readable of ET
  32.     if(epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,&ev) < 0){
  33.         err_sys("epoll register fialed");
  34.         return -1;
  35.     }
  36.     for(;;){

  37.         nready=epoll_wait(epfd,events,20,500);              //wait for epoll events
  38.         for(i=0;i<nready;++i){
  39.             if(events[i].data.fd == listenfd){              //connect event
  40.                 clilen = sizeof(cliaddr);
  41.                 connfd = Accept(listenfd,(SA*)&cliaddr, &clilen);
  42.                 ev.data.fd = connfd;
  43.                 ev.events = EPOLLIN|EPOLLET;
  44.                 epoll_ctl(epfd,EPOLL_CTL_ADD,connfd,&ev);
  45.             }
  46.             
  47.             else if(events[i].events&EPOLLIN){               //input event
  48.                 if((sockfd = events[i].data.fd) < 0)//no valid sockfd
  49.                     continue;
  50.                 if((n = Read(sockfd, buf, MAXLINE)) < 0){    //read socket
  51.                     if(errno == ECONNRESET) {                //connect error
  52.                         Close(sockfd);
  53.                         events[i].data.fd = -1;
  54.                     }else
  55.                         err_sys("read error");
  56.                 }else if (n == 0) {                          //read error
  57.                     Close(sockfd);
  58.                     events[i].data.fd = -1;
  59.                 }
  60.                 ev.data.fd=sockfd;
  61.                 ev.events=EPOLLOUT|EPOLLET;
  62.                 epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);
  63.             }
  64.             else if(events[i].events&EPOLLOUT){              //output event
  65.                 sockfd = events[i].data.fd;
  66.                 Write(sockfd, buf, n);
  67.                 ev.data.fd=sockfd;
  68.                 ev.events=EPOLLIN|EPOLLET;
  69.                 epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);
  70.             }
  71.         }
  72.     }
  73. }

阅读(1253) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~