From 058a77eabd6d47e3180360b28b8775a7c19eb064 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Sun, 30 Jun 2024 20:07:46 +0300 Subject: naive socket server --- src/main.zig | 5 +++++ src/socket-server.zig | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.zig b/src/main.zig index b46023e..e5e4099 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,5 +1,6 @@ const std = @import("std"); const ls = @import("ls.zig"); +const socket_server = @import("socket-server.zig"); pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; @@ -18,6 +19,10 @@ pub fn main() !void { return; } + if (args.len == 2 and std.mem.eql(u8, args[1], "tcp-foo")) { + try socket_server.start_server(); + } + std.debug.print("Unknown command. Use 'help' for usage information.\n", .{}); print_help(); } diff --git a/src/socket-server.zig b/src/socket-server.zig index 01c5790..b77c4d5 100644 --- a/src/socket-server.zig +++ b/src/socket-server.zig @@ -1,3 +1,22 @@ const std = @import("std"); +const print = std.debug.print; +const net = std.net; -pub fn start_server() !void {} +pub fn start_server() !void { + print("Starting server\n", .{}); + + const ip = [_]u8{ 127, 0, 0, 1 }; + const port = 9000; + + const addr = net.Address.initIp4(ip, port); + + var server = try net.Address.listen(addr, .{}); + print("Listening at {}\n", .{server.listen_address}); + defer server.deinit(); + + while (true) { + var connection = try server.accept(); + _ = try connection.stream.write("Hello, client\n"); + defer connection.stream.close(); + } +} -- cgit 1.4.1-2-gfad0