Jump to content
JIMSMITH

Indy Readbytes with timeout

Recommended Posts

I use the indy tidtcpserver with the readbytes method.  The code is ReadBytes(Buffer,buffsize, False).  The buffsize is set for 1024.  I receive varying amounts of data say 7000 bytes.  After 6 passes I receive an timeout because ( after the first 6 *1024 = 6144 the last pass only has 7000 - 6144 = 856) only 856 bytes remain and the timeout occurs waiting to fill the buffer.  When the timeout happens the bytes appear to remain in the iohandler buffer.  The question is what is the best way to get the 6 chunks and the remaining 856 bytes without generating an exception?  I prefer to use readbytes because ther could be different types of data other than strings.  I hope this make sense and I appreciate assistance in advance.

Share this post


Link to post

Calling ReadBytes() with an AByteCount value greater than 0 will wait until the specified number of bytes have arrived in the InputBuffer. So, if you are expecting 7000 bytes then just call ReadBytes() 1 time with AByteCount set to 7000.

 

But, if you don't know ahead of time how many bytes are coming, then you can call ReadBytes() with an AByteCount value less than 0 and it will return however many bytes are currently available in the InputBuffer (if it is empty, ReadBytes() will wait until any bytes arrive first).

 

If you don't want to change your current logic, then you can simply catch the EIdReadTimeout exception and call ReadBytes() again with the AByteCount value set to the IOHandler's current InputBuffer.Size.

Edited by Remy Lebeau

Share this post


Link to post
1 hour ago, Remy Lebeau said:

Calling ReadBytes() with an AByteCount value greater than 0 will wait until the specified number of bytes have arrived in the InputBuffer. So, if you are expecting 7000 bytes then just call ReadBytes() 1 time with AByteCount set to 7000.

 

But, if you don't know ahead of time how many bytes are coming, then you can call ReadBytes() with an AByteCount value less than 0 and it will return however many bytes are currently available in the InputBuffer (if it is empty, ReadBytes() will wait until any bytes arrive first).

 

If you don't want to change your current logic, then you can simply catch the EIdReadTimeout exception and call ReadBytes() again with the AByteCount value set to the IOHandler's current InputBuffer.Size.

Damn!  You are awesome.

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
×