starruler
starruler

Reputation: 302

D - Read 4 byte size from socket

How can I read a uint from a connected socket directly without having to do any conversion.

Effectively, what is the D equivalent to read(sock, &four_byte_var, 4). I can't seem to convert a uint to anything that the D standard library socket.receive(void[]) will accept.

Upvotes: 1

Views: 205

Answers (2)

Vladimir Panteleev
Vladimir Panteleev

Reputation: 25187

You need to pass a slice that surrounds the variable:

uint n;
socket.receive((&n)[0..1]);

Note that this approach is endian-sensitive.

Upvotes: 3

dav1d
dav1d

Reputation: 6055

It's pretty easy with std.socketstream:

auto stream = new SocketStream(socket);

uint foo;
stream.read(foo);

Note: the std.stream and std.socketstream modules will be replaced by a new interface (well, that might take a while).

Upvotes: 1

Related Questions