Skip to content

Instantly share code, notes, and snippets.

@hholst80
Created December 28, 2023 00:11
Show Gist options
  • Save hholst80/29a05647670d2d2ae38b4dd2a349ed48 to your computer and use it in GitHub Desktop.
Save hholst80/29a05647670d2d2ae38b4dd2a349ed48 to your computer and use it in GitHub Desktop.
const std = @import("std");
pub fn main() !void {
// const allocator = std.heap.page_allocator;
const listen_address = try std.net.Address.parseIp4("127.0.0.1", 1337);
var socket = std.net.StreamServer.init(.{});
defer socket.deinit();
// Start listening for connections
try socket.listen(listen_address);
std.log.info("Server listening on {}", .{listen_address});
// Accept a connection
while (true) {
const connection = try socket.accept();
std.log.info("Received connection from {}", .{connection.address});
defer connection.stream.close();
var buf: [4096]u8 = undefined;
var parser = std.http.protocol.HeadersParser.initStatic(&buf);
try parser.read(connection, undefined, true);
// You can now read from and write to `connection.stream`
// connection.stream.close(); // Remember to close each connection
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment