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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
| // 简单的 HTTP 服务器示例,展示不同机制的使用 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include <poll.h> #include <sys/epoll.h>
#define PORT 8080 #define MAX_CLIENTS 1000 #define BUFFER_SIZE 4096
// 创建监听套接字 int create_server_socket(int port) { int server_fd; struct sockaddr_in address; int opt = 1; if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { perror("socket failed"); return -1; } if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) { perror("setsockopt"); close(server_fd); return -1; } address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(port); if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { perror("bind failed"); close(server_fd); return -1; } if (listen(server_fd, 3) < 0) { perror("listen"); close(server_fd); return -1; } return server_fd; }
// 处理 HTTP 请求 void handle_http_request(int client_fd) { char buffer[BUFFER_SIZE]; ssize_t bytes_read = read(client_fd, buffer, sizeof(buffer) - 1); if (bytes_read > 0) { buffer[bytes_read] = '\0'; // 简单的 HTTP 响应 const char *response = "HTTP/1.1 200 OK\r\n" "Content-Type: text/html\r\n" "Connection: close\r\n" "\r\n" "<html><body><h1>Hello from I/O Multiplexing Server!</h1></body></html>\r\n"; write(client_fd, response, strlen(response)); } close(client_fd); }
// 使用 poll 的 HTTP 服务器 int http_server_poll(int port) { int server_fd, client_fd; struct sockaddr_in address; int addrlen = sizeof(address); struct pollfd *fds; int max_fds = MAX_CLIENTS + 1; int nfds = 1; printf("Starting HTTP server with poll on port %d\n", port); server_fd = create_server_socket(port); if (server_fd == -1) return -1; fds = calloc(max_fds, sizeof(struct pollfd)); if (!fds) { perror("calloc"); close(server_fd); return -1; } // 添加监听套接字 fds[0].fd = server_fd; fds[0].events = POLLIN; fds[0].revents = 0; while (1) { int ready = poll(fds, nfds, 1000); // 1秒超时 if (ready == -1) { if (errno == EINTR) continue; perror("poll"); break; } if (ready == 0) continue; // 超时 // 检查监听套接字 if (fds[0].revents & POLLIN) { client_fd = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen); if (client_fd >= 0) { if (nfds < max_fds) { fds[nfds].fd = client_fd; fds[nfds].events = POLLIN; fds[nfds].revents = 0; nfds++; printf("New connection from %s:%d\n", inet_ntoa(address.sin_addr), ntohs(address.sin_port)); } else { printf("Too many connections, rejecting\n"); close(client_fd); } } } // 检查客户端连接 for (int i = 1; i < nfds; i++) { if (fds[i].revents & POLLIN) { handle_http_request(fds[i].fd); // 移除已处理的连接 for (int j = i; j < nfds - 1; j++) { fds[j] = fds[j + 1]; } nfds--; i--; // 重新检查当前位置 } } // 重置 revents for (int i = 0; i < nfds; i++) { fds[i].revents = 0; } } free(fds); close(server_fd); return 0; }
// 使用 epoll 的 HTTP 服务器 int http_server_epoll(int port) { int server_fd, client_fd, epoll_fd; struct sockaddr_in address; int addrlen = sizeof(address); struct epoll_event ev, events[MAX_CLIENTS]; int nfds; printf("Starting HTTP server with epoll on port %d\n", port); server_fd = create_server_socket(port); if (server_fd == -1) return -1; epoll_fd = epoll_create1(0); if (epoll_fd == -1) { perror("epoll_create1"); close(server_fd); return -1; } // 添加监听套接字到 epoll ev.events = EPOLLIN; ev.data.fd = server_fd; if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, server_fd, &ev) == -1) { perror("epoll_ctl: listen"); close(server_fd); close(epoll_fd); return -1; } while (1) { nfds = epoll_wait(epoll_fd, events, MAX_CLIENTS, 1000); // 1秒超时 if (nfds == -1) { if (errno == EINTR) continue; perror("epoll_wait"); break; } if (nfds == 0) continue; // 超时 for (int i = 0; i < nfds; i++) { if (events[i].data.fd == server_fd) { // 新连接 client_fd = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen); if (client_fd >= 0) { ev.events = EPOLLIN | EPOLLET; ev.data.fd = client_fd; if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev) == -1) { perror("epoll_ctl: client"); close(client_fd); } else { printf("New connection from %s:%d\n", inet_ntoa(address.sin_addr), ntohs(address.sin_port)); } } } else { // 客户端数据 handle_http_request(events[i].data.fd); epoll_ctl(epoll_fd, EPOLL_CTL_DEL, events[i].data.fd, NULL); } } } close(epoll_fd); close(server_fd); return 0; }
|