Small network abstraction layer around TCP & UDP.
- Implements the minimal API surface for basic networking
- Makes cross-platform abstractions
- Supports blocking and non-blocking I/O via
select/poll - UDP multicast support
build.zig.zon:
.{
.name="appname",
.version="0.0.0",
.dependencies= .{
.network= .{
.url="https://github.com/MasterQ32/zig-network/archive/<COMMIT_HASH_HERE>.tar.gz",
.hash="HASH_GOES_HERE",
},
},
}(To aquire the hash, please remove the line containing .hash, the compiler will then tell you which line to put back)
build.zig:
exe.addModule("network", b.dependency("network", .{}).module("network"));constnetwork=@import("network");
test"Connect to an echo server" {
trynetwork.init();
defernetwork.deinit();
constsock=trynetwork.connectToHost(std.heap.page_allocator, "tcpbin.com", 4242, .tcp);
defersock.close();
constmsg="Hi from socket!\n";
trysock.writer().writeAll(msg);
varbuf: [128]u8=undefined;
std.debug.print("Echo: {}", .{buf[0..trysock.reader().readAll(buf[0..msg.len])]});
}See async.zig for a more complete example on how to use asynchronous I/O to make a small TCP server.
Build all examples:
$ zig build examplesBuild a specific example:
$ zig build sync-examplesTo test an example, eg. echo:
$ ./zig-out/bin/echo 3000in another terminal
$ nc localhost 3000
hello
hello
how are you
how are youOn Windows receive and send function calls are asynchronous and cooperate with the standard library event loop
when io_mode = .evented is set in the root file of your program.
Other calls (connect, listen, accept etc) are blocking.