|
| 1 | +#include <Server/socket.hpp> |
| 2 | + |
| 3 | +#include <cstdio> |
| 4 | +#include <cstring> |
| 5 | +#include <iostream> |
| 6 | +#include <list> |
| 7 | +#include <sys/epoll.h> |
| 8 | +#include <sys/socket.h> |
| 9 | +#include <unistd.h> |
| 10 | + |
| 11 | +#define MAXCN 10 //连接队列中的个数 |
| 12 | +#define quit "quit" //关闭命令 |
| 13 | + |
| 14 | +void server_epoll(int serverfd) |
| 15 | +{ |
| 16 | + std::list<int> clientfds; |
| 17 | + struct epoll_event event; |
| 18 | + //事件数组 |
| 19 | + struct epoll_event eventList[MAXCN]; |
| 20 | + int epollfd = epoll_create(MAXCN); // |
| 21 | + int timeout = 3000; |
| 22 | + event.events = EPOLLIN | EPOLLET; |
| 23 | + event.data.fd = serverfd; |
| 24 | + if (epoll_ctl(epollfd, EPOLL_CTL_ADD, serverfd, &event) < 0) { |
| 25 | + perror("Epoll Add serverfd Fail:"); |
| 26 | + return; |
| 27 | + } |
| 28 | + setNonBlocking(serverfd); //配置非阻塞模式 |
| 29 | + event.data.fd = STDIN_FILENO; |
| 30 | + if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &event) < 0) { |
| 31 | + perror("Epoll Add STDIN_FILENO Fail:"); |
| 32 | + return; |
| 33 | + } |
| 34 | + setNonBlocking(STDIN_FILENO); |
| 35 | + //epoll |
| 36 | + while (1) { |
| 37 | + std::cout << "等待客户端连接..." << std::endl; |
| 38 | + //epoll_wait |
| 39 | + int ret = epoll_wait(epollfd, eventList, MAXCN, timeout); |
| 40 | + if (ret < 0) { |
| 41 | + perror("epoll error:"); |
| 42 | + break; |
| 43 | + } else if (ret == 0) { |
| 44 | + printf("timeout ...\n"); |
| 45 | + continue; |
| 46 | + } |
| 47 | + //直接获取了事件数量,给出了活动的流,这里是和poll区别的关键 |
| 48 | + int i = 0; |
| 49 | + for (i = 0; i < ret; i++) { |
| 50 | + //错误退出 |
| 51 | + if ((eventList[i].events & EPOLLERR) || (eventList[i].events & EPOLLHUP) |
| 52 | + || !(eventList[i].events & EPOLLIN)) { |
| 53 | + perror("epoll error:"); |
| 54 | + close(eventList[i].data.fd); |
| 55 | + continue; |
| 56 | + } |
| 57 | + if (eventList[i].data.fd == STDIN_FILENO) { |
| 58 | + char buf[BUFSIZ]; |
| 59 | + memset(buf, 0, sizeof buf); |
| 60 | + if (fgets(buf, BUFSIZ, stdin) == nullptr) { |
| 61 | + perror("End:"); |
| 62 | + exit(EXIT_FAILURE); |
| 63 | + } else { |
| 64 | + buf[strlen(buf) - 1] = '\0'; //减一的原因是不要回车字符 |
| 65 | + // 经验值:这一步非常重要的哦!!!!!!!! |
| 66 | + if (strcmp(buf, quit) == 0) { |
| 67 | + printf("Quit command!\n"); |
| 68 | + for (int j : clientfds) { |
| 69 | + send(j, buf, strlen(buf), 0); |
| 70 | + close(j); |
| 71 | + } |
| 72 | + close(serverfd); |
| 73 | + printf("关闭服务端\n"); |
| 74 | + clientfds.clear(); |
| 75 | + break; //关闭服务端 |
| 76 | + } |
| 77 | + for (int j : clientfds) { |
| 78 | + ssize_t nsend = send(j, buf, strlen(buf), 0); |
| 79 | + if (nsend < 0) { |
| 80 | + perror("send"); |
| 81 | + } |
| 82 | + printf("clinet[%d]\n", j); |
| 83 | + } |
| 84 | + } |
| 85 | + } else if (eventList[i].data.fd == serverfd) { |
| 86 | + int clientfd = server_accept(serverfd); |
| 87 | + if (clientfd <= 0) { |
| 88 | + perror("accept error:"); |
| 89 | + continue; |
| 90 | + } |
| 91 | + show_info(clientfd); |
| 92 | + if (clientfds.size() >= (MAXCN - 2)) { |
| 93 | + fprintf(stderr, "connfd size over %d\n", MAXCN); |
| 94 | + close(clientfd); |
| 95 | + } else { |
| 96 | + //将新建立的连接添加到EPOLL的监听中 |
| 97 | + struct epoll_event event_; |
| 98 | + event_.data.fd = clientfd; |
| 99 | + event_.events = EPOLLIN | EPOLLET; |
| 100 | + epoll_ctl(epollfd, EPOLL_CTL_ADD, clientfd, &event_); |
| 101 | + setNonBlocking(clientfd); //配置非阻塞模式 |
| 102 | + clientfds.push_back(clientfd); // |
| 103 | + } |
| 104 | + } else { |
| 105 | + char buf[BUFSIZ]; |
| 106 | + memset(buf, 0, sizeof buf); |
| 107 | + int sockfd = eventList[i].data.fd; |
| 108 | + //接受客户端数据 |
| 109 | + ssize_t nread = recv(sockfd, buf, sizeof buf, 0); |
| 110 | + show_info(sockfd); |
| 111 | + if (nread <= 0) { |
| 112 | + int res = epoll_ctl(epollfd, |
| 113 | + EPOLL_CTL_DEL, |
| 114 | + sockfd, |
| 115 | + nullptr); //将该文件描述符从红黑树摘除 |
| 116 | + if (res == -1) { |
| 117 | + perror("epoll_ctl error"); |
| 118 | + } |
| 119 | + close(sockfd); //关闭与该客户端的链接 |
| 120 | + printf("客户端连接关闭\n"); |
| 121 | + clientfds.remove(sockfd); |
| 122 | + } else { //正常接收到服务器的数据 |
| 123 | + printf("client[%d] send:%s\n", i, buf); |
| 124 | + snprintf(buf, 1024, "%s", "hello client!"); |
| 125 | + send(eventList[i].data.fd, buf, 1024, 0); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + if (serverfd) { |
| 131 | + close(serverfd); |
| 132 | + } |
| 133 | + if (epollfd) { |
| 134 | + close(epollfd); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +int main() |
| 139 | +{ |
| 140 | + std::cout << "Hello World!" << std::endl; |
| 141 | + int serverfd = socketListenBind(); |
| 142 | + if (serverfd < 0) { |
| 143 | + perror("Failed to open server:"); |
| 144 | + return -1; |
| 145 | + } |
| 146 | + printf("serverfd=%d\n", serverfd); |
| 147 | + server_epoll(serverfd); |
| 148 | + return 0; |
| 149 | +} |
0 commit comments