Jump to content
EugeneK

Receiving TBytes

Recommended Posts

Hi

 

We now have method to sending TBytes in socket, can we have the same for receiving?

Share this post


Link to post
var
  buffer: TBytes;
  read: Integer;
Begin
  [...]
  read := Self.Receive(@buffer[0], Length(buffer));

 

Share this post


Link to post

From the notes for ICS V8.70 TWSocket:

 

Added ReceiveTB(var Data : TBytes; MaxLen : Integer = -1) : Integer; where MaxLen is optional, to receive TCP data into a TBytes dynamic array of bytes. Also ReceiveFromTB and ReceiveFrom6TB for UDP datagrams.

 

Receiving into a fixed size buffer is more slightly more efficient, but TBytes has more RTL support for converting to strings, etc.  ReceiveTB will resize the variable up to MaxLen. 

 

The new functions are used in the OverbyteIcsIpStreamLog unit which how they were tested.

 

Angus

 

 

 

 

Share this post


Link to post

@TC

Just remember the streamed data could be broken into different TCP packets at literally any place. So even converting a chunk to a string is not reliable unless you use 1-byte encodings (no, UTF-8 is not one of them). Otherwise you must use some kind of concatenating buffer/stream which makes TBytes application useless

Edited by Fr0sT.Brutal

Share this post


Link to post
9 minutes ago, Fr0sT.Brutal said:

Otherwise you must use some kind of concatenating buffer/stream

I wrote my helper class for this purpose, it can be used and abused as one feels like it:

Maybe it worth to revisit that code, I don't remember what lurks within it's lines.

Share this post


Link to post

Indeed, the OverbyteIcsIpStreamLog unit builds text lines by parsing a TByte buffer one character at a time.  But many quick and dirty programs accept that TCP sends full packets, like my new WebSocket sample. 

 

The other advantage of TByte is avoidance of pointer handling, all those @ and ^ symbols that really have no place in modern applications since they can be abused so easily.  Ditto the Move function.  ICS simply offers alternatives, choose the easiest to use.

 

Angus 

Share this post


Link to post

Hi

 

Can we also have function that returns TBytes? It is more convenient in many cases

Share this post


Link to post
Quote

Can we also have function that returns TBytes? It is more convenient in many cases

This will be in SVN next week, seems simpler but needs an extra line of code to check the TBytes length.

 

Angus

  • Like 1

Share this post


Link to post

Hi

 

I wonder what is the need for MaxLen parameter? There is no such logic in ReceiveStr functions, if there is more data than MaxLen is it discarded or you can receive it in next call?

Share this post


Link to post

While ReceiveStr may not have MaxLen, other similar functions do, so it's backward compatible.  Since it is defaulted, you can ignore it.

 

Angus

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×