Code : Tout sélectionner
Declare ManageUser(user.l)
InitNetwork()
#MAX_USER = 50
Global Eol.s : Eol = Chr(13) + Chr(10)
Structure user
CID.l
StartRequest.b
ThreadID.l
Request.l
RequestLen.l
EndStructure
Dim UserArray.user(#MAX_USER)
Request = AllocateMemory(2000)
OpenConsole()
SID = CreateNetworkServer(25)
If SID
PrintN("Server lancé")
Repeat
sEvent.l = NetworkServerEvent()
If sEvent
CID.l = NetworkClientID()
Select sEvent
Case 1
For tuser = 0 To #MAX_USER - 1
If UserArray(tuser)\CID = 0
Break
EndIf
Next
UserArray(tuser)\CID = CID
UserArray(tuser)\ThreadID = CreateThread(@ManageUser(), tuser)
PrintN("Nouveau client : " + Str(CID))
Case 2
For tuser = 0 To #MAX_USER
If UserArray(tuser)\CID = CID And UserArray(tuser)\StartRequest = #false
UserArray(tuser)\StartRequest = #true
Break
EndIf
Next
Case 4
For tuser = 0 To #MAX_USER
If UserArray(tuser)\CID = CID
UserArray(tuser)\CID = 0
Break
EndIf
Next
EndSelect
EndIf
ForEver
EndIf
Procedure ManageUser(user.l)
Delay(200)
PrintN("Thread " + Str(UserArray(user)\ThreadID) + " créé pour le client " + Str(UserArray(user)\CID))
UserArray(user)\Request = AllocateMemory(2000)
Repeat
If UserArray(user)\CID = 0
PrintN("Thread " + Str(UserArray(user)\ThreadID) + " detruit")
KillThread(UserArray(user)\ThreadID)
EndIf
If UserArray(user)\StartRequest = #true
UserArray(user)\RequestLen = ReceiveNetworkData(UserArray(user)\CID, UserArray(user)\Request, 2000)
While UserArray(user)\RequestLen = 2000
UserArray(user)\RequestLen = ReceiveNetworkData(UserArray(user)\CID, UserArray(user)\Request, 2000)
Wend
PrintN("User " + Str(UserArray(user)\CID) + " : " + PeekS(UserArray(user)\Request))
UserArray(user)\StartRequest = #false
FreeMemory(UserArray(user)\Request)
UserArray(user)\Request = AllocateMemory(2000)
EndIf
ForEver
EndProcedure