Receiving data into a type using sockets
Andrew Wiley
wiley.andrew.j at gmail.com
Sun Jun 5 13:09:25 PDT 2011
On Sun, Jun 5, 2011 at 12:55 PM, David Nadlinger <see at klickverbot.at> wrote:
> 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.
>
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.
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20110605/579c7641/attachment.html>
More information about the Digitalmars-d-learn
mailing list