-
Notifications
You must be signed in to change notification settings - Fork 47
feat: adopt with Zig 0.15 #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||
| //! Start a TCP server at an unused port. | ||||||
| //! Start an echo TCP server at an unused port. | ||||||
| //! | ||||||
| //! Test with | ||||||
| //! echo "hello zig" | nc localhost <port> | ||||||
|
|
@@ -8,10 +8,6 @@ const net = std.net; | |||||
| const print = std.debug.print; | ||||||
|
|
||||||
| pub fn main() !void { | ||||||
| var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||||||
| defer _ = gpa.deinit(); | ||||||
| const allocator = gpa.allocator(); | ||||||
|
|
||||||
| const loopback = try net.Ip4Address.parse("127.0.0.1", 0); | ||||||
| const localhost = net.Address{ .in = loopback }; | ||||||
| var server = try localhost.listen(.{ | ||||||
|
|
@@ -22,13 +18,31 @@ pub fn main() !void { | |||||
| const addr = server.listen_address; | ||||||
| print("Listening on {}, access this port to end the program\n", .{addr.getPort()}); | ||||||
|
|
||||||
| var client = try server.accept(); | ||||||
| defer client.stream.close(); | ||||||
|
|
||||||
| print("Connection received! {} is sending data.\n", .{client.address}); | ||||||
|
|
||||||
| const message = try client.stream.reader().readAllAlloc(allocator, 1024); | ||||||
| defer allocator.free(message); | ||||||
| const client = try server.accept(); | ||||||
| // In real world, you'd want to handle multiple clients, probably in separate threads. | ||||||
| try handleClient(client); | ||||||
| } | ||||||
|
|
||||||
| print("{} says {s}\n", .{ client.address, message }); | ||||||
| fn handleClient(client: net.Server.Connection) !void { | ||||||
| print("Accepted connection from {f}\n", .{client.address}); | ||||||
| defer client.stream.close(); | ||||||
| var stream_buf: [1024]u8 = undefined; | ||||||
| var reader = client.stream.reader(&stream_buf); | ||||||
| // Here we echo back what we read directly, so the writer buffer is empty | ||||||
| var writer = client.stream.writer(&.{}); | ||||||
|
|
||||||
| while (true) { | ||||||
| print("Waiting for data from {f}...\n", .{client.address}); | ||||||
|
||||||
| const msg = reader.interface().takeDelimiterInclusive('\n') catch |err| { | ||||||
| if (err == error.EndOfStream) { | ||||||
| print("{f} closed the connection\n", .{client.address}); | ||||||
|
||||||
| print("{f} closed the connection\n", .{client.address}); | |
| print("{any} closed the connection\n", .{client.address}); |
Copilot
AI
Sep 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The format specifier {f} is for floating point numbers, but client.address is a network address. This should be {any} to properly format the address.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -16,13 +16,13 @@ pub fn main() !void { | |||||
| // Connect to peer | ||||||
| const stream = try net.tcpConnectToAddress(peer); | ||||||
| defer stream.close(); | ||||||
| print("Connecting to {}\n", .{peer}); | ||||||
| print("Connecting to {f}\n", .{peer}); | ||||||
|
||||||
| print("Connecting to {f}\n", .{peer}); | |
| print("Connecting to {any}\n", .{peer}); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -30,7 +30,7 @@ pub fn main() !void { | |||||
|
|
||||||
| var buf: [1024]u8 = undefined; | ||||||
|
|
||||||
| print("Listen on {any}...\n", .{addr}); | ||||||
| print("Listen on {f}\n", .{addr}); | ||||||
|
||||||
| print("Listen on {f}\n", .{addr}); | |
| print("Listen on {any}\n", .{addr}); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,6 +1,5 @@ | ||||||
| const std = @import("std"); | ||||||
| const log = std.log; | ||||||
| const WebSocket = std.http.WebSocket; | ||||||
| const Request = std.http.Server.Request; | ||||||
| const Connection = std.net.Server.Connection; | ||||||
|
|
||||||
|
|
@@ -11,7 +10,7 @@ pub fn main() !void { | |||||
| var server = try std.net.Address.listen(addr, .{ .reuse_address = true }); | ||||||
| defer server.deinit(); | ||||||
|
|
||||||
| log.info("Start HTTP server at {any}", .{addr}); | ||||||
| log.info("Start HTTP server at {f}", .{addr}); | ||||||
|
||||||
| log.info("Start HTTP server at {f}", .{addr}); | |
| log.info("Start HTTP server at {any}", .{addr}); |
Copilot
AI
Sep 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The format specifier {f} is for floating point numbers, but conn.address is a network address. This should be {any} to properly format the address.
| log.info("Got new client({f})!", .{conn.address}); | |
| log.info("Got new client({any})!", .{conn.address}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The format specifier
{f}is for floating point numbers, butclient.addressis a network address. This should be{any}to properly format the address.