1. #include <sys/epoll.h>
    2. #include <sys/socket.h>
    3. #include <netinet/in.h>
    4. #include <unistd.h>
    5. #include <arpa/inet.h>
    6. #include <sys/types.h>
    7. #include <stdio.h>
    8. #define EPOLLEVENTS 64
    9. #define FDSIZE 128
    10. #define MAX_SIZE 1024
    11. int main(int argc, char **argv){
    12. }
    13. static int socket_bind(const char* ip,int port){
    14. int listenfd;
    15. struct sockaddr_in serveraddr;
    16. listenfd = socket(AF_INET, SOCK_STREAM, 0);
    17. if (listenfd==-1)
    18. {
    19. perror("socket error");
    20. exit(-1);
    21. }
    22. bzero(&serveraddr,sizeof(serveraddr));
    23. serveraddr.sin_family = AF_INET;
    24. inet_pton(AF_INET,ip,&serveraddr.sin_addr);
    25. serveraddr.sin_port = htons(port);
    26. if (bind(listenfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr))==-1)
    27. {
    28. perror("bind error");
    29. exit(-1);
    30. }
    31. return listenfd;
    32. }
    33. static void do_epoll(int listenfd){
    34. int epolldf;
    35. struct epoll_event events[EPOLLEVENTS];
    36. int read_cnt;
    37. char buf[MAX_SIZE];
    38. memset(buf,0,MAX_SIZE);
    39. epolldf = epoll_create(FDSIZE);
    40. add_event(epolldf,listenfd,EPOLLIN);
    41. for (;;)
    42. {
    43. read_cnt = epoll_wait(epolldf,events,EPOLLEVENTS,-1);
    44. handle_events(epolldf,events,read_cnt,listenfd,buf);
    45. }
    46. close(epolldf);
    47. }
    48. static void handle_events(int epollfd, struct epoll_event* evnets,int num,int listenfd,char* buf){
    49. int i;
    50. int fd;
    51. for ( i = 0; i < num; i++)
    52. {
    53. fd = evnets[i].data.fd;
    54. if ((fd==listenfd) && (evnets[i].events&EPOLLIN))
    55. {
    56. handle_accept(epollfd,listenfd);
    57. }else if (evnets[i].events&EPOLLIN){
    58. do_read(epollfd,fd,buf);
    59. }else if (evnets[i].events&EPOLLOUT){
    60. do_write(epollfd,fd,buf);
    61. }
    62. }
    63. }
    64. static void add_event(int epollfd, int fd,int state){
    65. struct epoll_event ev;
    66. ev.events = state;
    67. ev.data.fd = fd;
    68. if (epoll_ctl(epollfd,EPOLL_CTL_ADD,fd,&ev)<0)
    69. {
    70. printf("add event failed! \r\n");
    71. }
    72. }
    73. static void delete_event(int epollfd, int fd,int state){
    74. struct epoll_event ev;
    75. ev.events = state;
    76. ev.data.fd = fd;
    77. if (epoll_ctl(epollfd,EPOLL_CTL_DEL,fd,&ev)<0)
    78. {
    79. printf("delete event failed! \r\n");
    80. }
    81. }
    82. static void modify_event(int epollfd, int fd,int state){
    83. struct epoll_event ev;
    84. ev.events = state;
    85. ev.data.fd = fd;
    86. if (epoll_ctl(epollfd,EPOLL_CTL_MOD,fd,&ev)<0)
    87. {
    88. printf("modify event failed! \r\n");
    89. }
    90. }
    91. static void handle_accept(int epollfd, int listenfd){
    92. int clifd;
    93. struct sockaddr_in cliaddr;
    94. socklen_t cliaddrlen;
    95. clifd = accept(epollfd, (struct sockaddr*)&cliaddr, &cliaddrlen);
    96. if (clifd == -1)
    97. {
    98. perror("accept error \n");
    99. }else{
    100. printf("accept a new client%s:%d\n",
    101. inet_ntoa(cliaddr.sin_addr),cliaddr.sin_port);
    102. add_event(epollfd,clifd,EPOLLIN);
    103. }
    104. }
    105. static void do_read(int epollfd,int fd, char *buf){
    106. int nread;
    107. nread = read(fd,buf,MAX_SIZE);
    108. if (nread== -1)
    109. {
    110. perror("read error \n");
    111. delete_event(epollfd,fd,EPOLLIN);
    112. close(fd);
    113. }else if (nread==0){
    114. fprintf(stderr,"client closed\n");
    115. delete_event(epollfd,fd,EPOLLIN);
    116. close(fd);
    117. }else{
    118. modify_event(epollfd,fd,EPOLLOUT);
    119. }
    120. }
    121. static void do_write(int epollfd,int fd,char* buf){
    122. int nwrite;
    123. nwrite = write(epollfd,buf,strlen(buf));
    124. if (nwrite == -1)
    125. {
    126. perror("write error \n");
    127. delete_event(epollfd,fd,EPOLLOUT);
    128. close(fd);
    129. }else
    130. {
    131. modify_event(epollfd,fd,EPOLLIN);
    132. memset(buf,0,MAX_SIZE);
    133. }
    134. }