Here is the server process:
Code: Select all
EnableExplicit
Procedure ProcessRequest(clientNumber)
Protected *memoryLoc = AllocateMemory(10000)
Protected result
Protected TextToSend.s = "ok"
result = ReceiveNetworkData(clientNumber, *memoryLoc, 10000)
FreeMemory(*memoryLoc)
Delay(5); delay as if reading data from a file
SendNetworkData(clientNumber, @TextToSend, Len(TextToSend))
PrintN("Client " + Str(clientNumber) + " received " + Str(result) + " bytes")
EndProcedure
Define NSEvent
Define thisClient
Define KeyPressed.s
InitNetwork()
CreateNetworkServer(#PB_Any, 12000)
OpenConsole()
PrintN("Started Network Server on port 12000")
PrintN("Press Escape to exit")
PrintN("")
Repeat
NSEvent = NetworkServerEvent() ; if we receive data, it will be indicated here
Select NSEvent
Case #PB_NetworkEvent_Connect ; a socket has connected
Case #PB_NetworkEvent_Data ; raw data has been received
thisClient = EventClient() ; get the event client identifier
CreateThread(@ProcessRequest(), thisClient); thread to process incoming data
Case #PB_NetworkEvent_File ; uploading a file
Case #PB_NetworkEvent_Disconnect; a socket disconnected
Default; if no event data has been received, sleep so we don't hammer the processor
Delay(50)
EndSelect
KeyPressed = Inkey()
Until KeyPressed = #ESC$
PrintN("Escape pressed, press <enter> to terminate this process")
Input()
End
; IDE Options = PureBasic 4.51 (Windows - x64)
; CursorPosition = 10
; Folding = -
; EnableThread
; Executable = test_server.exe
; CurrentDirectory = D:\dev\PureBasic\temp\
; CompileSourceDirectory
Here is the client process:
Code: Select all
EnableExplicit
Procedure netSend(counter)
Protected connectNumber.i
Protected TextToSend.s
Protected result.i
Protected *MemoryID
connectNumber = OpenNetworkConnection("localhost", 12000)
If connectNumber
TextToSend = "This is counter # " + RSet(Str(counter), 1000, " ")
result = SendNetworkData(connectNumber, @TextToSend, Len(TextToSend))
If result > 0
*MemoryID = AllocateMemory(100)
If *MemoryID
result = ReceiveNetworkData(connectNumber, *MemoryID, 100)
FreeMemory(*MemoryID)
EndIf
Else
PrintN("Could not write to the socket, result = " + Str(result))
EndIf
CloseNetworkConnection(connectNumber)
Else
PrintN("Could not open socket to localhost on port 12000")
EndIf
EndProcedure
Define Thread.i
Define counter.f = 0
Define loopNdx.i
Define loopCT.i = 50
Define startTime.i = ElapsedMilliseconds()
InitNetwork()
OpenConsole()
PrintN("Starting " + Str(loopCT) + " loops")
For loopNdx=1 To loopCT
counter = counter + 1
Thread = CreateThread(@netSend(), counter)
If counter/10 = Int(counter/10)
PrintN(Str(counter) + " send attempts processed")
EndIf
Delay(50); sleep 1/20th of a second here
Next loopNdx
PrintN("Loop went " + Str(loopNdx - 1) + " times")
PrintN("Process took " + Str((ElapsedMilliseconds() - startTime) / 1000) + " seconds")
Input()
End
; IDE Options = PureBasic 4.51 (Windows - x64)
; CursorPosition = 12
; Folding = -
; EnableThread
; Executable = test_client.exe
; HideErrorLog
; CurrentDirectory = D:\dev\PureBasic\temp\
; CompileSourceDirectory
; EnablePurifier
Here is the client output:
Code: Select all
Starting 50 loops
10 send attempts processed
20 send attempts processed
30 send attempts processed
40 send attempts processed
50 send attempts processed
Loop went 50 times
Process took 2 seconds
Here is the Server output, note there should only be 50 lines, for 50 client connection attempts:
Code: Select all
Started Network Server on port 12000
Press Escape to exit
Client 8796063864560 received -1 bytes
Client 8796063864560 received 1018 bytes
Client 8796063864560 received -1 bytes
Client 8796063866080 received 1018 bytes
Client 8796063866080 received -1 bytes
Client 8796063866016 received -1 bytes
Client 8796063866016 received 1018 bytes
Client 8796063865968 received 1018 bytes
Client 8796063865968 received -1 bytes
Client 8796063865968 received -1 bytes
Client 8796063866016 received -1 bytes
Client 8796063866016 received 1018 bytes
Client 8796063865936 received 1018 bytes
Client 8796063865936 received -1 bytes
Client 8796063866128 received 1018 bytes
Client 8796063866128 received -1 bytes
Client 8796063866080 received 1018 bytes
Client 8796063866080 received -1 bytes
Client 8796063866128 received 1018 bytes
Client 8796063866128 received -1 bytes
Client 8796063865936 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063866032 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063865936 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063866032 received 1018 bytes
Client 8796063865936 received 1018 bytes
Client 8796063865936 received -1 bytes
Client 8796063866032 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063866032 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063865936 received -1 bytes
Client 8796063866032 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063865936 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063866032 received 1018 bytes
Client 8796063865936 received 1018 bytes
Client 8796063865936 received -1 bytes
Client 8796063866032 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063865936 received -1 bytes
Client 8796063866032 received -1 bytes
Client 8796063866032 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063865936 received -1 bytes
Client 8796063865936 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063866032 received 1018 bytes
Client 8796063865936 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063866032 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063865936 received -1 bytes
Client 8796063866032 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063865936 received -1 bytes
Client 8796063866032 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063865936 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063866032 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063865936 received -1 bytes
Client 8796063865936 received -1 bytes
Client 8796063866032 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063865936 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063866032 received 1018 bytes
Client 8796063865936 received 1018 bytes
Client 8796063865936 received -1 bytes
Client 8796063882752 received 1018 bytes
Client 8796063882752 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063865936 received -1 bytes
Client 8796063866032 received -1 bytes
Client 8796063866032 received 1018 bytes
Client 8796063865936 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063866032 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063865936 received -1 bytes
Client 8796063866032 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063865936 received -1 bytes
Client 8796063866032 received 1018 bytes
Client 8796063866032 received -1 bytes
Client 8796063865936 received 1018 bytes
Client 8796063865936 received -1 bytes
Client 8796063866032 received -1 bytes
Client 8796063866032 received 1018 bytes
Client 8796063865936 received 1018 bytes
Client 8796063865936 received -1 bytes
Escape pressed, press <enter> to terminate this process
There are actually 106 lines where the server thought there was a new client connection, when there really wasn't. Please also note the client number. It appears that EventClient() is not reset until data is read from a socket, at least that's what I theorize.