epoll_socket.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Copied from
  2. // https://banu.com/blog/2/how-to-use-epoll-a-complete-example-in-c/epoll-example.c
  3. // Meant to be used for edge triggered epoll
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <netdb.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include <sys/epoll.h>
  13. #include <errno.h>
  14. #define MAXEVENTS 64
  15. static int
  16. make_socket_non_blocking (int sfd)
  17. {
  18. int flags, s;
  19. flags = fcntl (sfd, F_GETFL, 0);
  20. if (flags == -1) {
  21. perror ("fcntl");
  22. return -1;
  23. }
  24. flags |= O_NONBLOCK;
  25. s = fcntl (sfd, F_SETFL, flags);
  26. if (s == -1) {
  27. perror ("fcntl");
  28. return -1;
  29. }
  30. return 0;
  31. }
  32. static int
  33. create_and_bind (char *port)
  34. {
  35. struct addrinfo hints;
  36. struct addrinfo *result, *rp;
  37. int s, sfd;
  38. memset (&hints, 0, sizeof (struct addrinfo));
  39. hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
  40. hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
  41. hints.ai_flags = AI_PASSIVE; /* All interfaces */
  42. s = getaddrinfo (NULL, port, &hints, &result);
  43. if (s != 0) {
  44. fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s));
  45. return -1;
  46. }
  47. for (rp = result; rp != NULL; rp = rp->ai_next) {
  48. sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  49. if (sfd == -1)
  50. continue;
  51. s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
  52. if (s == 0)
  53. /* We managed to bind successfully! */
  54. break;
  55. close (sfd);
  56. }
  57. if (rp == NULL) {
  58. fprintf (stderr, "Could not bind\n");
  59. return -1;
  60. }
  61. freeaddrinfo (result);
  62. return sfd;
  63. }
  64. int main (int argc, char *argv[])
  65. {
  66. int sfd, s;
  67. int efd;
  68. struct epoll_event event;
  69. struct epoll_event *events;
  70. if (argc != 2) {
  71. fprintf(stderr, "Usage: %s [port]\n", argv[0]);
  72. exit(EXIT_FAILURE);
  73. }
  74. sfd = create_and_bind (argv[1]);
  75. if (sfd == -1)
  76. abort();
  77. s = make_socket_non_blocking(sfd);
  78. if (s == -1)
  79. abort();
  80. s = listen(sfd, SOMAXCONN);
  81. if (s == -1) {
  82. perror("listen");
  83. abort();
  84. }
  85. efd = epoll_create1(0);
  86. if (efd == -1) {
  87. perror("epoll_create");
  88. abort();
  89. }
  90. event.data.fd = sfd;
  91. event.events = EPOLLIN | EPOLLET;
  92. s = epoll_ctl(efd, EPOLL_CTL_ADD, sfd, &event);
  93. if (s == -1) {
  94. perror("epoll_ctl");
  95. abort();
  96. }
  97. /* Buffer where events are returned */
  98. events = calloc(MAXEVENTS, sizeof event);
  99. /* The event loop */
  100. while (1) {
  101. int n, i;
  102. n = epoll_wait(efd, events, MAXEVENTS, -1);
  103. for (i = 0; i < n; i++) {
  104. if ((events[i].events & EPOLLERR) ||
  105. (events[i].events & EPOLLHUP) ||
  106. (!(events[i].events & EPOLLIN))) {
  107. /* An error has occured on this fd, or the socket is not
  108. ready for reading (why were we notified then?) */
  109. fprintf(stderr, "epoll error\n");
  110. close(events[i].data.fd);
  111. continue;
  112. }
  113. else if (sfd == events[i].data.fd) {
  114. /* We have a notification on the listening socket, which
  115. means one or more incoming connections. */
  116. while (1) {
  117. struct sockaddr in_addr;
  118. socklen_t in_len;
  119. int infd;
  120. char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
  121. in_len = sizeof in_addr;
  122. infd = accept (sfd, &in_addr, &in_len);
  123. if (infd == -1) {
  124. if ((errno == EAGAIN) ||
  125. (errno == EWOULDBLOCK)) {
  126. /* We have processed all incoming
  127. connections. */
  128. break;
  129. } else {
  130. perror ("accept");
  131. break;
  132. }
  133. }
  134. s = getnameinfo(&in_addr, in_len,
  135. hbuf, sizeof hbuf,
  136. sbuf, sizeof sbuf,
  137. NI_NUMERICHOST | NI_NUMERICSERV);
  138. if (s == 0)
  139. printf("Accepted connection on descriptor %d "
  140. "(host=%s, port=%s)\n", infd, hbuf, sbuf);
  141. /* Make the incoming socket non-blocking and add it to the
  142. list of fds to monitor. */
  143. s = make_socket_non_blocking(infd);
  144. if (s == -1)
  145. abort();
  146. event.data.fd = infd;
  147. event.events = EPOLLIN | EPOLLET;
  148. s = epoll_ctl(efd, EPOLL_CTL_ADD, infd, &event);
  149. if (s == -1) {
  150. perror("epoll_ctl");
  151. abort();
  152. }
  153. }
  154. continue;
  155. } else {
  156. /* We have data on the fd waiting to be read. Read and
  157. display it. We must read whatever data is available
  158. completely, as we are running in edge-triggered mode
  159. and won't get a notification again for the same
  160. data. */
  161. int done = 0;
  162. while (1) {
  163. ssize_t count;
  164. char buf[512];
  165. count = read(events[i].data.fd, buf, sizeof buf);
  166. if (count == -1) {
  167. /* If errno == EAGAIN, that means we have read all
  168. data. So go back to the main loop. */
  169. if (errno != EAGAIN) {
  170. perror ("read");
  171. done = 1;
  172. }
  173. break;
  174. } else if (count == 0) {
  175. /* End of file. The remote has closed the
  176. connection. */
  177. done = 1;
  178. break;
  179. }
  180. /* Write the buffer to standard output */
  181. s = write(1, buf, count);
  182. if (s == -1) {
  183. perror("write");
  184. abort();
  185. }
  186. }
  187. if (done) {
  188. printf("Closed connection on descriptor %d\n",
  189. events[i].data.fd);
  190. /* Closing the descriptor will make epoll remove it
  191. from the set of descriptors which are monitored. */
  192. close (events[i].data.fd);
  193. }
  194. }
  195. }
  196. }
  197. free(events);
  198. close(sfd);
  199. return EXIT_SUCCESS;
  200. }