test_epoll_socket.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include <arpa/inet.h>
  2. #include <assert.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <netinet/in.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/epoll.h>
  10. #include <sys/socket.h>
  11. #include <sys/types.h>
  12. #include <unistd.h>
  13. #define SERVER_IP "111.111.11.1"
  14. #define CLIENT_IP "111.111.11.2"
  15. #define PORT 8888
  16. #define MAX_EVENTS 10
  17. #define BUFFER_SIZE 1024
  18. // 函数声明
  19. void server_process();
  20. void client_process();
  21. int main() {
  22. pid_t pid = fork();
  23. if (pid < 0) {
  24. perror("fork failed");
  25. exit(EXIT_FAILURE);
  26. } else if (pid == 0) {
  27. // 子进程作为客户端
  28. // 等待一秒,确保服务器已启动
  29. sleep(1);
  30. client_process();
  31. } else {
  32. // 父进程作为服务器
  33. server_process();
  34. }
  35. return 0;
  36. }
  37. // 服务器进程逻辑
  38. void server_process() {
  39. printf("[Server] Starting server process...\n");
  40. int listen_sock, conn_sock, epoll_fd;
  41. struct sockaddr_in server_addr, client_addr;
  42. socklen_t client_len = sizeof(client_addr);
  43. struct epoll_event ev, events[MAX_EVENTS];
  44. char buffer[BUFFER_SIZE];
  45. int data_processed = 0; // 添加标志位,标记是否已处理过数据
  46. if ((listen_sock = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) < 0) {
  47. perror("[Server] socket creation failed");
  48. exit(EXIT_FAILURE);
  49. }
  50. memset(&server_addr, 0, sizeof(server_addr));
  51. server_addr.sin_family = AF_INET;
  52. server_addr.sin_port = htons(PORT);
  53. if (inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr) <= 0) {
  54. perror("[Server] inet_pton failed");
  55. exit(EXIT_FAILURE);
  56. }
  57. if (bind(listen_sock,
  58. (struct sockaddr *)&server_addr,
  59. sizeof(server_addr)) < 0) {
  60. perror("[Server] bind failed");
  61. exit(EXIT_FAILURE);
  62. }
  63. if (listen(listen_sock, 1) < 0) {
  64. perror("[Server] listen failed");
  65. exit(EXIT_FAILURE);
  66. }
  67. printf("[Server] Listening on %s:%d\n", SERVER_IP, PORT);
  68. if ((epoll_fd = epoll_create1(0)) < 0) {
  69. perror("[Server] epoll_create1 failed");
  70. exit(EXIT_FAILURE);
  71. }
  72. ev.events = EPOLLIN;
  73. ev.data.fd = listen_sock;
  74. if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_sock, &ev) < 0) {
  75. perror("[Server] epoll_ctl: listen_sock failed");
  76. exit(EXIT_FAILURE);
  77. }
  78. printf("Adding listening socket %d to epoll\n", listen_sock);
  79. while (!data_processed) {
  80. int nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
  81. if (nfds < 0) {
  82. perror("[Server] epoll_wait failed");
  83. exit(EXIT_FAILURE);
  84. }
  85. for (int n = 0; n < nfds; ++n) {
  86. if (events[n].data.fd == listen_sock) {
  87. printf("trying to accept new connection...\n");
  88. while (1) {
  89. conn_sock = accept(listen_sock,
  90. (struct sockaddr *)&client_addr,
  91. &client_len);
  92. if (conn_sock < 0) {
  93. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  94. printf("All incoming connections have been "
  95. "processed.\n");
  96. break;
  97. } else {
  98. perror("accept error");
  99. exit(EXIT_FAILURE);
  100. break;
  101. }
  102. }
  103. ev.events = EPOLLIN | EPOLLET;
  104. ev.data.fd = conn_sock;
  105. if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, conn_sock, &ev) <
  106. 0) {
  107. perror("[Server] epoll_ctl: conn_sock failed");
  108. exit(EXIT_FAILURE);
  109. }
  110. printf("[Server] Accepted connection from %s:%d\n",
  111. inet_ntoa(client_addr.sin_addr),
  112. ntohs(client_addr.sin_port));
  113. }
  114. } else {
  115. printf("[Server] handling client data...\n");
  116. int client_fd = events[n].data.fd;
  117. int nread = read(client_fd, buffer, BUFFER_SIZE);
  118. if (nread == -1) {
  119. if (errno != EAGAIN) {
  120. perror("[Server] read error");
  121. close(client_fd);
  122. }
  123. } else if (nread == 0) {
  124. printf("[Server] Client disconnected.\n");
  125. close(client_fd);
  126. } else {
  127. buffer[nread] = '\0';
  128. printf("[Server] Received from client: %s\n", buffer);
  129. write(client_fd, buffer, nread);
  130. printf("[Server] Echoed data back to client. Server will "
  131. "now exit.\n");
  132. data_processed = 1; // 设置退出标志
  133. sleep(3);
  134. close(client_fd);
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. printf("[Server] Server process completed.\n");
  141. close(listen_sock);
  142. close(epoll_fd);
  143. }
  144. // 客户端进程逻辑
  145. void client_process() {
  146. printf("[Client] Starting client process...\n");
  147. int sock = 0;
  148. struct sockaddr_in client_bind_addr, server_addr;
  149. char buffer[BUFFER_SIZE] = {0};
  150. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  151. perror("[Client] socket creation failed");
  152. exit(EXIT_FAILURE);
  153. }
  154. memset(&client_bind_addr, 0, sizeof(client_bind_addr));
  155. client_bind_addr.sin_family = AF_INET;
  156. client_bind_addr.sin_port = htons(7777);
  157. if (inet_pton(AF_INET, CLIENT_IP, &client_bind_addr.sin_addr) <= 0) {
  158. perror("[Client] inet_pton for bind failed");
  159. exit(EXIT_FAILURE);
  160. }
  161. if (bind(sock,
  162. (struct sockaddr *)&client_bind_addr,
  163. sizeof(client_bind_addr)) < 0) {
  164. perror("[Client] bind failed");
  165. exit(EXIT_FAILURE);
  166. }
  167. printf("[Client] Bound to IP %s\n", CLIENT_IP);
  168. memset(&server_addr, 0, sizeof(server_addr));
  169. server_addr.sin_family = AF_INET;
  170. server_addr.sin_port = htons(PORT);
  171. if (inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr) <= 0) {
  172. perror("[Client] inet_pton for connect failed");
  173. exit(EXIT_FAILURE);
  174. }
  175. if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) <
  176. 0) {
  177. perror("[Client] connect failed");
  178. exit(EXIT_FAILURE);
  179. }
  180. printf("[Client] Connected to server %s:%d\n", SERVER_IP, PORT);
  181. const char *message = "Hello from client";
  182. write(sock, message, strlen(message));
  183. printf("[Client] Sent: %s\n", message);
  184. sleep(1);
  185. int valread = read(sock, buffer, BUFFER_SIZE);
  186. if (valread > 0) {
  187. buffer[valread] = '\0';
  188. printf("[Client] Received: %s\n", buffer);
  189. }
  190. printf("[Client] Client process completed.\n");
  191. close(sock);
  192. }