Corrections to SendNetworkData() and SendNetworkString()

Found an issue in the documentation ? Please report it here !

Moderator: Documentation Editors

Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Corrections to SendNetworkData() and SendNetworkString()

Post by Oso »

https://www.purebasic.com/documentation ... kdata.html
Return value

Returns the number of bytes actually sent. If the number of bytes returned is not equal to the 'Length' parameter the receiving buffer of the user is probably full. If nothing could be sent then 'Result' will equal -1.
There is a difference in behaviour, depending on whether the connection was opened under (a) the server-side process, as CreateNetworkServer() for an incoming client, or (b) via the client-side, as OpenNetworkConnection().

The above 'Return value' is true when sending data from the server-side, where the server times-out if the client is busy and returns -1 or the number of bytes sent. But from the client side, it pauses instead and doesn't time-out. As it pauses indefinitely, the return value therefore behaves differently. It only ever returns -1 if the client has disconnected, in which case #PB_NetworkEvent_Disconnect then applies.

To the extent that it helps to confirm the behaviour from the client-side, the below server and client shows this, where the client waits forever at around 6000 as the buffer fills.

For simplicity, the example uses SendNetworkString() rather than SendNetworkData(), since both behave the same way. In fact, the documentation for SendNetworkString() makes only very limited mention of the return value at all, so I think that should be consistent with the page for SendNetworkData(). Therefore, I suggest two corrections, (1) the client-side behaviour of SendNetworkData() which pauses instead of that which it states, and then (2) SendNetworkString() to match the same page as SendNetworkData(), as its behaviour is the same.

I think the correction is worthwhile, because it currently gives the impression that developers' client-side code should properly handle a -1 return and retry sending, when in fact it's never going to happen at all. Hope this explains it anyway — quite a lot of time spent testing this :D

server_pause.pb

Code: Select all

OpenConsole()
*Buffer = AllocateMemory(1000)
If CreateNetworkServer(0, 6832, #PB_Network_IPv4 | #PB_Network_TCP, "")
  PrintN("Created the server host")
  Repeat
    ServerEvent = NetworkServerEvent()
    If ServerEvent
      ClientID = EventClient()
      
      Select ServerEvent
        Case #PB_NetworkEvent_Connect
          
        Case #PB_NetworkEvent_Data
          recvd.i = ReceiveNetworkData(ClientID, *Buffer, 1000)
          strinp.s = PeekS(*Buffer, recvd.i, #PB_Ascii)
          cntcr.i + 1
          
          Print(strinp)
          Delay(1000)
          If cntcr = 5
            PrintN("")
            Print("Server paused - Press <ENTER> to continue : ")
            Input()
          EndIf
          
        Case #PB_NetworkEvent_Disconnect
          PrintN("Client has closed the connection")
          finish.b = #True
      EndSelect
      
    EndIf
  Until finish.b
Else
  PrintN("Can't create the server (port in use ?).")
EndIf
Print("Press <ENTER> : ")
Input()
client_pause.pb

Code: Select all

OpenConsole()
Print("Press <ENTER> to connect and start the client routine : ")
Input()
ConnectionID = OpenNetworkConnection("127.0.0.1", 6832)
If ConnectionID
  PrintN("Connected okay with the server - now sending data to the server")
  
  For a=1 To 10000
    result.i = SendNetworkString(ConnectionID, RSet(Str(a), 10, "0") + " abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz" + #CRLF$, #PB_Ascii)
    PrintN(LSet(Str(a), 10) + " Sent bytes = " + result)
  Next a  
  Print("Press <ENTER> to end connection : ")
  Input()
  CloseNetworkConnection(ConnectionID)
Else
  PrintN("Can't connect with the server")
  Delay(2000)
EndIf
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2139
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Corrections to SendNetworkData() and SendNetworkString()

Post by Andre »

Network stuff isn't my field of knowledge, sorry...

Fred should decide if the both mentioned commands should get extended description and/or this suggested example codes included in their docs.
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Post Reply