Si vous cherchez un UDF robuste et utilisable directement dans vos applications, je vous conseil ça
Salut!
Voila un truc que j'ai fait alors que je m'ennuyait du boulot!
Un exemple de serveur TCP multi-clients, tout simple.
Je ne donne pas de clients pour le tester, mais vous pouvez tout simplement faire dans votre navigateur internet l'adresse
$__IP:$__PORT (exemple: 192.168.1.2:8080)
► Afficher le texte
Code : Tout sélectionner
#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.6.1
Author: Matwachich
Script Function:
Example of a multi-clients TCP server
#ce ----------------------------------------------------------------------------
Global Const $maxClients = 10
Global $__Sockets[$maxClients] ; Array of connected sockets
Global $__IP = @IPAddress1, $__PORT = 8080
Global $__MainSocket, $iNewSocket, $sRecv
For $i = 0 To $maxClients - 1 ; Init the array (-1 => Invalid socket)
$__Sockets[$i] = -1
Next
TCPStartup()
; ---
$__MainSocket = TCPListen($__IP, $__PORT) ; Creating server
If @error Then Exit MsgBox(16, "Error", "Unable to create listening socket (" & @error & " - " & @extended & ")")
ConsoleWrite("+> Server created: " & $__IP & ":" & $__PORT & @CRLF)
; ---
HotKeySet("+{esc}", "_terminate")
Global $terminate = 0
While Not $terminate
; Try to catch a connection
$iNewSocket = TCPAccept($__MainSocket)
; If new connection
If $iNewSocket <> -1 Then
; try to stor the connected socket in the sockets array
For $i = 0 To $maxClients - 1
If $__Sockets[$i] = -1 Then
$__Sockets[$i] = $iNewSocket
_TCP_NewClientFunc($iNewSocket) ; Triger the new client function
ExitLoop ; New connection stored, exiting loop
EndIf
Next
EndIf
For $i = 0 To $maxClients - 1
$sRecv = TCPRecv($__Sockets[$i], 1024)
; Invalid socket (either client disconnect, or already invalid)
If @error Then
; If it was previously connected (a valide socket)
If $__Sockets[$i] <> -1 Then
_TCP_ClientDisconnectFunc($__Sockets[$i]) ; Triger client disconnect function
EndIf
$__Sockets[$i] = -1 ; Reset the array's element to -1
ContinueLoop
EndIf
; If data received
If $sRecv Then
_TCP_ReceiveFunc($__Sockets[$i], $sRecv)
EndIf
Next
Wend
; ---
; Trigered when new client connects
Func _TCP_NewClientFunc($hSocket)
ConsoleWrite(">>> New client: " & _SocketToIP($hSocket) & @CRLF)
EndFunc
; Trigered when some data received
Func _TCP_ReceiveFunc($hSocket, $sData)
ConsoleWrite($sData & @CRLF)
EndFunc
; Trigered when client disconnects
Func _TCP_ClientDisconnectFunc($hSocket)
ConsoleWrite(">>> Client disconnected: " & _SocketToIP($hSocket) & @CRLF)
EndFunc
; ---
TCPShutdown()
; ---
; Used to send data to all connected (valid) sockets
; NB: $iExeptionSocket used for example to not re-send data for the sender
; in a chat program for example
Func _TCP_Broadcast($sData, $iExeptionSocket = -1)
For $i = 0 To $maxClients - 1
If $__Sockets[$i] <> -1 And $__Sockets[$i] <> $iExeptionSocket Then
TCPSend($__Sockets[$i], $sData)
EndIf
Next
EndFunc
Func _terminate()
;~ If MsgBox(36, "Confirmation", "Quit?") = 7 Then Return
$terminate = 1
EndFunc
Func _SocketToIP($SHOCKET)
Local $sockaddr, $aRet
$sockaddr = DllStructCreate("short;ushort;uint;char[8]")
$aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
"ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
If Not @error And $aRet[0] = 0 Then
$aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
If Not @error Then $aRet = $aRet[0]
Else
$aRet = 0
EndIf
$sockaddr = 0
Return $aRet
EndFunc ;==>SocketToIP