how to tell if ReceiveNetworkData is done

Everything else that doesn't fall into one of the other PB categories.
beernutz
New User
New User
Posts: 5
Joined: Sat Jul 12, 2003 9:38 pm

how to tell if ReceiveNetworkData is done

Post by beernutz »

As the title implies i am trying to figure out a good way to tell when ReceiveNetworkData is done transmitting data.

I am basically sending a custom site command to an FTP server then waiting for a response. Since the reponses can be large, they are often split up overal several calls to ReceiveNetworkData. the problem comes when i try to determine if it is finally at the end of the data or not.

can anyone help or point me at some examples that show this correctly?

Am i just blind and not seeing something obvious?

EDIT:
I do realise that the return value is supposed to indicate amount of data read and amount remaining, but for some reason i have been having problems with it working reliably.

Thanks a LOT for any help!
legider_pb
User
User
Posts: 39
Joined: Tue Nov 25, 2003 12:28 pm

Post by legider_pb »

Check out "NetworkClientEvent(ConnectionID)" in the help file, it will allow you to tell if there is data waiting in your OS's buffer or not.
beernutz
New User
New User
Posts: 5
Joined: Sat Jul 12, 2003 9:38 pm

example code to illustrate the problem

Post by beernutz »

Here is a routine i wrote and am using to get data. 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 the information!

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
beernutz
New User
New User
Posts: 5
Joined: Sat Jul 12, 2003 9:38 pm

DOH! Found the answer

Post by beernutz »

ok, this was definately a case of READ THE F****** RFC! 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
Post Reply