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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
| #define _POSIX_C_SOURCE 200112L #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/select.h> #include <signal.h> #include <time.h> #include <errno.h> #include <string.h> #include <fcntl.h> #include <getopt.h>
// 服务器配置结构体 struct server_config { int max_clients; int timeout_seconds; int verbose; int use_signals; };
// 客户端信息结构体 struct client_info { int fd; int connected; time_t connect_time; char buffer[1024]; size_t buffer_pos; };
// 服务器状态结构体 struct server_state { int running; int client_count; struct client_info *clients; int max_clients; };
// 全局变量 volatile sig_atomic_t server_terminate = 0;
// 信号处理函数 void signal_handler(int sig) { server_terminate = 1; printf("\n收到终止信号 %d\n", sig); }
// 初始化服务器状态 int init_server_state(struct server_state *state, int max_clients) { state->running = 1; state->client_count = 0; state->max_clients = max_clients; state->clients = calloc(max_clients, sizeof(struct client_info)); if (!state->clients) { perror("分配客户端数组失败"); return -1; } return 0; }
// 添加客户端 int add_client(struct server_state *state, int client_fd) { if (state->client_count >= state->max_clients) { fprintf(stderr, "客户端数量已达上限: %d\n", state->max_clients); return -1; } for (int i = 0; i < state->max_clients; i++) { if (!state->clients[i].connected) { state->clients[i].fd = client_fd; state->clients[i].connected = 1; state->clients[i].connect_time = time(NULL); state->clients[i].buffer_pos = 0; state->client_count++; printf("添加客户端 %d (fd: %d),当前客户端数: %d\n", i, client_fd, state->client_count); return i; } } fprintf(stderr, "找不到空闲的客户端槽位\n"); return -1; }
// 移除客户端 void remove_client(struct server_state *state, int client_index) { if (client_index >= 0 && client_index < state->max_clients && state->clients[client_index].connected) { close(state->clients[client_index].fd); memset(&state->clients[client_index], 0, sizeof(struct client_info)); state->client_count--; printf("移除客户端 %d,剩余客户端数: %d\n", client_index, state->client_count); } }
// 显示服务器状态 void show_server_status(const struct server_state *state) { printf("=== 服务器状态 ===\n"); printf("运行状态: %s\n", state->running ? "运行中" : "已停止"); printf("客户端数量: %d/%d\n", state->client_count, state->max_clients); printf("当前时间: %s", ctime(&(time_t){time(NULL)})); if (state->client_count > 0) { printf("连接的客户端:\n"); for (int i = 0; i < state->max_clients; i++) { if (state->clients[i].connected) { printf(" [%d] fd=%d 连接时间: %s", i, state->clients[i].fd, ctime(&state->clients[i].connect_time)); } } } printf("\n"); }
int main(int argc, char *argv[]) { struct server_config config = { .max_clients = 10, .timeout_seconds = 30, .verbose = 0, .use_signals = 0 }; struct server_state server_state_struct; fd_set read_fds; struct timespec timeout; sigset_t sigmask; int max_fd = STDIN_FILENO; int ready; printf("=== pselect 事件驱动服务器模拟器 ===\n\n"); // 解析命令行参数 static struct option long_options[] = { {"clients", required_argument, 0, 'c'}, {"timeout", required_argument, 0, 't'}, {"verbose", no_argument, 0, 'v'}, {"signals", no_argument, 0, 's'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; int opt; while ((opt = getopt_long(argc, argv, "c:t:vsh", long_options, NULL)) != -1) { switch (opt) { case 'c': config.max_clients = atoi(optarg); if (config.max_clients <= 0) config.max_clients = 10; break; case 't': config.timeout_seconds = atoi(optarg); if (config.timeout_seconds <= 0) config.timeout_seconds = 30; break; case 'v': config.verbose = 1; break; case 's': config.use_signals = 1; break; case 'h': printf("用法: %s [选项]\n", argv[0]); printf("选项:\n"); printf(" -c, --clients=NUM 最大客户端数 (默认 10)\n"); printf(" -t, --timeout=SECONDS 超时时间 (默认 30 秒)\n"); printf(" -v, --verbose 详细输出\n"); printf(" -s, --signals 启用信号处理\n"); printf(" -h, --help 显示此帮助信息\n"); return 0; default: fprintf(stderr, "使用 '%s --help' 查看帮助信息\n", argv[0]); return 1; } } // 设置信号处理 if (config.use_signals) { struct sigaction sa; sa.sa_handler = signal_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sigaction(SIGINT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); printf("✓ 信号处理已启用\n"); } // 初始化服务器状态 if (init_server_state(&server_state_struct, config.max_clients) == -1) { return 1; } printf("服务器配置:\n"); printf(" 最大客户端数: %d\n", config.max_clients); printf(" 超时时间: %d 秒\n", config.timeout_seconds); printf(" 详细输出: %s\n", config.verbose ? "是" : "否"); printf(" 信号处理: %s\n", config.use_signals ? "是" : "否"); printf("\n"); // 设置超时时间 timeout.tv_sec = config.timeout_seconds; timeout.tv_nsec = 0; // 设置信号屏蔽集 sigemptyset(&sigmask); printf("启动服务器模拟...\n"); printf("可用命令:\n"); printf(" 输入 'status' 查看服务器状态\n"); printf(" 输入 'connect' 模拟客户端连接\n"); printf(" 输入 'quit' 或 'exit' 退出服务器\n"); printf(" 输入 'help' 显示帮助信息\n"); printf(" 按 Ctrl+C 发送终止信号\n"); printf("\n"); // 主循环 while (server_state_struct.running && !server_terminate) { // 初始化文件描述符集合 FD_ZERO(&read_fds); FD_SET(STDIN_FILENO, &read_fds); max_fd = STDIN_FILENO; // 添加连接的客户端 for (int i = 0; i < server_state_struct.max_clients; i++) { if (server_state_struct.clients[i].connected) { FD_SET(server_state_struct.clients[i].fd, &read_fds); if (server_state_struct.clients[i].fd > max_fd) { max_fd = server_state_struct.clients[i].fd; } } } if (config.verbose) { printf("监视 %d 个文件描述符 (最大fd: %d)...\n", server_state_struct.client_count + 1, max_fd); } // 使用 pselect 等待事件 ready = pselect(max_fd + 1, &read_fds, NULL, NULL, &timeout, &sigmask); if (ready == -1) { if (errno == EINTR) { if (config.verbose) { printf("pselect 被信号中断\n"); } continue; } else { perror("pselect 失败"); break; } } else if (ready == 0) { if (config.verbose) { printf("超时: 没有事件发生\n"); } } else { if (config.verbose) { printf("准备就绪的文件描述符数量: %d\n", ready); } // 处理标准输入事件 if (FD_ISSET(STDIN_FILENO, &read_fds)) { char input_buffer[256]; ssize_t bytes_read = read(STDIN_FILENO, input_buffer, sizeof(input_buffer) - 1); if (bytes_read > 0) { input_buffer[bytes_read] = '\0'; // 处理特殊命令 if (strncmp(input_buffer, "quit", 4) == 0 || strncmp(input_buffer, "exit", 4) == 0) { printf("收到退出命令\n"); server_state_struct.running = 0; server_terminate = 1; } else if (strncmp(input_buffer, "status", 6) == 0) { show_server_status(&server_state_struct); } else if (strncmp(input_buffer, "help", 4) == 0) { printf("可用命令:\n"); printf(" quit/exit - 退出服务器\n"); printf(" status - 显示服务器状态\n"); printf(" help - 显示帮助信息\n"); printf(" Ctrl+C - 发送终止信号\n"); } else if (strncmp(input_buffer, "connect", 7) == 0) { if (server_state_struct.client_count < server_state_struct.max_clients) { // 模拟创建客户端连接 int fake_fd = 1000 + server_state_struct.client_count; int client_index = add_client(&server_state_struct, fake_fd); if (client_index != -1) { printf("模拟客户端连接成功 (fd: %d)\n", fake_fd); } } else { printf("客户端数量已达上限\n"); } } else { printf("收到输入: %s", input_buffer); } } } // 处理客户端事件 for (int i = 0; i < server_state_struct.max_clients; i++) { if (server_state_struct.clients[i].connected && FD_ISSET(server_state_struct.clients[i].fd, &read_fds)) { char client_buffer[256]; ssize_t bytes_read = read(server_state_struct.clients[i].fd, client_buffer, sizeof(client_buffer) - 1); if (bytes_read > 0) { client_buffer[bytes_read] = '\0'; printf("客户端 %d 发送数据: %s", i, client_buffer); // 回显数据 write(server_state_struct.clients[i].fd, "Echo: ", 6); write(server_state_struct.clients[i].fd, client_buffer, bytes_read); } else if (bytes_read == 0) { printf("客户端 %d 断开连接\n", i); remove_client(&server_state_struct, i); } else { if (errno != EAGAIN && errno != EWOULDBLOCK) { perror("读取客户端数据失败"); remove_client(&server_state_struct, i); } } } } } } // 清理资源 printf("清理资源...\n"); for (int i = 0; i < server_state_struct.max_clients; i++) { if (server_state_struct.clients[i].connected) { remove_client(&server_state_struct, i); } } free(server_state_struct.clients); printf("服务器模拟结束\n"); printf("\n=== pselect 服务器应用说明 ===\n"); printf("核心技术:\n"); printf("1. 多路复用: 同时监视多个文件描述符\n"); printf("2. 事件驱动: 基于事件的通知机制\n"); printf("3. 信号安全: 原子的信号处理\n"); printf("4. 超时控制: 精确的超时管理\n"); printf("5. 资源管理: 动态的客户端管理\n"); printf("\n"); printf("pselect 优势:\n"); printf("1. 原子性: 等待和信号处理是原子操作\n"); printf("2. 灵活性: 可以精确控制信号屏蔽\n"); printf("3. 精确性: 纳秒级超时控制\n"); printf("4. 可扩展: 适用于中小规模并发\n"); printf("5. 安全性: 避免信号处理中的竞态条件\n"); return 0; }
|