epoll

来源:互联网 发布:linux delay 头文件 编辑:程序博客网 时间:2024/06/13 05:03
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                             }//if101                             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                         }//if106                         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                                 }//if119                             }//if120                             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 if127                             else{}128                         }//else if129                         else{}130                     }//for131                     break;132                 }//default133         }134     }135     return 0;136 }          
原创粉丝点击