epoll_socket.c 7.0 KB

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