[4.50] Problem with ReceiveNetworkData

Just starting out? Need help? Post your questions and find answers here.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

[4.50] Problem with ReceiveNetworkData

Post by helpy »

I am experimenting with ReceiveNetworkData

Quote from help:
The 'Result' returns the number of bytes effectively read. If 'Result' is equal to DataBufferLength, then more data is available to be read. If an error occured on the connection (link broken, connection close by the server etc.), 'Result' will be -1.
I tried to do the following, cause of my interpretation of description:

Code: Select all

; only used if there is an network event !!!
ReceivedBytes = ReceiveNetworkData( ConnId, *buffer, BufferLength )

Select Received Bytes

  Case -1
    ; Connection error ==> error handling
    ; 

  Case 0
    ; I assume that ReceiveNetworkData will return 0
    ; if last ReceiveNetworkData() wrote BufferLength bytes
    ; in to buffer!

  Case BufferLength
    ; According to description this means, that there is still
    ; data available to be read.

  Default
    ; No more data available at this time
    ; ==> wait for next network event!

EndSelect
But it does not work like this!

On the server:
  • Client sent 16 Bytes
  • I used ReceiveNetworkData( Connection, *buffer, 16 ) and got 16 Bytes!
  • according to the description I called ReceiveNetworkData( ClientID, *buffer, 16 ) again and the function returned -1
  • -1 does mean that there is an error! ==> but there was no error, because the server can send data back to the client!
On the client:
  • Server sent 16 Bytes
  • I used ReceiveNetworkData( Connection, *buffer, 16 ) and got 16 Bytes!
  • according to the description I called ReceiveNetworkData( ClientID, *buffer, 16 ) again and now the function blocks!
tested on Windows XP SP3, PureBasic 4.50, with local server and local client, connection type TCP (not UDP)

Is this a bug in the network lib? ... or is this a bug in the description? ... or do I misunderstand something? ... or ???

Thank you for all your help!

cu, helpy
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: [4.50] Problem with ReceiveNetworkData

Post by netmaestro »

according to the description I called ReceiveNetworkData( ClientID, *buffer, 16 ) again and now the function blocks!
When you called ReceiveNetworkData the second time, did you do it in response to a NetworkClientEvent() which returned #PB_NetworkEvent_Data?
BERESHEIT
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: [4.50] Problem with ReceiveNetworkData

Post by helpy »

Thank you netmaestro!

In the meantime I made some more tests ... and also tried your suggestion!
This seems to be the solution :-)

There are a lot of examples in the forum where ReceiveNetworkData() is used in a loop (after a network client event #PB_NetworkEvent_Data) until the returned value is not equal the buffer length.

See example ==> http://www.purebasic.fr/english/viewtop ... 08#p321508

I also understood the description in the PureBasic help this way, that I can call ReceiveNetworkData() as long as there is data available without checking NetworkClientEvent() in the meantime!

But now I will change this.
cu, helpy
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: [4.50] Problem with ReceiveNetworkData

Post by Trond »

It really shouldn't work like you describe in your first post. It should work like I describe here: http://www.purebasic.fr/english/viewtop ... 08#p321508

A small test shows that everything works correctly here.

Code: Select all

; SERVER

InitNetwork()
CreateNetworkServer(0, 1025)
Buffersize = 16
*Buffer = AllocateMemory(Buffersize)
Debug "Started server"
Repeat
    SEvent = NetworkServerEvent()
    Select SEvent
      Case 0
        Delay(10)
      Case #PB_NetworkEvent_Connect
        Debug "A new client has connected! " + Str(EventClient())

      Case #PB_NetworkEvent_Data
        Repeat
          Result = ReceiveNetworkData(EventClient(), *Buffer, Buffersize)
          If Result
            Debug "Received " + PeekS(*Buffer, Result) + " from " + Str(EventClient())
          Else
            Debug "Just did a non-blocking read of 0 bytes."
          EndIf
        Until Result <> Buffersize

      Case #PB_NetworkEvent_Disconnect
        Debug Str(EventClient()) + " disconnected."
        Break
    EndSelect
ForEver

Code: Select all

; CLIENT

InitNetwork()
C = OpenNetworkConnection("127.0.0.1", 1025)
Debug "Client started"
SendNetworkData(C, @"'Hello from me!'", 16)
CloseNetworkConnection(C)
For I = 0 To 5
  Delay(100)
Next
Debug "Client ended"
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: [4.50] Problem with ReceiveNetworkData

Post by infratec »

Hi helpy
There are a lot of examples in the forum where ReceiveNetworkData() is used in a loop (after a network client event #PB_NetworkEvent_Data) until the returned value is not equal the buffer length.
Of course and this is ok.

Because it means that there are more bytes received than you took in your buffer.
So you have to copy more bytes in your buffer.
And this can be done in a loop.
And all this bytes was received by the first network event.

In your example you had already read all bytes (16 of 16).
So an additional call will block until new data arrives.

Bernd
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: [4.50] Problem with ReceiveNetworkData

Post by helpy »

Thank you Trond!

You example works here as you described.

BUT I have an example here, which works like I described it in my first posting.

I will do some more testing ... but when I do not find a bug in my code I will post it here!

cu, helpy
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: [4.50] Problem with ReceiveNetworkData

Post by Trond »

In your example you had already read all bytes (16 of 16).
So an additional call will block until new data arrives.
Actually, an additional call returns 0 without blocking. Further calls will block, though.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: [4.50] Problem with ReceiveNetworkData

Post by helpy »

Hello Trond!

Here is my example (compiled in ascii mode!!!), that it is possible that an additional call of ReceiveNetworkData() does not always return 0 if the previous call returns "BufferSize" (in my example BlockSize):

Server:

Code: Select all

; SERVER

If InitNetwork()
	Debug "Network initialized!"
Else
	Debug "Could not initialize network!"
	End
EndIf

server = CreateNetworkServer(#PB_Any, 5555)
If server
	Debug "Server was started and is listening ..."
Else
	Debug "Could not start server!"
	End
EndIf

Blocksize = 16
Buffersize = 1024
*Buffer = AllocateMemory(Buffersize)
If *Buffer
	Debug "Buffer allocated!"
Else
	Debug "Could not allocate buffer!"
	End
EndIf

Debug "Started server"
Repeat
  SEvent = NetworkServerEvent()
  Select SEvent
    Case 0
      Delay(10)
    Case #PB_NetworkEvent_Connect
    	Debug "A new client has connected! " + Str(EventClient())

    Case #PB_NetworkEvent_Data
    	BytesReceived = 0
    	Debug ""
      Repeat
      	Result = ReceiveNetworkData(EventClient(), *Buffer+BytesReceived, Blocksize)
      	Debug "Result = " + Str( Result )
      	If Result = -1
      		Debug "Error receiving data!"
      		Break
      	ElseIf Result
      		Debug "Received " + Str(Result) + " bytes from " + Str(EventClient())
      		BytesReceived + Result
        Else
          Debug "Just did a non-blocking read of 0 bytes."
        EndIf
      Until Result <> Blocksize
      Debug "Received " + #DQUOTE$ + PeekS(*Buffer, BytesReceived) + #DQUOTE$

    Case #PB_NetworkEvent_Disconnect
      Debug Str(EventClient()) + " disconnected."
      Break
  EndSelect
ForEver
Client:

Code: Select all

; CLIENT

If InitNetwork()
	Debug "Network initialized!"
Else
	Debug "Could not initialize network!"
	End
EndIf

Conn = OpenNetworkConnection("localhost", 5555)

If Conn 
	Debug "Client started"
Else
	Debug "Could not start client"
	End
EndIf

Buffersize = 16
*Buffer = AllocateMemory(Buffersize)
If *Buffer
	Debug "Buffer allocated!"
Else
	Debug "Could not allocate buffer!"
	End
EndIf

For i = 1 To 32
	Bytes = SendNetworkData(Conn, @"client message: 0123456789ABCDEF", i)
	Debug Str(Bytes) + " Bytes sent"
	Delay(2000)
Next i

Debug "Closing network connection ..."
CloseNetworkConnection(Conn)
Delay( 2000 )
Debug "Client network connection closed!"
Here the debug output of the client:

Code: Select all

Network initialized!
Client started
Buffer allocated!
1 Bytes sent
2 Bytes sent
...
15 Bytes sent
16 Bytes sent
17 Bytes sent
...
31 Bytes sent
32 Bytes sent
Closing network connection ...
Client network connection closed!
And the debug output of the server:

Code: Select all

Network initialized!
Server was started and is listening ...
Buffer allocated!
Started server
A new client has connected! 3941808

Result = 1
Received 1 bytes from 3941808
Received "c"

Result = 2
Received 2 bytes from 3941808
Received "cl"

...

Result = 15
Received 15 bytes from 3941808
Received "client message:"

Result = 16
Received 16 bytes from 3941808
Result = -1
Error receiving data!
Received "client message: "

Result = 16
Received 16 bytes from 3941808
Result = 1
Received 1 bytes from 3941808
Received "client message: 0"

...

Result = 16
Received 16 bytes from 3941808
Result = 15
Received 15 bytes from 3941808
Received "client message: 0123456789ABCDE"

Result = 16
Received 16 bytes from 3941808
Result = 16
Received 16 bytes from 3941808
Result = -1
Error receiving data!
Received "client message: 0123456789ABCDEF"
3941808 disconnected.
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
KGB_X
User
User
Posts: 63
Joined: Tue Apr 22, 2008 6:06 pm
Location: No(r)way

Re: [4.50] Problem with ReceiveNetworkData

Post by KGB_X »

I'm using the Linux version and have tested this and i also get -1

I have tested this to receive data that are from 20-60 characters
sometimes i get the complete line and other time i only get parts of the line.

Using netcat is showing the same data as one line.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: [4.50] Problem with ReceiveNetworkData

Post by helpy »

Hello all,

I agree with netmaestro!

In my opinion it is more secure to use ReceiveNetworkData() only once after a NetworkEventClient() or NetworkEventServer() ... and do not use ReceiveNetworkData() in a loop without calling NetworkEvent******().

If ReceiveNetworkData() returns BufferSize ... and there is more data to read, NetworkEvent******() will return the appropriate event!

cu,
helpy
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
Post Reply