成熟度提示:本库功能完善且经过充分测试,但为新近发布。适用于开发、内部工具及受控环境。在代码库积累数月实战加固之前,不建议在涉及不可信网络输入的生产环境中部署。
高性能、异常驱动的 POSIX 系统套接字工具包。提供简洁现代的 C API,支持 TCP、UDP、Unix 域套接字、HTTP/1.1、HTTP/2、QUIC、WebSocket 及 TLS/DTLS,具备全面的错误处理、零拷贝 I/O 和跨平台事件轮询。
TRY/EXCEPT/FINALLY 实现干净的错误传播sendfile() 和分散/聚集 I/O| Feature | Linux | BSD/macOS | 回退 |
|---|---|---|---|
| 事件轮询 | epoll | kqueue | poll(2) |
| 异步 I/O | io_uring (5.1+) | kqueue AIO | 边缘触发 |
| TCP Fast Open | 3.7+ | 10.0+/10.11+ | 禁用 |
| 拥塞控制 | 可配置 | - | - |
| 对等凭据 | SO_PEERCRED | LOCAL_PEERCRED | - |
| SYN 防护 | TCP_DEFER_ACCEPT | SO_ACCEPTFILTER | 用户空间 |
cmake -S . -B build cmake --build build -j
cd build && ctest --output-on-failure
cmake -S . -B build -DENABLE_TLS=ON
cmake -S . -B build -DENABLE_SANITIZERS=ON
cmake -S . -B build -DENABLE_FUZZING=ON -DCMAKE_C_COMPILER=clang
### 基本TCP服务器```c
#include "socket/Socket.h"
#include "core/Except.h"
#include <stdio.h>
int main(void)
{
Socket_T server = Socket_new(AF_INET, SOCK_STREAM, 0);
TRY
Socket_setreuseaddr(server);
Socket_bind(server, NULL, 8080);
Socket_listen(server, 128);
printf("Server listening on port 8080...\n");
while (1) {
Socket_T client = Socket_accept(server);
if (client) {
char buf[1024];
ssize_t n = Socket_recv(client, buf, sizeof(buf) - 1);
if (n > 0) {
buf[n] = '\0';
Socket_sendall(client, buf, n);
}
Socket_free(&client);
}
}
EXCEPT(Socket_Failed)
fprintf(stderr, "Error: %s\n", Socket_GetLastError());
END_TRY;
Socket_free(&server);
return 0;
}
#include "socket/Socket.h" #include "core/Except.h" #include <stdio.h> #include <string.h>
int main(void) { Socket_T client = Socket_new(AF_INET, SOCK_STREAM, 0);
TRY
Socket_connect(client, "127.0.0.1", 8080);
printf("Connected to server\n");
const char *msg = "Hello, Server!";
Socket_sendall(client, msg, strlen(msg));
char buf[1024];
ssize_t n = Socket_recvall(client, buf, strlen(msg));
buf[n] = '\0';
printf("Received: %s\n", buf);
EXCEPT(Socket_Failed)
fprintf(stderr, "Error: %s\n", Socket_GetLastError());
EXCEPT(Socket_Closed)
fprintf(stderr, "Connection closed\n");
END_TRY;
Socket_free(&client);
return 0;
}
### Convenience Functions (One-Call Setup)
对于常见用例,便利函数简化了套接字设置:```c
#include "socket/Socket.h"
#include "socket/SocketDgram.h"
/* TCP Server - one call creates listening socket */
Socket_T server = Socket_listen_tcp("0.0.0.0", 8080, 128);
while (running) {
Socket_T client = Socket_accept_timeout(server, 1000); // 1s timeout
if (client) handle_client(client);
}
Socket_free(&server);
/* TCP Client with timeout - one call connects */
Socket_T client = Socket_connect_tcp("api.example.com", 443, 5000); // 5s timeout
Socket_sendall(client, request, len);
Socket_free(&client);
/* UDP Server - one call binds */
SocketDgram_T udp = SocketDgram_bind_udp("0.0.0.0", 5353);
SocketDgram_recvfrom(udp, buf, sizeof(buf), sender_ip, sizeof(sender_ip), &sender_port);
SocketDgram_free(&udp);
/* Unix Domain Server */
Socket_T unix_srv = Socket_listen_unix("/var/run/app.sock", 128);
Socket_free(&unix_srv);
/* Unix Domain Client with timeout */
Socket_T unix_cli = Socket_new(AF_UNIX, SOCK_STREAM, 0);
Socket_connect_unix_timeout(unix_cli, "/var/run/app.sock", 5000);
Socket_free(&unix_cli);
/* Non-blocking connect (for event loops) */
Socket_T sock = Socket_new(AF_INET, SOCK_STREAM, 0);
int status = Socket_connect_nonblocking(sock, "192.168.1.1", 8080);
if (status == 1) {
/* In progress - poll for POLL_WRITE then check Socket_isconnected() */
}
对于偏好基于返回码的错误处理的用户,简单 API 提供了一个便利层,封装了基于异常的底层实现:```c #include "simple/SocketSimple.h"