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
