Désolé de vous importuner avec mes problèmes, mais je débute en TCP et là, je dois dire que je coince :/. En fait, je réalise une application pour mon serveur, qui me permettra de l'administrer facilement à distance depuis mon PC. Le système pour transmettre des commandes au serveur fonctionne, grâce à un exemple de script que j'ai modifié à ma guise. Seulement, quand j'essaie d'envoyer des infos via TCP depuis le serveur sur le client, eh bien rien du tout, aucune réponse ...
Voici mes codes :
Client
► Afficher le texte
Code : Tout sélectionner
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("IntraServeur CLIENT", 390, 222, 192, 124)
Global $Group1 = GUICtrlCreateGroup("Contrôle machine", 8, 8, 161, 89)
Global $Button1 = GUICtrlCreateButton("Extinction", 24, 32, 129, 25, $WS_GROUP)
Global $Button2 = GUICtrlCreateButton("Redémarrage", 24, 64, 129, 25, $WS_GROUP)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global Const $maxClients = 10
Global $__Sockets[$maxClients]
Local $ipserveur = @IPAddress1
Local $PORT = 8080
TCPStartup()
$ConnectedSocket = -1
$ConnectedSocket = TCPConnect($ipserveur, $PORT)
If @error Then
MsgBox(16,"Erreur","Impossible de se connecter au serveur distant !")
Exit
EndIf
$__MainSocket = TCPListen($ipserveur, 8090) ; Creating server
If @error Then Exit MsgBox(16, "Erreur", "Impossible de créer un socket d'écoute (" & @error & " - " & @extended & ")")
AdLibRegister("ReplyFromServ",10)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
TCPSend($ConnectedSocket,"shutdown")
Case $Button2
TCPSend($ConnectedSocket,"reboot")
EndSwitch
WEnd
Func ReplyFromServ()
$iNewSocket = TCPAccept($__MainSocket)
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
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
EndIf
$__Sockets[$i] = -1 ; Reset the array's element to -1
ContinueLoop
EndIf
; If data received
If $sRecv Then
MsgBox(64,"Réponse du serveur",$sRecv)
EndIf
Next
EndFunc
► Afficher le texte
Code : Tout sélectionner
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=Serveur.exe
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("IntraServeur SERVEUR", 274, 107, @DesktopWidth-280, 5,BitOR($WS_CLIPSIBLINGS, $WS_Border),$WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
Global $Label1 = GUICtrlCreateLabel("IntraServeur SERVEUR - Lancé.", 8, 30, 260, 81, $SS_CENTER)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global Const $maxClients = 10
Global $__Sockets[$maxClients] ; Array of connected sockets
Global $__IP = @IPAddress1, $__PORT = 8080
Global $__MainSocket, $iNewSocket, $sRecv
Global $ConnectedSocket
$p = PluginOpen("extendcmd.dll")
For $i = 0 To $maxClients - 1 ; Init the array (-1 => Invalid socket)
$__Sockets[$i] = -1
Next
TCPStartup()
Local $ipserveur = @IPAddress1
Local $PORT = 8090
; ---
$__MainSocket = TCPListen($__IP, $__PORT) ; Creating server
If @error Then Exit MsgBox(16, "Erreur", "Impossible de créer un socket d'écoute (" & @error & " - " & @extended & ")")
setconsolecolor(10)
ConsoleWrite("+> Serveur cr‚‚ : " & $__IP & ":" & $__PORT & @CRLF)
WinSetTitle(@ScriptFullPath,"","IntraServeur CONSOLE SERVEUR [" & $__IP & ":" & $__PORT & "]")
; ---
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)
setconsolecolor(11)
ConsoleWrite(">>> Nouveau client : " & _SocketToIP($hSocket) & @CRLF)
$ConnectedSocket = -1
$ConnectedSocket = TCPConnect($ipserveur, $PORT)
If @error Then
MsgBox(16,"Erreur","Impossible de se connecter au client !")
EndIf
TCPSend($ConnectedSocket,"Serveur connecté au client !")
EndFunc
; Trigered when some data received
Func _TCP_ReceiveFunc($hSocket, $sData)
If _IPAuth($hSocket) Then
If ($sData = "reboot") Then
setconsolecolor(7)
ConsoleWrite(">>> [" & @MDAY & "/" & @MON & " " & @HOUR & "h" & @MIN & "m" & @SEC & "s] [" & _ResolutionIP($hSocket) & "] Commande machine : Red‚marrage" & @CRLF)
FileWriteLine("log.txt",">>> [" & @MDAY & "/" & @MON & " " & @HOUR & "h" & @MIN & "m" & @SEC & "s] [" & _ResolutionIP($hSocket) & "] Commande machine : Redémarrage" & @CRLF)
TCPSend($ConnectedSocket,"Commande de redémarrage exécutée avec succès !")
EndIf
If ($sData = "shutdown") Then
setconsolecolor(7)
ConsoleWrite(">>> [" & @MDAY & "/" & @MON & " " & @HOUR & "h" & @MIN & "m" & @SEC & "s] [" & _ResolutionIP($hSocket) & "] Commande machine : Extinction" & @CRLF)
TCPSend($ConnectedSocket,"Commande d'extinction exécutée avec succès !")
FileWriteLine("log.txt",">>> [" & @MDAY & "/" & @MON & " " & @HOUR & "h" & @MIN & "m" & @SEC & "s] [" & _ResolutionIP($hSocket) & "] Commande machine : Extinction" & @CRLF)
EndIf
If (StringLeft($sData,4) = "au3:") Then
setconsolecolor(7)
ConsoleWrite(">>> [" & @MDAY & "/" & @MON & " " & @HOUR & "h" & @MIN & "m" & @SEC & "s] [" & _ResolutionIP($hSocket) & "] Commande Autoit : "& StringMid($sData,5) & @CRLF)
Execute(StringMid($sData,5))
FileWriteLine("log.txt",">>> [" & @MDAY & "/" & @MON & " " & @HOUR & "h" & @MIN & "m" & @SEC & "s] [" & _ResolutionIP($hSocket) & "] Commande Autoit : "& StringMid($sData,5) & @CRLF)
EndIf
If (StringLeft($sData,4) = "dos:") Then
setconsolecolor(7)
ConsoleWrite(">>> [" & @MDAY & "/" & @MON & " " & @HOUR & "h" & @MIN & "m" & @SEC & "s] [" & _ResolutionIP($hSocket) & "] Commande DOS : "& StringMid($sData,5) & @CRLF)
Run(StringMid($sData,5))
FileWriteLine("log.txt",">>> [" & @MDAY & "/" & @MON & " " & @HOUR & "h" & @MIN & "m" & @SEC & "s] [" & _ResolutionIP($hSocket) & "] Commande DOS : "& StringMid($sData,5) & @CRLF)
EndIf
Else
setconsolecolor(12)
ConsoleWrite(">>> [" & @MDAY & "/" & @MON & " " & @HOUR & "h" & @MIN & "m" & @SEC & "s] [" & _ResolutionIP($hSocket) & "] Commande bloqu‚e. IP non autoris‚e." & @CRLF)
TCPSend($ConnectedSocket,"Commande non exécutée. IP non autorisée.")
FileWriteLine("log.txt",">>> [" & @MDAY & "/" & @MON & " " & @HOUR & "h" & @MIN & "m" & @SEC & "s] [" & _ResolutionIP($hSocket) & "] Commande bloquée. IP non autorisée." & @CRLF)
EndIf
EndFunc
; Trigered when client disconnects
Func _TCP_ClientDisconnectFunc($hSocket)
setconsolecolor(11)
ConsoleWrite(">>> Client d‚connect‚ : " & _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
Func _ResolutionIP($ip)
$ippure = _SocketToIP($ip)
$name = INIRead("clients_res.ini","Names",$ippure,"")
If $name <> "" Then
Return $name
Else
Return $ippure
EndIf
EndFunc
Func _IPAuth($ip)
$ippure = _SocketToIP($ip)
$name = INIRead("clients_auth.ini","Names",$ippure,"")
If $name == "Allow" Then
Return True
Else
Return False
EndIf
EndFunc
PluginClose($p)Voilà. Merci d'avance pour votre aide.



