TheDude
TheDude

Reputation: 3105

(Indy) TIdHTTP EIdSocketError Socket Error # 0 exceptions when downloading files

I have this delphi code that basically download files from a secure server (using Indy build 10.5.8 r4743 if I'm not mistaken):

The problem is: I'm getting random "Socket Error # 0" exceptions that I couldn't get fix or even understand:

Project MyProject.exe raised exception class EIdSocketError with message 'Socket Error # 0

Stack is here, pascal code is:

IdHTTP            := TIdHTTP.Create(nil);
TheSSL            := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
TheCompressor     := TIdCompressorZLib.Create(nil);
TheCookieManager  := TIdCookieManager.Create(IdHTTP);

try SetupWebComponents(IdHTTP, TheSSL, TheCompressor, TheCookieManager); except end;

TheCookieManager.OnNewCookie := MainForm.SyncNewCookie;

// Go!
Stream            := TMemoryStream.Create;
try
   IsError        := False;
   try
      with IdHTTP do
      begin
           OnWork                := MainForm.IdHTTPWork_Download;
           try
              try
                 IsNewFile       := (Not FileExists(LocalFile));
                 if IsNewFile then
                    TheFile      := TFileStream.Create(LocalFile, fmCreate)
                 else
                     // File already exist, resume download
                     TheFile     := TFileStream.Create(LocalFile, fmOpenReadWrite);

                 DoExit          := False;

                 // Use [ Request.Referer ] it to pass VersionGUID to Work event (to record download progress in db)
                 Request.Referer := VersionGUID;
                 repeat
                       // Get resume byte
                       if IsNewFile then
                       begin
                            ResumeByte := 0;
                            IsNewFile  := False;
                       end
                       else
                           ResumeByte  := GetResumeByteFromDB();

                       if FileExists(LocalFile) then
                       begin
                            // File already exist, resume download
                            DoExit     := (ResumeByte >= TheFileSize);
                            TheFile.Seek(ResumeByte, soFromBeginning);
                       end;

                       // Save ResumeByte, it will be used to record download progress in db & eventually use it to resume downloads)
                       IdHTTP.Tag      := ResumeByte;

                       Request.Range := IntToStr(ResumeByte) + '-';

                       Get(TheURL, TheFile);
                       IsError         := (Not (ResponseCode in [200, 206])) OR (Pos('login', URL.Document) <> 0);

                       // Break if there's any error (to allow retrying later in a clean fashion)
                       if IsError then
                          Break;
                 until DoExit;

                 Disconnect;
              except
                    if (ResponseCode = 404) then
                    begin
                         // File doesn't exist, get out
                         Result        := False;
                         Exit;
                    end;
              end;    // try/except
           finally
                  FreeAndNil(TheFile);
           end;    // try/finally
      end;    // with
   except
         IsError  := True;
   end;    // try/except
finally
       Stream.Free;
end;

I posted a similar question some time ago, but that was regarding upload, not download. The code was fixed since then with the kind help of SO members, and the same code (used to deal with cookies, re-login, etc...) is being used now so I assume the problem is really in the download procedure shown above

Can someone please take a look at this tell me what I'm doing wrong?

Upvotes: 1

Views: 5754

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595349

Just like with your other question, you should upgrade to a more recent version, if possible, and verify the problem still occurs before then asking for help. The current version is 10.5.9 r4861.

Upvotes: 2

Related Questions