epoll_wait_timeout.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <sys/epoll.h>
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <netdb.h>
  13. /* On success, a file descriptor for the new socket is returned.
  14. * On error, -1 is returned. */
  15. static int create_and_bind (char *port) {
  16. struct addrinfo hints;
  17. struct addrinfo *result, *rp;
  18. int s, sfd;
  19. memset (&hints, 0, sizeof (struct addrinfo));
  20. hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
  21. hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
  22. hints.ai_flags = AI_PASSIVE; /* All interfaces */
  23. s = getaddrinfo (NULL, port, &hints, &result);
  24. if (s != 0) {
  25. fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s));
  26. return -1;
  27. }
  28. for (rp = result; rp != NULL; rp = rp->ai_next) {
  29. sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  30. if (sfd == -1)
  31. continue;
  32. s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
  33. if (s == 0) {
  34. /* We managed to bind successfully! */
  35. break;
  36. }
  37. close (sfd);
  38. }
  39. if (rp == NULL) {
  40. fprintf (stderr, "Could not bind\n");
  41. return -1;
  42. }
  43. freeaddrinfo (result);
  44. return sfd;
  45. }
  46. /* On success, 0 is returned. On error, -1 is returned. */
  47. static int make_socket_non_blocking (int sfd) {
  48. int flags, s;
  49. flags = fcntl (sfd, F_GETFL, 0);
  50. if (flags == -1) {
  51. perror ("fcntl");
  52. return -1;
  53. }
  54. flags |= O_NONBLOCK;
  55. s = fcntl (sfd, F_SETFL, flags);
  56. if (s == -1) {
  57. perror ("fcntl");
  58. return -1;
  59. }
  60. return 0;
  61. }
  62. #define MAXEVENTS 64
  63. int main (int argc, char *argv[]) {
  64. int sfd, s, n;
  65. int efd;
  66. struct epoll_event event;
  67. struct epoll_event *events;
  68. if (argc != 2) {
  69. perror("please specify port");
  70. return 1;
  71. }
  72. sfd = create_and_bind(argv[1]);
  73. if (sfd == -1)
  74. return 1;
  75. s = make_socket_non_blocking(sfd);
  76. if (s == -1)
  77. return 1;
  78. s = listen(sfd, SOMAXCONN);
  79. if (s == -1) {
  80. perror ("listen");
  81. return 1;
  82. }
  83. efd = epoll_create1(0);
  84. if (efd == -1) {
  85. perror ("epoll_create");
  86. return 1;
  87. }
  88. event.data.fd = sfd;
  89. event.events = EPOLLIN | EPOLLET;
  90. s = epoll_ctl (efd, EPOLL_CTL_ADD, sfd, &event);
  91. if (s == -1) {
  92. perror ("epoll_ctl");
  93. return 1;
  94. }
  95. events = calloc (MAXEVENTS, sizeof event);
  96. /* epoll_wait with 1 second timeout */
  97. n = epoll_wait (efd, events, MAXEVENTS, 1000);
  98. printf("epoll_wait test passed\n");
  99. free (events);
  100. close (sfd);
  101. return 0;
  102. }