epoll_wait_timeout.c 2.8 KB

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