2.6 epoll服务器

2.6.1 服务端

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <arpa/inet.h>
  9. #include <sys/epoll.h>
  10. #define SERVER_PORT (7778)
  11. #define EPOLL_MAX_NUM (2048)
  12. #define BUFFER_MAX_LEN (4096)
  13. char buffer[BUFFER_MAX_LEN];
  14. void str_toupper(char *str)
  15. {
  16. int i;
  17. for (i = 0; i < strlen(str); i ++) {
  18. str[i] = toupper(str[i]);
  19. }
  20. }
  21. int main(int argc, char **argv)
  22. {
  23. int listen_fd = 0;
  24. int client_fd = 0;
  25. struct sockaddr_in server_addr;
  26. struct sockaddr_in client_addr;
  27. socklen_t client_len;
  28. int epfd = 0;
  29. struct epoll_event event, *my_events;
  30. // socket
  31. listen_fd = socket(AF_INET, SOCK_STREAM, 0);
  32. // bind
  33. server_addr.sin_family = AF_INET;
  34. server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  35. server_addr.sin_port = htons(SERVER_PORT);
  36. bind(listen_fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
  37. // listen
  38. listen(listen_fd, 10);
  39. // epoll create
  40. epfd = epoll_create(EPOLL_MAX_NUM);
  41. if (epfd < 0) {
  42. perror("epoll create");
  43. goto END;
  44. }
  45. // listen_fd -> epoll
  46. event.events = EPOLLIN;
  47. event.data.fd = listen_fd;
  48. if (epoll_ctl(epfd, EPOLL_CTL_ADD, listen_fd, &event) < 0) {
  49. perror("epoll ctl add listen_fd ");
  50. goto END;
  51. }
  52. my_events = malloc(sizeof(struct epoll_event) * EPOLL_MAX_NUM);
  53. while (1) {
  54. // epoll wait
  55. int active_fds_cnt = epoll_wait(epfd, my_events, EPOLL_MAX_NUM, -1);
  56. int i = 0;
  57. for (i = 0; i < active_fds_cnt; i++) {
  58. // if fd == listen_fd
  59. if (my_events[i].data.fd == listen_fd) {
  60. //accept
  61. client_fd = accept(listen_fd, (struct sockaddr*)&client_addr, &client_len);
  62. if (client_fd < 0) {
  63. perror("accept");
  64. continue;
  65. }
  66. char ip[20];
  67. printf("new connection[%s:%d]\n", inet_ntop(AF_INET, &client_addr.sin_addr, ip, sizeof(ip)), ntohs(client_addr.sin_port));
  68. event.events = EPOLLIN | EPOLLET;
  69. event.data.fd = client_fd;
  70. epoll_ctl(epfd, EPOLL_CTL_ADD, client_fd, &event);
  71. }
  72. else if (my_events[i].events & EPOLLIN) {
  73. printf("EPOLLIN\n");
  74. client_fd = my_events[i].data.fd;
  75. // do read
  76. buffer[0] = '\0';
  77. int n = read(client_fd, buffer, 5);
  78. if (n < 0) {
  79. perror("read");
  80. continue;
  81. }
  82. else if (n == 0) {
  83. epoll_ctl(epfd, EPOLL_CTL_DEL, client_fd, &event);
  84. close(client_fd);
  85. }
  86. else {
  87. printf("[read]: %s\n", buffer);
  88. buffer[n] = '\0';
  89. #if 1
  90. str_toupper(buffer);
  91. write(client_fd, buffer, strlen(buffer));
  92. printf("[write]: %s\n", buffer);
  93. memset(buffer, 0, BUFFER_MAX_LEN);
  94. #endif
  95. /*
  96. event.events = EPOLLOUT;
  97. event.data.fd = client_fd;
  98. epoll_ctl(epfd, EPOLL_CTL_MOD, client_fd, &event);
  99. */
  100. }
  101. }
  102. else if (my_events[i].events & EPOLLOUT) {
  103. printf("EPOLLOUT\n");
  104. /*
  105. client_fd = my_events[i].data.fd;
  106. str_toupper(buffer);
  107. write(client_fd, buffer, strlen(buffer));
  108. printf("[write]: %s\n", buffer);
  109. memset(buffer, 0, BUFFER_MAX_LEN);
  110. event.events = EPOLLIN;
  111. event.data.fd = client_fd;
  112. epoll_ctl(epfd, EPOLL_CTL_MOD, client_fd, &event);
  113. */
  114. }
  115. }
  116. }
  117. END:
  118. close(epfd);
  119. close(listen_fd);
  120. return 0;
  121. }

2.6.2 客户端

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <strings.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <arpa/inet.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #define MAX_LINE (1024)
  11. #define SERVER_PORT (7778)
  12. void setnoblocking(int fd)
  13. {
  14. int opts = 0;
  15. opts = fcntl(fd, F_GETFL);
  16. opts = opts | O_NONBLOCK;
  17. fcntl(fd, F_SETFL);
  18. }
  19. int main(int argc, char **argv)
  20. {
  21. int sockfd;
  22. char recvline[MAX_LINE + 1] = {0};
  23. struct sockaddr_in server_addr;
  24. if (argc != 2) {
  25. fprintf(stderr, "usage ./client <SERVER_IP>\n");
  26. exit(0);
  27. }
  28. // 创建socket
  29. if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  30. fprintf(stderr, "socket error");
  31. exit(0);
  32. }
  33. // server addr 赋值
  34. bzero(&server_addr, sizeof(server_addr));
  35. server_addr.sin_family = AF_INET;
  36. server_addr.sin_port = htons(SERVER_PORT);
  37. if (inet_pton(AF_INET, argv[1], &server_addr.sin_addr) <= 0) {
  38. fprintf(stderr, "inet_pton error for %s", argv[1]);
  39. exit(0);
  40. }
  41. // 链接服务端
  42. if (connect(sockfd, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0) {
  43. perror("connect");
  44. fprintf(stderr, "connect error\n");
  45. exit(0);
  46. }
  47. setnoblocking(sockfd);
  48. char input[100];
  49. int n = 0;
  50. int count = 0;
  51. // 不断的从标准输入字符串
  52. while (fgets(input, 100, stdin) != NULL)
  53. {
  54. printf("[send] %s\n", input);
  55. n = 0;
  56. // 把输入的字符串发送 到 服务器中去
  57. n = send(sockfd, input, strlen(input), 0);
  58. if (n < 0) {
  59. perror("send");
  60. }
  61. n = 0;
  62. count = 0;
  63. // 读取 服务器返回的数据
  64. while (1)
  65. {
  66. n = read(sockfd, recvline + count, MAX_LINE);
  67. if (n == MAX_LINE)
  68. {
  69. count += n;
  70. continue;
  71. }
  72. else if (n < 0){
  73. perror("recv");
  74. break;
  75. }
  76. else {
  77. count += n;
  78. recvline[count] = '\0';
  79. printf("[recv] %s\n", recvline);
  80. break;
  81. }
  82. }
  83. }
  84. return 0;
  85. }