feat: try add socket

This commit is contained in:
2025-12-13 19:19:53 +01:00
parent 07b82994ff
commit 79e902eedd
2 changed files with 27 additions and 10 deletions

View File

@@ -1,18 +1,13 @@
const std = @import("std"); const std = @import("std");
const x = @import("protocol.zig"); const protocol = @import("protocol.zig");
const socket = @import("socket.zig");
pub fn main() !void { pub fn main() !void {
std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); const alloc = std.heap.GeneralPurposeAllocator(.{}){};
socket.listen(alloc);
} }
test { test {
_ = @import("protocol.zig"); _ = @import("protocol.zig");
} _ = @import("socket.zig");
test "simple test" {
const gpa = std.testing.allocator;
var list: std.ArrayList(i32) = .empty;
defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
try list.append(gpa, 42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
} }

22
src/socket.zig Normal file
View File

@@ -0,0 +1,22 @@
const std = @import("std");
pub fn listen(alloc: std.mem.Allocator) !void {
const addr = std.net.Address.initIp4(.{ 0, 0, 0, 0 }, 3667);
var server = try addr.listen(.{});
std.log.info("Server listening on port 3667", .{});
var client = try server.accept();
defer client.stream.close();
const client_reader = client.stream.reader();
const client_writer = client.stream.writer();
while (true) {
const msg = try client_reader.readUntilDelimiterOrEofAlloc(alloc, '\n', 65536) orelse break;
defer alloc.free(msg);
std.log.info("Recieved message: \"{}\"", .{std.zig.fmtEscapes(msg)});
try client_writer.writeAll(msg);
}
}