epoll_wait_timeout.c 2.7 KB

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