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
| #include <stdio.h> #include <stdlib.h> #include <string.h>
#ifdef _WIN32 #include <winsock2.h> #include <ws2tcpip.h> #pragma comment(lib, "ws2_32.lib") typedef int socklen_t; #define CLOSE_SOCKET closesocket #define poll WSAPoll #else #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <poll.h> #define CLOSE_SOCKET close #define SOCKET int #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #endif
#define PORT 8889 #define BUFFER_SIZE 1024 #define MAX_CLIENTS 30
void init_platform_specific() { #ifdef _WIN32 WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { fprintf(stderr, "WSAStartup 失败\n"); exit(EXIT_FAILURE); } #endif }
void cleanup_platform_specific() { #ifdef _WIN32 WSACleanup(); #endif }
int main() { init_platform_specific(); SOCKET master_socket; struct sockaddr_in server_addr, client_addr; char buffer[BUFFER_SIZE]; int activity, i, addrlen = sizeof(client_addr); struct pollfd fds[MAX_CLIENTS + 1]; int nfds = 1; if ((master_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { perror("socket 创建失败"); cleanup_platform_specific(); exit(EXIT_FAILURE); } #ifdef _WIN32 char opt = 1; #else int opt = 1; #endif if (setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt)) == SOCKET_ERROR) { perror("setsockopt 失败"); CLOSE_SOCKET(master_socket); cleanup_platform_specific(); exit(EXIT_FAILURE); } memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = htons(PORT); if (bind(master_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) { perror("bind 失败"); CLOSE_SOCKET(master_socket); cleanup_platform_specific(); exit(EXIT_FAILURE); } if (listen(master_socket, 3) == SOCKET_ERROR) { perror("listen 失败"); CLOSE_SOCKET(master_socket); cleanup_platform_specific(); exit(EXIT_FAILURE); } printf("服务器启动成功,监听端口 %d\n", PORT); memset(fds, 0, sizeof(fds)); fds[0].fd = master_socket; fds[0].events = POLLIN; for (i = 1; i <= MAX_CLIENTS; i++) { fds[i].fd = -1; } while (1) { activity = poll(fds, nfds, 500); if (activity < 0) { perror("poll 错误"); break; } if (fds[0].revents & POLLIN) { SOCKET new_socket; if ((new_socket = accept(master_socket, (struct sockaddr*)&client_addr, (socklen_t*)&addrlen)) == INVALID_SOCKET) { perror("accept 失败"); continue; } printf("新连接,socket fd: %d, IP: %s, 端口: %d\n", (int)new_socket, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); const char* welcome_message = "欢迎连接到poll服务器\r\n"; if (send(new_socket, welcome_message, strlen(welcome_message), 0) != strlen(welcome_message)) { perror("send 失败"); } int slot = -1; for (i = 1; i <= MAX_CLIENTS; i++) { if (fds[i].fd < 0) { fds[i].fd = new_socket; fds[i].events = POLLIN; slot = i; break; } } if (slot != -1) { printf("添加到clients列表,位置: %d\n", slot); if (slot >= nfds) { nfds = slot + 1; } } else { printf("无法添加新连接,服务器已满\n"); CLOSE_SOCKET(new_socket); } } for (i = 1; i < nfds; i++) { if (fds[i].fd < 0) continue; if (fds[i].revents & POLLIN) { memset(buffer, 0, BUFFER_SIZE); int valread = recv(fds[i].fd, buffer, BUFFER_SIZE, 0); if (valread <= 0) { getpeername(fds[i].fd, (struct sockaddr*)&client_addr, (socklen_t*)&addrlen); printf("客户端断开连接,IP: %s, 端口: %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); CLOSE_SOCKET(fds[i].fd); fds[i].fd = -1; if (i == nfds - 1) { while (nfds > 0 && fds[nfds-1].fd == -1) { nfds--; } } } else { buffer[valread] = '\0'; printf("从客户端接收: %s\n", buffer); char response[BUFFER_SIZE + 30]; sprintf(response, "服务器收到: %s", buffer); send(fds[i].fd, response, strlen(response), 0); } } } } for (i = 0; i < nfds; i++) { if (fds[i].fd >= 0) { CLOSE_SOCKET(fds[i].fd); } } cleanup_platform_specific(); return 0; }
|