ReceiveNetworkData: Result < BufferLength

Just starting out? Need help? Post your questions and find answers here.
crackhead
User
User
Posts: 12
Joined: Tue Apr 06, 2010 9:26 pm

ReceiveNetworkData: Result < BufferLength

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: ReceiveNetworkData: Result < BufferLength

Post 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.
crackhead
User
User
Posts: 12
Joined: Tue Apr 06, 2010 9:26 pm

Re: ReceiveNetworkData: Result < BufferLength

Post 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
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Re: ReceiveNetworkData: Result < BufferLength

Post 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! :shock:
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. :o


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?
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: ReceiveNetworkData: Result < BufferLength

Post 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.
Post Reply