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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
| #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <fcntl.h> #include <sys/select.h> #include <sys/epoll.h> #include <netdb.h>
typedef enum { SOCKET_TYPE_TCP = SOCK_STREAM, SOCKET_TYPE_UDP = SOCK_DGRAM, SOCKET_TYPE_RAW = SOCK_RAW } socket_type_t;
typedef enum { SOCKET_STATE_CLOSED = 0, SOCKET_STATE_LISTENING, SOCKET_STATE_CONNECTING, SOCKET_STATE_CONNECTED, SOCKET_STATE_ERROR } socket_state_t;
typedef struct { int fd; socket_type_t type; socket_state_t state; struct sockaddr_in local_addr; struct sockaddr_in remote_addr; int reuse_addr; int non_blocking; int keep_alive; int send_buffer_size; int recv_buffer_size; time_t create_time; time_t last_activity; } socket_info_t;
socket_info_t* create_socket(socket_type_t type, const char* ip, int port) { socket_info_t* sock_info = (socket_info_t*)malloc(sizeof(socket_info_t)); if (!sock_info) { printf("错误: 无法分配Socket信息内存\n"); return NULL; } memset(sock_info, 0, sizeof(socket_info_t)); sock_info->fd = socket(AF_INET, type, 0); if (sock_info->fd < 0) { printf("错误: 创建Socket失败: %s\n", strerror(errno)); free(sock_info); return NULL; } sock_info->type = type; sock_info->state = SOCKET_STATE_CLOSED; sock_info->create_time = time(NULL); sock_info->last_activity = sock_info->create_time; sock_info->local_addr.sin_family = AF_INET; sock_info->local_addr.sin_port = htons(port); if (ip && strlen(ip) > 0) { inet_pton(AF_INET, ip, &sock_info->local_addr.sin_addr); } else { sock_info->local_addr.sin_addr.s_addr = INADDR_ANY; } printf("创建Socket: fd=%d, 类型=%s, 地址=%s:%d\n", sock_info->fd, (type == SOCKET_TYPE_TCP) ? "TCP" : "UDP", ip ? ip : "0.0.0.0", port); return sock_info; }
int set_socket_option(socket_info_t* sock_info, int option, int value) { if (!sock_info || sock_info->fd < 0) { return -1; } int result = 0; switch (option) { case SO_REUSEADDR: result = setsockopt(sock_info->fd, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value)); if (result == 0) { sock_info->reuse_addr = value; } break; case SO_KEEPALIVE: result = setsockopt(sock_info->fd, SOL_SOCKET, SO_KEEPALIVE, &value, sizeof(value)); if (result == 0) { sock_info->keep_alive = value; } break; case SO_SNDBUF: result = setsockopt(sock_info->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)); if (result == 0) { sock_info->send_buffer_size = value; } break; case SO_RCVBUF: result = setsockopt(sock_info->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)); if (result == 0) { sock_info->recv_buffer_size = value; } break; default: result = setsockopt(sock_info->fd, SOL_SOCKET, option, &value, sizeof(value)); break; } if (result < 0) { printf("错误: 设置Socket选项失败: %s\n", strerror(errno)); } else { printf("设置Socket选项: fd=%d, 选项=%d, 值=%d\n", sock_info->fd, option, value); } return result; }
int set_non_blocking(socket_info_t* sock_info, int non_blocking) { if (!sock_info || sock_info->fd < 0) { return -1; } int flags = fcntl(sock_info->fd, F_GETFL, 0); if (flags < 0) { printf("错误: 获取Socket标志失败: %s\n", strerror(errno)); return -1; } if (non_blocking) { flags |= O_NONBLOCK; } else { flags &= ~O_NONBLOCK; } if (fcntl(sock_info->fd, F_SETFL, flags) < 0) { printf("错误: 设置Socket标志失败: %s\n", strerror(errno)); return -1; } sock_info->non_blocking = non_blocking; printf("设置Socket模式: fd=%d, 非阻塞=%s\n", sock_info->fd, non_blocking ? "是" : "否"); return 0; }
int bind_socket(socket_info_t* sock_info) { if (!sock_info || sock_info->fd < 0) { return -1; } if (bind(sock_info->fd, (struct sockaddr*)&sock_info->local_addr, sizeof(sock_info->local_addr)) < 0) { printf("错误: 绑定Socket失败: %s\n", strerror(errno)); return -1; } char ip_str[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &sock_info->local_addr.sin_addr, ip_str, sizeof(ip_str)); printf("绑定Socket: fd=%d, 地址=%s:%d\n", sock_info->fd, ip_str, ntohs(sock_info->local_addr.sin_port)); return 0; }
int listen_socket(socket_info_t* sock_info, int backlog) { if (!sock_info || sock_info->fd < 0) { return -1; } if (sock_info->type != SOCKET_TYPE_TCP) { printf("错误: 只有TCP Socket可以监听\n"); return -1; } if (listen(sock_info->fd, backlog) < 0) { printf("错误: 监听Socket失败: %s\n", strerror(errno)); return -1; } sock_info->state = SOCKET_STATE_LISTENING; printf("监听Socket: fd=%d, 队列长度=%d\n", sock_info->fd, backlog); return 0; }
int connect_socket(socket_info_t* sock_info, const char* ip, int port) { if (!sock_info || sock_info->fd < 0) { return -1; } sock_info->remote_addr.sin_family = AF_INET; sock_info->remote_addr.sin_port = htons(port); if (inet_pton(AF_INET, ip, &sock_info->remote_addr.sin_addr) <= 0) { printf("错误: 无效的IP地址: %s\n", ip); return -1; } sock_info->state = SOCKET_STATE_CONNECTING; if (connect(sock_info->fd, (struct sockaddr*)&sock_info->remote_addr, sizeof(sock_info->remote_addr)) < 0) { if (errno == EINPROGRESS && sock_info->non_blocking) { printf("连接进行中: fd=%d, 地址=%s:%d\n", sock_info->fd, ip, port); return 0; } else { printf("错误: 连接Socket失败: %s\n", strerror(errno)); sock_info->state = SOCKET_STATE_ERROR; return -1; } } sock_info->state = SOCKET_STATE_CONNECTED; sock_info->last_activity = time(NULL); printf("连接成功: fd=%d, 地址=%s:%d\n", sock_info->fd, ip, port); return 0; }
socket_info_t* accept_socket(socket_info_t* listen_sock) { if (!listen_sock || listen_sock->fd < 0 || listen_sock->state != SOCKET_STATE_LISTENING) { return NULL; } struct sockaddr_in client_addr; socklen_t addr_len = sizeof(client_addr); int client_fd = accept(listen_sock->fd, (struct sockaddr*)&client_addr, &addr_len); if (client_fd < 0) { if (errno != EAGAIN && errno != EWOULDBLOCK) { printf("错误: 接受连接失败: %s\n", strerror(errno)); } return NULL; } socket_info_t* client_sock = (socket_info_t*)malloc(sizeof(socket_info_t)); if (!client_sock) { close(client_fd); return NULL; } memset(client_sock, 0, sizeof(socket_info_t)); client_sock->fd = client_fd; client_sock->type = SOCKET_TYPE_TCP; client_sock->state = SOCKET_STATE_CONNECTED; client_sock->local_addr = listen_sock->local_addr; client_sock->remote_addr = client_addr; client_sock->create_time = time(NULL); client_sock->last_activity = client_sock->create_time; char client_ip[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &client_addr.sin_addr, client_ip, sizeof(client_ip)); printf("接受连接: fd=%d, 客户端=%s:%d\n", client_fd, client_ip, ntohs(client_addr.sin_port)); return client_sock; }
ssize_t send_data(socket_info_t* sock_info, const void* data, size_t len, int flags) { if (!sock_info || sock_info->fd < 0 || !data) { return -1; } ssize_t sent = send(sock_info->fd, data, len, flags); if (sent < 0) { if (errno != EAGAIN && errno != EWOULDBLOCK) { printf("错误: 发送数据失败: %s\n", strerror(errno)); sock_info->state = SOCKET_STATE_ERROR; } } else { sock_info->last_activity = time(NULL); printf("发送数据: fd=%d, 长度=%zd/%zu\n", sock_info->fd, sent, len); } return sent; }
ssize_t recv_data(socket_info_t* sock_info, void* buffer, size_t len, int flags) { if (!sock_info || sock_info->fd < 0 || !buffer) { return -1; } ssize_t received = recv(sock_info->fd, buffer, len, flags); if (received < 0) { if (errno != EAGAIN && errno != EWOULDBLOCK) { printf("错误: 接收数据失败: %s\n", strerror(errno)); sock_info->state = SOCKET_STATE_ERROR; } } else if (received == 0) { printf("连接关闭: fd=%d\n", sock_info->fd); sock_info->state = SOCKET_STATE_CLOSED; } else { sock_info->last_activity = time(NULL); printf("接收数据: fd=%d, 长度=%zd\n", sock_info->fd, received); } return received; }
ssize_t sendto_data(socket_info_t* sock_info, const void* data, size_t len, const char* ip, int port) { if (!sock_info || sock_info->fd < 0 || !data) { return -1; } struct sockaddr_in dest_addr; dest_addr.sin_family = AF_INET; dest_addr.sin_port = htons(port); if (inet_pton(AF_INET, ip, &dest_addr.sin_addr) <= 0) { printf("错误: 无效的IP地址: %s\n", ip); return -1; } ssize_t sent = sendto(sock_info->fd, data, len, 0, (struct sockaddr*)&dest_addr, sizeof(dest_addr)); if (sent < 0) { printf("错误: UDP发送失败: %s\n", strerror(errno)); } else { sock_info->last_activity = time(NULL); printf("UDP发送: fd=%d, 目标=%s:%d, 长度=%zd\n", sock_info->fd, ip, port, sent); } return sent; }
ssize_t recvfrom_data(socket_info_t* sock_info, void* buffer, size_t len, char* src_ip, int* src_port) { if (!sock_info || sock_info->fd < 0 || !buffer) { return -1; } struct sockaddr_in src_addr; socklen_t addr_len = sizeof(src_addr); ssize_t received = recvfrom(sock_info->fd, buffer, len, 0, (struct sockaddr*)&src_addr, &addr_len); if (received < 0) { if (errno != EAGAIN && errno != EWOULDBLOCK) { printf("错误: UDP接收失败: %s\n", strerror(errno)); } } else { sock_info->last_activity = time(NULL); if (src_ip) { inet_ntop(AF_INET, &src_addr.sin_addr, src_ip, INET_ADDRSTRLEN); } if (src_port) { *src_port = ntohs(src_addr.sin_port); } printf("UDP接收: fd=%d, 来源=%s:%d, 长度=%zd\n", sock_info->fd, src_ip ? src_ip : "unknown", src_port ? *src_port : 0, received); } return received; }
void close_socket(socket_info_t* sock_info) { if (!sock_info) { return; } if (sock_info->fd >= 0) { close(sock_info->fd); printf("关闭Socket: fd=%d\n", sock_info->fd); } free(sock_info); }
void print_socket_info(const socket_info_t* sock_info) { if (!sock_info) { return; } char local_ip[INET_ADDRSTRLEN]; char remote_ip[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &sock_info->local_addr.sin_addr, local_ip, sizeof(local_ip)); inet_ntop(AF_INET, &sock_info->remote_addr.sin_addr, remote_ip, sizeof(remote_ip)); printf("Socket信息:\n"); printf(" 文件描述符: %d\n", sock_info->fd); printf(" 类型: %s\n", (sock_info->type == SOCKET_TYPE_TCP) ? "TCP" : "UDP"); printf(" 状态: %d\n", sock_info->state); printf(" 本地地址: %s:%d\n", local_ip, ntohs(sock_info->local_addr.sin_port)); printf(" 远程地址: %s:%d\n", remote_ip, ntohs(sock_info->remote_addr.sin_port)); printf(" 非阻塞: %s\n", sock_info->non_blocking ? "是" : "否"); printf(" 地址重用: %s\n", sock_info->reuse_addr ? "是" : "否"); printf(" 保持连接: %s\n", sock_info->keep_alive ? "是" : "否"); printf(" 发送缓冲区: %d\n", sock_info->send_buffer_size); printf(" 接收缓冲区: %d\n", sock_info->recv_buffer_size); printf(" 创建时间: %ld\n", sock_info->create_time); printf(" 最后活动: %ld\n", sock_info->last_activity); }
|