Search code examples
delphiftpindy

How do I send a TMemoryStream using Indy's IdTrivialFTP?


I am using Delphi 2010 and Indy 10 that ships with it. The MemStream is a TMemoryStream and it contains a file downloaded from a website.

IdTrivialFTP1 := TIdTrivialFTP.Create(nil);
try
  IdTrivialFtp1.Name := 'IdTrivialFTP1';
  IdTrivialFTP1.Host := 'my-tftp-server-name.contoso.com';
  IdTrivialFTP1.Port := 69;
  IdTrivialFTP1.ReceiveTimeout := 4000;

  MemStream.Position := 0;
  IdTrivialFTP1.Put(MemStream, sFileName);
finally
  FreeAndNil(IdTrivialFTP1);
end;

Whenever I run this code, I get the error:

Project TestProject.exe raised exception class ERangeError with message 'Range check error'.

I've been doing quite a bit of google-ing to understand this error, but I'm currently at a loss. I've also seen only 1 example of how to perform a TFTP PUT using Indy components.

Any ideas?

UPDATE: If I trace into the error a bit, I find that the error is being thrown from within "IdTrivialFTP.pas" on line 272:

    CurrentDataBlk := WordToStr(GStack.HostToNetwork(Word(TFTP_DATA)))
+ WordToStr(GStack.HostToNetwork(BlockCtr));

For clarity, here is the context of that code:

if BlockCtr > PrevBlockCtr then
  begin
    DataLen := IndyMin(BufferSize - hdrsize, SourceStream.Size - SourceStream.Position);
    SetLength(CurrentDataBlk, DataLen + hdrsize);
    CurrentDataBlk := WordToStr(GStack.HostToNetwork(Word(TFTP_DATA))) + WordToStr(GStack.HostToNetwork(BlockCtr));
    SetLength(CurrentDataBlk, DataLen + hdrsize);
    //SourceStream.ReadBuffer(CurrentDataBlk[hdrsize+1], DataLen);
    DoWork(wmWrite, DataLen);
    TerminateTransfer := DataLen < BufferSize - hdrsize;
    PrevBlockCtr := BlockCtr;
  end;
  Send(FPeerIP, FPeerPort, CurrentDataBlk);
until False;  { repeat }

Solution

  • The version of Indy 10 that shipped with D2010 is NOT the latest Indy 10 revision that is available. The Indy code you have quoted as being broken does not exist in the current version anymore. TIdTrivialFTP was re-written awhile back ago to fix a lot of issues (I forgot to check it in at the time, though. I have done so now). Please download the latest Indy 10 snapshot from Indy's SVN server or Fulgan mirror (you will have to wait a day for it to catch up). Download links are available on Indy's website.