Hi everyone,
could someone help me with a sample tcp client code that reads line-by-line. I can only find samples for pre 0.15 and could not manage to migrate to 0.15.
Hi everyone,
could someone help me with a sample tcp client code that reads line-by-line. I can only find samples for pre 0.15 and could not manage to migrate to 0.15.
This is also my first time trying to connect to the server using zig. There might be some poorly written parts, but the functions have been tested and are successful.
var gpa: std.heap.DebugAllocator(.{}) = .init;
const allocator = gpa.allocator();
const stream = try std.net.tcpConnectToHost(allocator, "tcpbin.com", 4242);
defer stream.close();
var rbuffer: [4096]u8 = undefined;
var reader = stream.reader(&rbuffer);
const r: *std.Io.Reader = reader.interface();
var wbuffer: [4096]u8 = undefined;
var writer = stream.writer(&wbuffer);
const w: *std.Io.Writer = &writer.interface;
try w.writeAll("hello\r\nworld\r\n");
try w.flush();
while (try r.takeDelimiter('\n')) |line_maybe_with_cr| {
const line = if (line_maybe_with_cr[line_maybe_with_cr.len - 1] == '\r') line_maybe_with_cr[0 .. line_maybe_with_cr.len - 1] else line_maybe_with_cr;
std.debug.print("{s}\n", .{line});
}
Thank you, this works perfectly!