while i implemented this function, waiting for a cr+lf might not be a good idea in case such data will not arrive.
A better way is to check if there is data pending in the buffer and to read it and process it using a state machine.
Here is a code fragment of what i mean :
[code:1:9c1924e417] Result = Socketstat(idx , 0) ' get status
select case result
Case Sock_established
Result2 = Socketstat(idx , Sel_recv) ' get number of bytes waiting
If Result2 > 0 Then
Do
if result2 > cmaxbuf then 'more than can fit
btr = cmaxbuf
result2 = result2 - cmaxbuf
else
btr = result2
result2 = 0
end if
print "RD:" ; btr
Result = Tcpread(idx , buf(1) , btr)
for j = 1 to btr
if bstoreflag = 0 then 'no valid data yet
if buf(j) = 13 then 'we look for 13..10..13..10
bstoreflag = 1 'next state
end if
elseif bstoreflag = 1 then ' we expect 10 now
if buf(j) = 10 then 'ok
bstoreflag = 2
else
bstoreflag = 0
end if
elseif bstoreflag = 2 then 'we expect 13
if buf(j) = 13 then
bstoreflag = 3
else
bstoreflag = 0
end if
elseif bstoreflag = 3 then 'last one
if buf(j) = 10 then 'got it
bstoreflag = 4
else
bstoreflag = 0
end if
elseif bstoreflag = 4 then ' we may store
cnt = cnt + 1
tmp(cnt) = buf(j)
end if
next
[/code:1:9c1924e417]
while established, we read the amount of data in the receive buffer. we need to empty this soon as possible.
we do that using a state machine ( bstoreflag). if we get 13, we go to the next state, if we get 10, again we go to the next state. now we may store the data into some buffer.
the sample processes a header. which has 2 cr+lf.
in our case we would simply begin stroring data till we get 13, then we wait for 10 and when we get that, we received a line.
when using tcpread with a string, it should be long enough to hold a complete line so the data is not split up. while it is safe for your strings (no overwrite) it does not help much when you want to process a complete line.
↧