Let me cut to the chase. I have a problem, with some code I'm writting and I think I'm overlooking something. I'm trying to write a fileserver, I've scraped server designs already, but have found one that stuck. The problem is for each client that connects the main app executes a thread, this thread waits for the client to send data,a file or disconnect. Once it gets something other than a disconnect it creates another thread that processes the request. Problem is after a few clients it begins to spin lock, despite the improvements. Thanks in advance.
server:
Code: Select all
; Fileserver 2.0
If InitNetwork() = 0
MessageRequester("Error", "I could not init the network!", 0)
EndIf
;Global *recbuf
;Global *sndbuf
Result = CreateNetworkServer(2004) ; for our linux users use a non-privlaged port
;Global fname.s
;Global Client
If Result = 0
MessageRequester("Error", "Socket is in use or blocked! Cannot start Server!", 0)
End
EndIf
Result = 0
Procedure preq(cli)
OnErrorResume()
*recbuf = AllocateMemory(1000)
*sndbuf = AllocateMemory(1000)
ReceiveNetworkData(cli, *recbuf, 1000) ; 3 KB buffer
; parse data
rdata$ = PeekS(*recbuf) ; dump buffer into a string
If rdata$ <> ""
Debug rdata$
EndIf
FreeMemory(*recbuf)
FreeMemory(*sndbuf)
EndProcedure
Procedure newclient(ClientID)
;OnErrorResume()
Repeat
Result = NetworkServerEvent()
If Result = 2
;Debug ("crap got")
thid.l = CreateThread(@preq(), ClientID)
WaitThread(thid)
;preq(Client)
EndIf
If Result = 3
;ReceiveNetworkFile(ClientID, fname$)
EndIf
If Result = 4
ClientID = 0
EndIf
Reult = 0
Delay(1); one millisecond but its enough to stop it from eating the CPU time
Until Result = 4
EndProcedure
Repeat
Result = NetworkServerEvent()
If Result = 1
Client = NetworkClientID() ; single thread for now
CreateThread(@newclient(), Client)
Client = 0
EndIf
Result = 0
Delay(1)
ForEver
Code: Select all
If InitNetwork() = 0
MessageRequester("Error", "I could not init the network!", 0)
EndIf
*Buffer = AllocateMemory(1000)
ConnectionID = OpenNetworkConnection("localhost", 2004)
;SendNetworkFile(ConnectionID, "C:\ftp.fbs")
SendNetworkString(ConnectionID, "load fbsl.dll")
CloseNetworkConnection(ConnectionID)
