#How to read buffer

1 messages · Page 1 of 1 (latest)

pine ocean
#

For example, I want to cast 2 bytes to uint16

ionic vortex
#

std.mem.readInt(u16, buf[0..2], .little);

pine ocean
#
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

vital obsidian
#

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

pine ocean
#

I solved this problem thanks How to convert buff to utf 8 characters

jolly jackal
#

if you just want to turn raw values of some kind into readable text then std.fmt and writer().print are your friend