To @infratec — I remember your comment some weeks ago when you helped me to implement UDP communication between two processes, where you mentioned, "Your sended messages should not be longer than 1500 bytes. Else it will be more complicated". I guess this is relevant. viewtopic.php?p=590452#p590452
To demonstrate, I have used the example in the PB documentation, adding code to transmit a directory listing (because it's a useful test). It is equivalent to the loss of data that I see in my main application, but simplified so others can see. If running the below, add your IP address in client.pb, run the server first, then start the client. The client will send a one-line message to the server and the server will respond, sending back a directory listing which happens to be of C:\Windows\System32.
I have found other posts on this subject, one of which includes a comprehensive module for dealing with what it refers to as data over 64kb, but I'm keen to keep this to a set of changes to the below sample from the docs if I can, because then it becomes possible for me to understand a manageable amount of detail. Thanks in advance.
Here is a link to an example of the data loss... https://ibb.co/C2ttVHw
server.pb
Code: Select all
; ------------------------------------------------------------
;
; PureBasic - Network (Server) example file
;
; (c) Fantaisie Software
;
; ------------------------------------------------------------
;
; Note: run the Client.pb file to send some data to this server
;
OpenConsole()
EnableExplicit
Define *Buffer = AllocateMemory(1000)
Define Quit
Define ClientID, ServerEvent
Declare Send_A_Lot(ClientID) ; Procedure sends a lot of 'dir' data to client as a test
If CreateNetworkServer(0, 6832, #PB_Network_IPv4 | #PB_Network_TCP)
PrintN("PureBasic - Server - Server created")
Repeat
ServerEvent = NetworkServerEvent() ; Server event loop
If ServerEvent ; Server event triggered
ClientID = EventClient() ; Which client ID ?
Select ServerEvent
Case #PB_NetworkEvent_Connect ; Client connects
PrintN("PureBasic - Server - A new client has connected !")
Case #PB_NetworkEvent_Data ; Received data from client
PrintN("PureBasic - Server - Client " + ClientID + " has send a packet !")
ReceiveNetworkData(ClientID, *Buffer, 1000)
PrintN("Recevied data from client - string : "+PeekS(*Buffer, -1, #PB_Ascii))
Send_A_Lot(ClientID) ; We've received data from client, so send 'a lot' back
Quit = 1 ; Quit the server process now we've done
Case #PB_NetworkEvent_Disconnect ; Client has disconnected
PrintN("PureBasic - Server - Client " + ClientID + " has closed the connection...")
Quit = 1 ; Quit the server process
EndSelect
EndIf
Until Quit = 1
CloseNetworkServer(0) ; Close this server's connection
Else
PrintN("Error - Can't create the server (perhaps port in use ?)")
EndIf
PrintN("PureBasic - Server - Press Enter to quit the server.")
Input()
Procedure Send_A_Lot(ClientID)
Define dirpath.s = "C:\Windows\System32" ; 'Dir' provides a good source of test data
Define directory.l ; Directory id.
Define type.s ; Type 'blank' for files or <DIR>
PrintN("PureBasic - Server - Sending a long directory listing to the client...")
SendNetworkString(ClientID, "Directory for : " + dirpath.s + #CRLF$, #PB_Ascii)
directory.l = ExamineDirectory(#PB_Any, dirpath.s, "*.*")
If directory.l
While NextDirectoryEntry(directory.l)
type.s = " "
If DirectoryEntryType(directory.l) = #PB_DirectoryEntry_Directory
type.s = "<DIR>"
EndIf
SendNetworkString(ClientID, LSet(DirectoryEntryName(directory.l),40) + " " + type.s + " " + FormatDate("%dd/%mm/%yyyy", DirectoryEntryDate(directory.l, #PB_Date_Modified)) + " " + FormatDate("%hh:%ii:%ss", DirectoryEntryDate(directory.l, #PB_Date_Modified)) + " " + FormatNumber(DirectoryEntrySize(directory.l),0) + " bytes" + #CRLF$, #PB_Ascii)
Wend
FinishDirectory(directory.l)
Else
PrintN("Unable to open directory " + dirpath.s)
EndIf
EndProcedure
Code: Select all
; ------------------------------------------------------------
;
; PureBasic - Network (Client) example file
;
; (c) Fantaisie Software
;
; ------------------------------------------------------------
; Note: run the Server.pb file first To launch the server
OpenConsole()
EnableExplicit
Define *Buffer = AllocateMemory(1000)
Define IP.s = "192.168.0.181" ; Change this to your server's hostname or IP address
Define Quit
Define ConnectionID, ClientEvent
ConnectionID = OpenNetworkConnection(IP.s, 6832) ; Establish a connection to the server
If ConnectionID
PrintN("PureBasic - Client - Client has connected to the server...")
SendNetworkString(ConnectionID, "Hello from the client", #PB_Ascii) ; Send a 'hello' from the client to the server
PrintN("PureBasic - Client - A string has been sent to the server, please check it...")
Repeat
ClientEvent = NetworkClientEvent(ConnectionID) ; Client event loop
If clientEvent
Select ClientEvent
Case #PB_NetworkEvent_Data ; Client has received data
ReceiveNetworkData(ConnectionID, *Buffer, 1000) ; Receive the data buffer
Print(PeekS(*Buffer, -1, #PB_Ascii)) ; Print contents of buffer without CRLF
Case #PB_NetworkEvent_Disconnect
PrintN("")
PrintN("Server has closed the connection...")
Quit = 1 ; Quit the client process
EndSelect
EndIf
Until Quit = 1
CloseNetworkConnection(ConnectionID) ; Close the client connection
Else
PrintN("PureBasic - Client - Can't find the server (Is it launched ?).")
EndIf
PrintN("PureBasic - Client - Press Enter to quit the client.")
Input()