Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Jan : Re: IdFTP: Connect doesn't work after disconnect
| Subject: | Re: IdFTP: Connect doesn't work after disconnect |
| Posted by: | "A. Stroebel" (an..@ascomp.de) |
| Date: | Mon, 1 Jan 2007 02:51:31 |
It was just a fast written sample of what I am using, so don't wonder about
same parts of the source (label, goto). ;-)
IdFTP.Disconnect doesn't work, it will show an error message "Socked has
been disconnected" or sth. like that (error code 10054 if I right remember).
That's because of the internet connection isn't active when the except block
will be called, so IdFTP.Disconnect cannot succeed at anytime.
Any other ideas?
Best regards,
Andy
P.S. I use numceric literals since it is faster written than the constants
and I know all of the numeric literals by heart. :-)
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> schrieb im Newsbeitrag
news:45986458$1@newsgroups.borland.com...
>
> "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
none