Guest User

http_server

a guest
Aug 29th, 2025
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.71 KB | None | 0 0
  1. const std = @import("std");
  2. const net = std.net;
  3. const Address = net.Address;
  4.  
  5. pub fn main() !void {
  6.     const address = Address.initIp4(.{ 127, 0, 0, 1 }, 8080);
  7.     var server = try address.listen(.{});
  8.     defer server.deinit();
  9.  
  10.     var buffer: [1024]u8 = undefined;
  11.  
  12.     while (true) {
  13.         const connection = server.accept() catch |err| {
  14.             std.debug.print("Error accepting connection: {}", .{err});
  15.             continue;
  16.         };
  17.  
  18.         const connection_writer = connection.stream.writer(&buffer);
  19.  
  20.         var writer = connection_writer.interface;
  21.  
  22.         try writer.print("Hello from Server\n", .{});
  23.         try writer.flush();
  24.         connection.stream.close();
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment