how would I go about creating a Socket receiveAll method?

Unazed Spectaculum unazed at spec.org
Tue Dec 12 21:37:43 UTC 2017


On Tuesday, 12 December 2017 at 21:14:30 UTC, Adam D. Ruppe wrote:
> On Tuesday, 12 December 2017 at 21:03:54 UTC, Unazed 
> Spectaculum wrote:
>> I've decided to go for the run-time approach to this, it works 
>> fine with all of my tests so you have my greatest gratitude.
>
> the way I'd do it btw is simply:
>
> ubyte[] result;
> ubyte[1024] buffer;
> auto got = socket.receive(buffer[]);
> while(got > 0) {
>    result ~= buffer[0 .. got];
> }
>
> if(got < 0)
>   throw new Exception(lastSocketError());
>
> return result;
>
>
> so it uses the one static buffer to receive the stuff one block 
> at a time but just copies it over to the dynamic array with the 
> ~= operator

Oh, thanks; it's definitely shorter however I do enjoy the 
ability to generalize/ambiguate functions by providing optional 
parameters, however thanks for showing another way; I enjoy 
knowing multiple ways of performing one task so anything helps :)


More information about the Digitalmars-d-learn mailing list