#How to read buffer
1 messages · Page 1 of 1 (latest)
std.mem.readInt(u16, buf[0..2], .little);
const net = std.net;
const print = std.debug.print;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();
defer _ = gpa.deinit();
const addr = try net.Ip4Address.parse("127.0.0.1", 7777);
const local = net.Address{ .in = addr };
var sv = try local.listen(.{});
while (sv.accept()) |conn| {
const message = try conn.stream.reader().readAllAlloc(alloc, std.math.maxInt(u8));
const packetlen: u16 = std.mem.readInt(u16, message[0..2], .little);
print("packet is read {}", .{packetlen});
} else |err| {
print("error found {}", .{err});
}
}
When I use readallalloc nothing is received
how about reading from the connection?
conn.stream.reader().readInt(u16, .little);
not sure but readAllAlloc() might be waiting to read more than is available
I solved this problem thanks How to convert buff to utf 8 characters
Depends on the encoding you're converting from
if you just want to turn raw values of some kind into readable text then std.fmt and writer().print are your friend