zSockets is the Zig port of µSockets, which implements a highly optimized non-blocking, thread-per-CPU core networking library. zSockets provides a unified API, allowing developers to write code once that targets multiple event backends, platforms, and all supported networking protocols.
If you haven't already done so, initialize a zig project:
zig initInstall zSockets as a dependency to build.zig.zon:
zig fetch --save git+https://github.com/cryptodeal/zSocketsAdd zSockets to your build.zig:
exe.root_module.addImport("zSockets", b.dependency("zSockets", .{ .target=target, .optimize=optimize }).module("zSockets"));Import/call into zSockets from your codebase:
constzs=@import("zSockets");When using TCP/UDP (no TLS) and one of the default event backends, the library has zero dependencies and can be compiled to a tiny binary. The following details how to configure to use a specific backend, enable TLS, or support QUIC & HTTP3.
| Backend | Platforms | Is Default | Manually Configure |
|---|---|---|---|
| epoll | Linux | Yes | -DWITH_EPOLL |
| kqueue | Darwin/FreeBSD | Yes | -DWITH_KQUEUE |
| libuv | supported platforms | No | -DWITH_LIBUV |
| GCD | Darwin | No | -DWITH_GCD |
| TLS Library | Manually Configure |
|---|---|
| boringssl (preferred) | -DWITH_BORINGSSL |
| openssl | -DWITH_OPENSSL |
| Library | Manually Configure |
|---|---|
| lsquic | -DWITH_QUIC |
Clone the repo:
git clone https://github.com/cryptodeal/zSockets.gitTo run with TLS, generate certs using the misc/gen_test_certs.sh
bash misc/gen_test_certs.sh <output path>Modify the examples to point to the relevant certs and call the example using either -DWITH_BORINGSSL (preferred) or -DWITH_OPENSSL
zig build echo_serverCalls to localhost:3000 will logged to console.
zig build hammer_test_unixzig build hammer_testRun the HTTP Server:
zig build http_serverRun the HTTP Load Test (see examples/http_load_test.zig for info on customizing options passed):
zig build http_load_testRun the HTTP3 Server:
zig build http3_server -DWITH_BORINGSSL -DWITH_QUICRun the HTTP3 Client:
zig build http3_client -DWITH_BORINGSSL -DWITH_QUICEnsure that you've generated the relevant TLS certs and that examples/peer_verify_test.zig has been modified to point to the correct path for each cert.
Run Peer Verify Test:
zig build peer_verify_test -DWITH_BORINGSSLRun the TCP Server:
zig build tcp_serverRun the TCP Load Test (see examples/tcp_load_test.zig for info on customizing options passed):
zig build tcp_load_testRun the UDP Benchmark Server IPv4:
zig build udp_benchmarkRun the UDP Benchmark Client IPv4:
zig build udp_benchmark -- --type=clientRun the UDP Benchmark Server IPv6:
zig build udp_benchmark -- --protocol=ipv6Run the UDP Benchmark Client IPv6:
zig build udp_benchmark -- --type=client --protocol=ipv6- Unit tests
- Windows Support
io_uringbackend- Improve Quic/HTTP3 implementation