Hello Erel & agraham,
i did further testing and was successful in getting my code working, but..I'm still having issues with the client.DataAvailable.
I was able to get this here to work:
Sub MySub
...
send something to the server
GetNetworkResponse
make a decision depending on buf
...
End Sub
Sub GetNetworkResponse
Do Until client.DataAvailable = true
Sleep(300)
Loop
Dim bufferbits(4096) As byte
count = stream.ReadBytes(bufferbits(),4096)
buf = bit.BytesToString(bufferbits(),0,count)
Return buf
End Sub
So far so good.But if i change the time in Sleep() to more than 300, the program will deadlock within the Loop.This is because the client.DataAvailable never becomes true.You mentioned above, that the time does not matter for this function.So i could - theoretically - execute client.DataAvailable at the end of my program and would get back a true ( yes there's data i received from the server) and could then retrieve this data - if the server ever responded someting.
So..why does my code above not work anymore if the sleep() is set higher than 300...? I do not have an explanation for that, do you..?
If i add a counter, which exits the loop after a certain number of cycles, everything works ok:
Sub GetNetworkResponse
x=1
buf = ""
Do Until client.DataAvailable = true
Sleep(500)
x = x+1
If x > 50 Then
Return
End If
Loop
Dim bufferbits(4096) As byte
count = stream.ReadBytes(bufferbits(),4096)
buf = bit.BytesToString(bufferbits(),0,count)
Return buf
End Sub
If the condition persists, the if..then needs to break the loop after the specified maximum number of loop cycles...and if this occurs, the buf has the correct server response stored.So the answer was there, but the client.DataAvailable prevent the loop from finishing, so it loops for ever.
To circumvent that problem, i use then this code :
x=1
buf = ""
Do While buf = ""
x = x+1
' If x > 20 Then
' Return
' End If
Dim bufferbits(4096) As byte
count = stream.ReadBytes(bufferbits(),4096)
buf = bit.BytesToString(bufferbits(),0,count)
Sleep(500)
Loop
Return buf
end sub
It just reads the buffer in a loop, until the buffer (buf) is different from an empty string.To prevent a deadlock from happening, if there is no answer from the server, i built in the If...then which is ending the loop after a certain number of cycles.
So i assume then: data.ClientAvailable is working in general, but sometimes it does not.I suspect it happens when the retrieved server response is not complete yet.
This would the look like this:
i send some request data to the server ( netwritebuffer() = bit.StringToBytes(s,0,StrLength(s)) )
after 100ms i check client.DataAvailable
client.DataAvailable is true
==> i retrieve the server response
the server is sending more response data
after 100ms i check client.DataAvailable
client.DataAvailable is false
If i rely on the client.DataAvailable ==> loop is infinite now
if i just retrieve the data, the buffer contains what i was looking for.
This probably happens, if the delay betweens checks is shorter than the server needs to send the complete answer.Since i cannot know the server's response time in advance and i don't know, if the server has completed its answer, i have either to check, if i can expect more data from server or i have to set a time out, after which i will give up waiting.
For this, i've to rely on the client.DataAvailable or i can do a cyclic stream.ReadBytes until i get a non-empty string or the timeout is reached.
Therefore my guess is, client.DataAvailable does indicate the first data received after a netwritebuffer(), but if the server sends more packets after the first data ( which was indicated by client.DataAvailable) had been retrieved using stream.ReadBytes, any subsequent reply packet is then not indicated by the the client.DataAvailable any more.
I took now the first code example, added the cycle count check as a timeout and complemented the Do Until..Loop check with "OR (buf <> "")", which prevents the code from running unnecessary loops passes:
Sub GetNetworkResponse
x=1
buf = ""
Do Until client.DataAvailable = true OR (buf <> "")
Sleep(500)
x = x+1
If x > 50 Then
Return
End If
Loop
Dim bufferbits(4096) As byte
count = stream.ReadBytes(bufferbits(),4096)
buf = bit.BytesToString(bufferbits(),0,count)
Return buf
End Sub
@Erel: please can you have a look at this behavior, why the client.DataAvailable does not indicate subsequent packets from the server, while i can read them anyhow..?
regards
TWELVE