1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| #include <unistd.h> #include <sys/types.h> #include <fcntl.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <signal.h> #include <fcntl.h> #include <sys/wait.h> #include <sys/epoll.h> #include <string.h>
#include <vector> #include <algorithm> #include <iostream>
typedef std::vector<epoll_event> EventList; #define ERR_EXIT(m) \ do \ { \ perror(m); \ exit(EXIT_FAILURE); \ } while (0)
#define PORT 8080
int main() {
signal(SIGPIPE, SIG_IGN); signal(SIGCHLD, SIG_IGN);
int idlefd = open("/dev/null", O_RDONLY | O_CLOEXEC);
int listenfd; if ((listenfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_TCP)) < 0) ERR_EXIT("socket");
struct sockaddr_in servaddr; memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
int on = 1; if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) ERR_EXIT("setsockopt");
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) ERR_EXIT("bind"); if (listen(listenfd, SOMAXCONN) < 0) ERR_EXIT("listen");
std::vector<int> clients; int epollfd; epollfd = epoll_create1(EPOLL_CLOEXEC);
struct epoll_event event; event.data.fd = listenfd; event.events = EPOLLIN ; epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &event);
EventList events(16); struct sockaddr_in peeraddr; socklen_t peerlen; int connfd;
int nready; while (1) { nready = epoll_wait(epollfd, &*events.begin(), static_cast<int>(events.size()), -1); if (nready == -1) { if (errno == EINTR) continue;
ERR_EXIT("epoll_wait"); } if (nready == 0) continue;
if ((size_t)nready == events.size()) events.resize(events.size() * 2);
for (int i = 0; i < nready; ++i) { if (events[i].data.fd == listenfd) { peerlen = sizeof(peeraddr); connfd = accept4(listenfd, (struct sockaddr *)&peeraddr, &peerlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
if (connfd == -1) { if (errno == EMFILE) {
close(idlefd); idlefd = accept(listenfd, NULL, NULL); close(idlefd); idlefd = open("/dev/null", O_RDONLY | O_CLOEXEC); continue; } else ERR_EXIT("accept4"); }
std::cout << "ip=" << inet_ntoa(peeraddr.sin_addr) << " port=" << ntohs(peeraddr.sin_port) << std::endl;
clients.push_back(connfd);
event.data.fd = connfd; event.events = EPOLLIN ; epoll_ctl(epollfd, EPOLL_CTL_ADD, connfd, &event); } else if (events[i].events & EPOLLIN) { connfd = events[i].data.fd; if (connfd < 0) continue;
char buf[1024] = {0}; int ret = read(connfd, buf, 1024); if (ret == -1) ERR_EXIT("read"); if (ret == 0) {
std::cout << "client close" << std::endl; close(connfd); event = events[i]; epoll_ctl(epollfd, EPOLL_CTL_DEL, connfd, &event); clients.erase(std::remove(clients.begin(), clients.end(), connfd), clients.end()); continue; }
std::cout << buf;
write(connfd, buf, strlen(buf)); } } }
return 0; }
|