Receiving data into a type using sockets
David Nadlinger
see at klickverbot.at
Sun Jun 5 12:55:54 PDT 2011
On 6/5/11 9:49 PM, Jonathan Sternberg wrote:
> Cool. It compiles, but it doesn't seem to be doing exactly what I want. Say I send
> 2 integers from the server to the client. When I do this on the client, it seems
> to do the wrong thing.
>
> int first, second;
> auto sock = new TcpSocket();
> sock.connect(new InternetAddress("localhost", 10000));
>
> writeln( sock.receive((&first)[0..int.sizeof]) );
> writeln( sock.receive((&second)[0..int.sizeof] );
>
> This seems to print 8 and then block on the second call to receive. I thought that
> D was supposed to recognize that the array was only 4 bytes long and read only
> that much. (note: on a 32-bit machine, so int comes out to 4 bytes)
>
> When I do:
>
> writeln( (&first)[0..int.sizeof].length );
>
> It prints 4 as it's supposed to.
&first is of type int*, so (&first)[0..int.sizeof] returns a slice
pointing to int.sizeof (i.e. 4) ints, not a single one as you intend to.
Just use »(&first)[0..1]« as per my original reply, and you should be fine.
David
More information about the Digitalmars-d-learn
mailing list