epoll

1 #include <stdio.h>                                                                                          
  2 #include <sys/epoll.h>
  3 #include <sys/types.h>
  4 #include <netinet/in.h>
  5 #include <arpa/inet.h>
  6 #include <string.h>
  7 #include <stdlib.h>
  8 
  9 static void Use(char* proc)
 10 {
 11     printf("Use:%s [local_ip] [local_port]",proc);
 12 }
 13 
 14 typedef struct fd_buf
 15 {
 16     int fd;
 17     char buf[10240];
 18 }fd_buf,*fd_buf_p;
 19 
 20 static void* alloc_fd_buf(int fd)
 21 {
 22     fd_buf_p tmp =(fd_buf_p)malloc(sizeof(fd_buf)); 
 23     if(!tmp){
 24         perror("mlloc");
 25  26     }
 27     tmp->fd=fd;
 28     return tmp;
 29 }
 30 
 31 int startup(const char* ip,int port)
 32 {
 33     int sock =socket(AF_INET,SOCK_STREAM,0);
 34     if(sock<0){
 35         perror("socket");
 36         return 1;
 37     }
 38 
 39     int opt= 1;
 40     setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
 41     struct sockaddr_in local;
 42     local.sin_family =AF_INET;
 43     local.sin_port =htons(port);
 44     local.sin_addr.s_addr = inet_addr(ip);
 45     if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0){
 46         perror("bind");
 47         return 2;
 48     }
 49     if(listen(sock,10)<0){

  50         perror("listen");
 51         return 3;
 52     }
 53     return sock;
 54 }
 55 
 56 int main(int argc,char*argv[])
 57 {
 58     if(argc!=3){
 59         Use(argv[0]);
 60         return 1;
 61     }
 62     int listen_sock =startup(argv[1],atoi(argv[2]));
 63     int epollfd =epoll_create(256);
 64     if(epollfd<0){
 65         perror("epoll_create)");
 66         close(listen_sock); 
 67         return 2;
 68     }
 69     struct epoll_event ev;
 70     ev.events=EPOLLIN;
 71     ev.data.ptr =alloc_fd_buf(listen_sock);
 72     epoll_ctl(epollfd,EPOLL_CTL_ADD,listen_sock,&ev);
 73     struct epoll_event evs[64];
 74     int timeout=-1;   
             75     int nums=0; 
 76     while(1)
 77     {
 78         switch((nums=epoll_wait(epollfd,evs,64,timeout)))
 79         {
 80             case -1:
 81                 perror("epoll_wait");
 82                 break;
 83             case 0:
 84                 printf("timeout...\n");
 85                 break;
 86             default:
 87                 {
 88                     int i=0;
 89                     for(;i<nums;i++){
 90                         fd_buf_p fp=(fd_buf_p)evs[i].data.ptr;
 91                         if(fp->fd==listen_sock&&\
 92                                 (evs[i].events&EPOLLIN)){
 93                             struct sockaddr_in client;
 94                             socklen_t len=sizeof(client);
 95                             int new_sock=accept(listen_sock,\
 96                                     (struct sockaddr*)&client,&len);
 97                             if(new_sock<0){
 98                                 perror("accept");
 99                                 continue;                                                 100                             }//if
101                             printf("get a new client!\n");
102                             ev.events=EPOLLIN;
103                             ev.data.ptr=alloc_fd_buf(new_sock);
104                             epoll_ctl(epollfd,EPOLL_CTL_ADD,new_sock,&ev);
105                         }//if
106                         else if(fp->fd!=listen_sock){
107                             if(evs[i].events&EPOLLIN){
108                                 ssize_t s =read(fp->fd,fp->buf,\
109                                         sizeof(fp->buf));
110                                 if(s>0){
111                                     fp->buf[s]=0;
112                                     printf("client say# %s:",fp->buf);
113                                     ev.events=EPOLLOUT;
114                                     ev.data.ptr=fp;
115                                     epoll_ctl(epollfd,EPOLL_CTL_MOD,\
116                                             fp->fd,NULL);
117                                     free(fp);
118                                 }//if
119                             }//if
120                             else if(evs[i].events&EPOLLOUT){
121                                 const char*msg = "HTTP/1.0 200 ok\r\n\r\n<html><h1>hello word</h1></html>";
122                                 write(fp->fd,msg,strlen(msg));
123                                 close(fp->fd);
124                                 epoll_ctl(epollfd,EPOLL_CTL_DEL,fp->fd,NULL);   
125                                 free(fp);
126                             }//else if
127                             else{}
128                         }//else if
129                         else{}
130                     }//for
131                     break;
132                 }//default
133         }
134     }
135     return 0;
136 }          

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值