Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Jan : Re: IdFTP: Connect doesn't work after disconnect

www.cryer.info
Managed Newsgroup Archive

Re: IdFTP: Connect doesn't work after disconnect

Subject:Re: IdFTP: Connect doesn't work after disconnect
Posted by:"Remy Lebeau (TeamB)" (no.spam@no.spam.com)
Date:Sun, 31 Dec 2006 17:29:41

"A. Stroebel" <andy@ascomp.de> wrote in message
news:459850d2@newsgroups.borland.com...

> label FTPRestart:

Goto statements are very old techniques and very discouraged in modern
programming circles.  You should use a loop instead:

>         if Application.MessageBox('Retry?, 'Connect failed', 36 OR
> MB_SYSTEMMODAL) = 6 then

Why are you using numeric literals like that?  You should be using the
constants defined in windows.pas instead for better readibility, ie:

    if Application.MessageBox('Retry?, 'Connect failed',
MB_ICONQUESTION or MB_YESNO or MB_SYSTEMMODAL) = IDYES then

>         IdFTP.Put(MyFile, FTPFileName, false);
<snip>
>         GoTo FTPRestart;

When Put() throws an error, you are calling Connect() right away
without caling Disconnect() first.  You need to disconnect the socket
before you can reconnect again.

> To test: connect, upload, disconnect your internet connection
manually. Now
> "Upload failed" will be shown, retry with "Yes". Now "Connect
failed" will
> be shown

Of course it will fail.  The previous socket is still allocated.  It
has to be cleaned up first.

Try this code instead:

    repeat
        try
            IdFTP.Connect;
        except
            if Application.MessageBox('Retry?, 'Connect failed',
MB_ICONQUESTION or MB_YESNO or MB_SYSTEMMODAL) <> IDYES then Exit;
            Continue;
        end;
        try
            IdFTP.Put(MyFile, FTPFileName, false);
        except
            IdFTP.Disconnect;
            if Application.MessageBox('Retry?, 'Upload failed',
MB_ICONQUESTION or MB_YESNO or MB_SYSTEMMODAL) <> IDYES then Exit;
            Continue;
        end;
        Break;
    until False;


Alternatively:

    type
        ERetryError = class(Exception);

    repeat
        try
            try
                IdFTP.Connect;
            except
                raise ERetryError.Create('Connect');
            end;
            try
                IdFTP.Put(MyFile, FTPFileName, false);
            except
                IdFTP.Disconnect;
                raise ERetryError.Create('Upload');
            end;
        except
            on E: ERetryError do
            begin
                if Application.MessageBox('Retry?, PChar(E.Message + '
failed'), MB_ICONQUESTION or MB_YESNO or MB_SYSTEMMODAL) <> IDYES then
Exit;
                Continue;
            end;
            on E: Exception do
            begin
                Application.MessageBox(PChar(E.Message), 'Unknown
error', MB_ICONERROR or MB_OK or MB_SYSTEMMODAL);
                Exit;
            end;
        end;
        Break;
    until False;


Gambit

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive