[En-Nut-Discussion] Industrial interfaces (Modbus TCP, Profinet, etc.)
nategoose at gmail.com
Thu Jul 17 20:48:57 CEST 2014
On Thu, Jul 17, 2014 at 1:59 PM, Thiago A. Corrêa <thiago.correa at gmail.com>
> Please notice the API returns the number of actually received bytes.
> The size is a maximum buffer size, not a minimum or required read
> size. That's a fairly common bug, to expect reads to return with the
> "requested" amount of bytes. Sometimes it does, sometimes it doesn't.
> It's your job to loop thru receive calls until you get the amount of
> bytes you want, something along:
> int rcvCount = 0;
> while( rcvCount < sizeof(mbap_header) )
> rcvCount += receive( socket, &mbap_header + rcvCount,
> sizeof(mbap_header) - rcvCount );
True, but it's actually still more complicated because receive can return -1
which isn't a real count and shouldn't be added to rcvCount, but I wanted
the code to be easy to follow.
More information about the En-Nut-Discussion