<div class="gmail_quote">On Sun, Jun 5, 2011 at 12:55 PM, David Nadlinger <span dir="ltr"><<a href="mailto:see@klickverbot.at">see@klickverbot.at</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div><div></div><div class="h5">On 6/5/11 9:49 PM, Jonathan Sternberg wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Cool. It compiles, but it doesn't seem to be doing exactly what I want. Say I send<br>
2 integers from the server to the client. When I do this on the client, it seems<br>
to do the wrong thing.<br>
<br>
int first, second;<br>
auto sock = new TcpSocket();<br>
sock.connect(new InternetAddress("localhost", 10000));<br>
<br>
writeln( sock.receive((&first)[0..int.sizeof]) );<br>
writeln( sock.receive((&second)[0..int.sizeof] );<br>
<br>
This seems to print 8 and then block on the second call to receive. I thought that<br>
D was supposed to recognize that the array was only 4 bytes long and read only<br>
that much. (note: on a 32-bit machine, so int comes out to 4 bytes)<br>
<br>
When I do:<br>
<br>
writeln( (&first)[0..int.sizeof].length );<br>
<br>
It prints 4 as it's supposed to.<br>
</blockquote>
<br></div></div>
&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.<br>

</blockquote><div><br></div><div>Also, note that receive is basically a direct call to C's receive, which means that it has the same behavior with regards to buffer filling. Calling sock.receive((&first)[0..1]) returns the number of bytes read, which may be less than 4. I've handled this in the past by writing a template function that takes a primitive type and loops until it has read all the bytes it needs.</div>

<div>Oh, and D's int is always 32 bits, on 32 bit or 64 bit platforms. size_t is an alias that switches from uint on 32 bit to ulong on 64 bit, and there's another alias somewhere for signed types (ssize_t, I believe). The primitive types always have the same sizes until you get into things like cent.</div>

</div><br>