Page 1 of 1
ReceiveNetworkData: Result < BufferLength
Posted: Wed Apr 14, 2010 12:05 am
by crackhead
Hi,
the docu says there is more to read, when the above expression is false.
Code: Select all
*Buffer = AllocateMemory(20000)
SendNetworkString(Socket, Header)
While Not NetworkClientEvent(Socket)
Delay(10)
Wend
BufferSize.l = MemorySize(*Buffer)
Repeat
Size.l = ReceiveNetworkData(Socket, *Buffer, BufferSize)
Debug Size.l
ForEver ;Size < BufferSize
gives me
2904
2048
2048
260
2048
856
0
means it, Result < BufferLength seems to be directly true, however, more data is following
So, to really get the whole data I'd currently need to check if ReceiveNetworkData returned 0, but this
would include a timeout, so how should I write it?
Thank you in advance.
Re: ReceiveNetworkData: Result < BufferLength
Posted: Wed Apr 14, 2010 11:50 am
by Trond
Your code above doesn't event compile. Post one that does.
the docu says there is more to read, when the above expression is false.
No it doesn't. Read the manual again.
If 'Result' is equal to DataBufferLength, then more data is available to be read.
Also, you should ONLY call ReceiveNetworkData() after NetworkEventClient() returns #PB_NetworkEvent_Data. Else ReceiveNetworkData() will not act like you want it to.
Re: ReceiveNetworkData: Result < BufferLength
Posted: Wed Apr 14, 2010 3:19 pm
by crackhead
Aye, sorry, was tired yesterday.
The docu says, there is more to read, when result is equal to buffersize.
As the results say, it's directly unequal, but there is still more to read.
Sorry, I'm a noob in aspects of networks..
Also, the code should just represent a little snippet
Re: ReceiveNetworkData: Result < BufferLength
Posted: Wed Apr 14, 2010 4:43 pm
by AND51
Trond wrote:Also, you should ONLY call ReceiveNetworkData() after NetworkEventClient() returns #PB_NetworkEvent_Data. Else ReceiveNetworkData() will not act like you want it to.
Dealing with PureBasic for approx. 4 ½ years, but you opened my eyes today, Trond! Thank you very much!
Although I haven't written code for quite a long time, I never realized this fact. I also used ReceiveNetworkData() in a loop and wondered why its return values were such volatile.
I now understand this circumstance this way:
Within an outer loop checking for a #PB_Network_Data event, I use ReceiveNetworkData() in an inner loop with a buffer of 1.000 until the returned value is not equal to 1.000 (due to senders might have paused sending or other network issues). The outer loop waits every time until an data-received event is registered and then triggers. The outer loop is left, when my HTML document, zip file, etc. has been fully received. Right?
Re: ReceiveNetworkData: Result < BufferLength
Posted: Wed Apr 14, 2010 6:15 pm
by Trond
AND51 wrote:
I now understand this circumstance this way:
Within an outer loop checking for a #PB_Network_Data event, I use ReceiveNetworkData() in an inner loop with a buffer of 1.000 until the returned value is not equal to 1.000 (due to senders might have paused sending or other network issues). The outer loop waits every time until an data-received event is registered and then triggers. The outer loop is left, when my HTML document, zip file, etc. has been fully received. Right?
This is entirely correct. And if you don't do it this way, things will, in my experience, just not work right.
So:
Code: Select all
repeat
; handle other events
; handle network events
Select EventclientNetwork()
case #receive:
repeat
Result = ReceiveNetworkData(Buffer)
until Result <> Buffersize
endselect
forever
There is a special case where the data is equal to buffersize, then result will equal buffersize even though there is no more data. In this case the next call to receivenetworkdata() will simply read 0 bytes and return immediately. BUT: in other cases, receivenetworkdata() blocks (well it did for me a few versions ago) if there is no more data.
I hope this answers the original question as well.