Reading data from the network without knowing the size of the buffer that is coming

Andre Pany andre at s-e-a-p.de
Thu Mar 21 18:27:56 UTC 2019


On Thursday, 21 March 2019 at 16:54:01 UTC, Roman Sztergbaum 
wrote:
> Hello !
>
> in my function :
>
> ```D
>   private config_load_answer load_config(string[] args)
>     in(args !is null, "args cannot be null")
>     in(args.length == 2, "need 1 arguments")
>     out(r; r.state == "SUCCESS", "load_config should succeed")
>     out(r; !r.config_name.empty, "config_name should not be 
> empty")
>     out(r; r.config_id > 0, "config_id should be different than 
> 0")
>     {
>         string config_key;
>         string readonly_config_key;
>         getopt(args, "key", &config_key, "readonly_key", 
> &readonly_config_key);
>         auto cfg = 
> deserialize_to!config_load("../API_doc/json_recipes/config_load.json");
>         cfg.config_key = config_key;
>         cfg.readonly_config_key = readonly_config_key;
>         writeln(cfg);
>         writeln(cfg.serializeToJsonPretty);
>         client_.socket.send(cfg.serializeToJson);
>         auto answer = new ubyte[256]; //
>         client_.socket.receive(answer);
>         (cast(string) answer).writeln;
>         return (cast(string) 
> answer).deserialize!config_load_answer;
>     }
> ```
>
> I would like to get rid of the "ubytes[256]" because I do not 
> know the size of the data that is comming, I would like to read 
> the entire buffer that I send at once. Can someone point me?
>
> (i'm pretty new to D langage, but i'm familiar with C++, and 
> for example in boost asio  we have `byte_transfered` so we can 
> create a buffer with this size and read the incoming data)
>
> Thank's a lot for your help !

I am also not a  expert in this area. If the message received 
from the server exceeds 256 bytes, you have to call the receive 
message several times until you know you received all bytes.

If the data received is smaller than 256 bytes, the array will 
contain NULL (character 0 value in the ascii table). This you 
might could use as indicator you received all values.

Is the server really without protocol? If the server code is 
under your control, you could switch to http, which has a message 
length attribute.

Ps. In case there is a network error, your program will terminate 
(out contract throws Error) without any chance to avoid this. You 
should throw an Exception at the end of method body instead for 
resource issues.

Kind regards
Andre


More information about the Digitalmars-d-learn mailing list