#network byte order

1 messages · Page 1 of 1 (latest)

midnight wave
#

I'm writing an epoll server in zig, and the client with C. I try to send a u16 in big endian but when I receive it on the client side and use ntohs on it the value is different.

Server: (22), Client: (5632)

std.mem.writeInt(u16, &len_bytes, self.csum.len, .big);
fringe swan
#

so self.csum.len is 22?

midnight wave
#

Yes

fringe swan
#

you could inspect len_bytes on the zig side to narrow this down, i guess it should be .{ 0x00, 0x16 }

midnight wave
#

Yes it is

#

Bytes: { 0, 16 }

fringe swan
#

hm

#

it definitely seems correct to use both std.mem.writeInt with .big in zig, and ntohs in C

#

i wonder if maybe in either the server or the client you're accidentally byte swapping it twice?

elfin swift
#

or inadvertently shifting it by reading from the wrong offset, like if the stream is { 0, 16, 0 } it could look like { 0, 16 } or { 16, 0 }