heyo everyone, this one is coming to zig in hopes of having fun with computers and other beings, learning cool programming stuff, and being not (less) bothered by llms and ai bs.
This one did some ziglings exercises to get into the language and has now started on its first zig project, a collection server for sensor measurements. The sensors will most likely talk with the server using json inside http inside (mutual) tls.
For now this one’s goal is to bind to a socket, to accept connections and read from them. This one is running into issues trying to read from a connection:
const std = @import("std");
const Io = std.Io;
const dprint = std.debug.print;
pub fn main(init: std.process.Init) !void {
// In order to do I/O operations need an `Io` instance.
const io = init.io;
const address = try std.Io.net.IpAddress.parse("::1", 1312);
dprint("addr: {}\n", .{address});
var listener = try address.listen(io, .{
.reuse_address = true,
});
defer listener.deinit(io);
var buffer: [1024]u8 = undefined;
while (true) {
const stream = try listener.accept(io);
dprint("accepted connection from: {}\n", .{stream.socket.address});
const reader = stream.reader(io, &buffer);
var ri = reader.interface;
if (ri.fillMore()) {
dprint("possibly filled the buffer", .{});
} else |err| {
dprint("failed to fill the buffer because: {any}", .{err});
}
// if (ri.takeDelimiterInclusive('\n')) |line| {
// dprint("line: {s}", .{line});
// } else |err| {
// dprint("error: {}", .{err});
// }
try stream.shutdown(io, .both);
}
}
When run this code produces this output and segfaults when getting a connection (from netcat):
addr: .{ .ip6 = .{ .port = 1312, .bytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, .flow = 0, .interface = .{ .index = 0 } } }
accepted connection from: .{ .ip6 = .{ .port = 40616, .bytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, .flow = 0, .interface = .{ .index = 0 } } }
Segmentation fault at address 0x79
/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/Io.zig:554:29: 0x103e393 in operate (std.zig)
return io.vtable.operate(io.userdata, operation);
^
/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/Io/net.zig:1275:31: 0x11f3118 in read (std.zig)
return (try io.operate(.{ .net_read = .{
^
/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/Io/net.zig:1329:36: 0x11f2b85 in readVec (std.zig)
const n = r.stream.read(io, dest) catch |err| {
^
/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/Io/Reader.zig:1138:29: 0x108422b in fillMore (std.zig)
_ = try r.vtable.readVec(r, &bufs);
^
/home/digital/projects/comfy-aggregator/src/main.zig:26:24: 0x11e532f in main (main.zig)
if (ri.fillMore()) {
^
/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/start.zig:797:30: 0x11e5f98 in callMain (std.zig)
return wrapMain(root.main(.{
^
/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/start.zig:218:5: 0x11e4ca1 in _start (std.zig)
asm volatile (switch (native_arch) {
^
fish: Job 1, './zig-out/bin/comfy_aggregator …' terminated by signal SIGABRT (Abort)
This one is using zig 0.17.0-dev.1786+75044cb04 (because that’s what ziglings asked for).
Please help this one by teaching it how to find a solution for its problem or explain why this is failing.