Hello folks!
My application will need to communicate with other instances of itself running on remote machines (sometimes even connected through a slow VPN tunnel). To that end, I need to write a reliable and robust system to send large amounts of data via the network (SendNetworkData, that is). We're talking magnitude of 25 MBytes or more.
I have written some basic code, both client and server. The client produces some random data in the memory, then transmits that to the server. Interestingly, the supposed limit of 65535 bytes documented in the manual don't seem to produce a problem. Perhaps you could make some comments as to where this script might fail or what external events could produce trouble.
Any thoughts appreciated!
merendo
Server:
Code:
If OpenConsole()
If InitNetwork() : PrintN("TCP/IP-Stack initialised.") : Else : End : EndIf
server = CreateNetworkServer(0, 8506)
large_reclen.l = 0
large_recbuffer$ = "" ; Large reception buffer.
small_reclen.w = 0
*small_recbuffer = AllocateMemory(1024) ; Received networkdata goes in here first, then into the large reception buffer.
If server
PrintN("Server running on port 8506")
Repeat
Delay(5)
ServerEvent = NetworkServerEvent()
If ServerEvent
EventClient = EventClient()
Select ServerEvent
Case #PB_NetworkEvent_Connect
PrintN("Client connected.")
Case #PB_NetworkEvent_Data
Repeat ; Keep looping until all data has been received
small_reclen = ReceiveNetworkData(EventClient, *small_recbuffer, 1024)
If small_reclen
large_recbuffer$ = large_recbuffer$ + PeekS(*small_recbuffer, small_reclen)
large_reclen + small_reclen
EndIf
Until Not small_reclen = 1024
PrintN("Received some data. Size: "+Str(large_reclen)+" characters. MD5: "+MD5Fingerprint(@large_recbuffer$, large_reclen))
large_reclen.l = 0
large_recbuffer$ = ""
small_reclen.w = 0
Case #PB_NetworkEvent_Disconnect
PrintN("Client disconnected.")
Input()
End
EndSelect
EndIf
ForEver
EndIf
EndIf
Client:
Code:
If OpenConsole()
If InitNetwork() : PrintN("TCP/IP-Stack initialised.") : Else : End : EndIf
Print("Input requested data length: ")
datenlaenge = Val(Input())
If datenlaenge < 1 : End : EndIf
*baseAddress = AllocateMemory(datenlaenge)
*currentAddress = *baseAddress
Print("Connect to: ")
target$ = Input()
If target$ = "" : End : EndIf
networkConn = OpenNetworkConnection(target$, 8506)
If networkConn
For x = 1 To datenlaenge
PokeS(*currentAddress, Chr(Random(223)+32), 1)
*currentAddress + 1
Next
PrintN("Connection established, random data is ready to transmit. Waiting 2.5 seconds before transmitting data...")
Delay(2500)
SendNetworkData(networkConn, *baseAddress, datenlaenge)
PrintN("Data sent. MD5: "+MD5Fingerprint(*baseAddress, datenlaenge))
Input()
EndIf
EndIf