Re: FtpServer closes connection if file does't exist

Giganews Newsgroups
Subject: Re: FtpServer closes connection if file does't exist
Posted by:  Remy Lebeau \(TeamB\) (no.spam@no.spam.com)
Date: Fri, 3 Oct 2008

"Tomasz B" <t.berg.REMO…@slican.pl> wrote in message
news:45B560C26865E340t.berg.REMO…@slican.pl...

> Servers which I use (Filezilla/vsFTPd) arespond with RFC 550
> code when are asked to operate on not existed file

So does TIdFTPServer when the OnRetreive File and OnStoreFile event handlers
fail to prepare the file for transfer.

> My code (shown below) should do the same but it also close
> the connection to the ftp client if required file doesn't exist and a
> passive mode is used

TIdFTPServer does not close the client connection when an error occurs.

> an EIdSocketError exception is thrown in client

Then your client is likely trying to read from the passive connection even
though the server reported a failed transfer.

> void __fastcall CServer::IdFTPServerRetrieveFile(
<snip>

You are throwing the wrong kind of exception.  Try this code instead:

    void __fastcall CServer::IdFTPServerRetrieveFile(TIdFTPServerContext
*aSender, const AnsiString aFileName, TStream* &vStream)
    {
        if (!FileExists(convertPath2Win( aFileName, aSender->HomeDir )))
            throw Exception("Failed to open file.");

        vStream = new TFileStream(convertPath2Win( aFileName,
aSender->HomeDir ) , fmOpenRead | fmShareDenyWrite ) ;
    }

Better, simply remove the call to FileExists(), as TFileStream will throw
its own exception if it fails to open the file:

    void __fastcall CServer::IdFTPServerRetrieveFile(TIdFTPServerContext
*aSender, const AnsiString aFileName, TStream* &vStream)
    {
        vStream = new TFileStream(convertPath2Win( aFileName,
aSender->HomeDir ) , fmOpenRead | fmShareDenyWrite ) ;
    }

--
Remy Lebeau (TeamB)

Replies

In response to

FtpServer closes connection if file does't exist posted by Tomasz B on Thu, 02 Oct 2008