mamuen,
Thanks for reminding me that I have to be more careful about waiting for all the data to arrive.
So let's say the server sends two commands like:
220 then a bunch of other text & CRLF
240 something else & CRLF
We might get "220 then" when we check for incoming data, and then we might get "a bunch of other text" & chr(13) & chr(10) & "240 Something el"
and then might get "se" & chr(13) & chr(10) on our next check.
The trick here is going to be to use a global variable to store our incomplete commands between checking for data. Then when we check for data, we check to see if there is a terminator in it (chr(13) & chr(10)) using something like:
If StrIndexOf(stringbuffer,chr(13) & chr(10),0) > -1
We can't assume that the terminator is the last part of our data, since we may have received part of the next message as well, so if there is a terminator, take everything before the terminator, append it to whatever may have come in before (our global variable) and then act accordingly. Of course we need to look for additional terminators or partial data before proceeding.
If we don't have a terminator, then append the data to our global variable. I don't have this code written in Basic4PPC yet, but here is pseudo-code based on what my REALbasic code does:
count = stream2.ReadBytes(bufferbits(),8192)
stringbuffer = bit.BytesToString(bufferbits(),0,count)
'//look for a terminator in our new data
i = StrIndexOf(stringbuffer,CHR(13) & CHR(10),0)
if i > -1 then
'//loop though and process commands until we run out of terminators
do while i > -1
'//parse out everything before the first terminator
'//append it to any existing data in our global variable
'//process the command
'//look for additional terminators in existing data
i = StrIndexOf(stringbuffer,CHR(13) & CHR(10),i)
loop
else
'//no terminator in this batch of data
'//append text to global variable and wait for more data.
end if
hope that helps,
__________________
Brian Rathbone
Laptop: AMD Turion 64 Mobile 2ghz, 2GB RAM, Dual GeForce Go 7900 GTX 768MB SLI, Dual 200GB HDD at RAID0. XP Pro sp2
Device: Treo 750, WM6, 300mhz,128MB RAM, 2GB miniSD, 240x240 display
|