Page 1 of 1

how to tell when an ftp server is done sending text

Posted: Thu Jan 29, 2004 9:43 pm
by beernutz
I am reposting this here in hopes of finding an answer:

Here is a routine i wrote and am using to get text data (the response from an ftp server to a custom command) from an ftp server. Currently i have to have the delay set higher if i am connecting to a slower site.

More succinctly here is the issue:

Ftp programs know when a server is done sending text to it over the command channel (port) somehow. I am not aware of how to tell this with purebasic. I can tell when there is no more data left in the buffer to read, but this does NOT mean that there is no more coming from the server.

The code below kinda works, but the delay is purely arbitrary and sometimes is not long enough.

if anyone can spot what i am doing wrong, or knows how to tell when the servers text output is finished, i would REALLY appreciate any help!

Thannks again!

Code: Select all

Procedure.s getFtpResponse(ConnectionID) 

    bufflen = 1000 
    Buffer = AllocateMemory(0, bufflen, 0) 
    bstr$ = "" 
    blen = 0 

    ; this is normally 2 or 3 (seconds) 
    numSecs = GetGadgetState(#delay) 

    ; init the start time 
    startTime = Date() 

    Repeat ; loop for numSecs seconds past the last data read 
        Result = NetworkClientEvent(ConnectionID) 
        ResultLen = -1 

        If result <> 0 
            ;Debug Str(result) + " = result" 
        EndIf 

        If Result = 2   ; if we have data to grab 
            Repeat      ; then grab the data until the buffer is empty 
                ResultLen = ReceiveNetworkData(ConnectionID, Buffer, bufflen) 
                Debug( Str(ResultLen) + " bytes read") 
                bstr$ = bstr$ + Left(PeekS(Buffer),ResultLen) 
                blen + ResultLen 
            Until ResultLen < bufflen 
            startTime = Date() ; reset our timer after we read the data 
        EndIf 
    Until ((Date() - StartTime) => numSecs) 

    final$ = Left(bstr$,blen) ; get JUST the data we read 
    final$ = RemoveString(final$, Chr(10)) ; and trim off the LineFeed characters 

    ProcedureReturn final$ 
EndProcedure 
 

DOH.. found the answer.

Posted: Thu Jan 29, 2004 10:03 pm
by beernutz
ok, this was definately a case of READ THE F****** RFP! lol

from http://www.w3.org/Protocols/rfc959/4_FileTransfer.html

Thus the format for multi-line replies is that the first line
will begin with the exact required reply code, followed
immediately by a Hyphen, "-" (also known as Minus), followed by
text. The last line will begin with the same code, followed
immediately by Space <SP>, optionally some text, and the Telnet
end-of-line code.
sorry if i pissed anyone off by asking first. I am ususally smarter than that. lol

Thought i would post this in case anyone else came across the same issue.