You have 2 scripts
If you press the “x” key, then the server send the message “fin” to all clients (Broadcast), the client then answers by the message “OK, j’ai bien reçu lordre de me déconnecter!” and disconnects.
The client only waits for server orders, if he receives “ping” he sends “pong”, and if he receives “fin”, he answers “OK, j’ai bien reçu lordre de me déconnecter!” and then, disconnects.
; Serveur
#NoTrayIcon
#include <Debug.au3>
#include "Server.au3"
_AutoItObject_Startup()
TCPStartup()
Global $runing = 1, $timer
; ##############################################################
_DebugSetup("TCP Server")
$oObj = _Class_TCPServer(@IPAddress1, 8083, 10, "supermotdepassesupersecret")
$oObj.Startup("_New", "_Lost", "_Recv", "_Receiving", "_TimedOut")
_DebugOut("Serveur en ligne")
HotKeySet("x", "_Kill")
$timer = TimerInit()
While $runing
$oObj.Process()
; ---
If TimerDiff($timer) >= 5000 Then
$oObj.Broadcast("ping")
$timer = TimerInit()
EndIf
WEnd
Func _Kill()
_DebugOut("Demande au(x) client(s) de se déconnecter...")
$oObj.Broadcast("fin")
EndFunc
Func _New($hSocket, $sIP)
_DebugOut("Client connecté! (" & $sIP & ")")
$oObj.Send($hSocket, "ping")
EndFunc
Func _Lost($hSocket, $sIP)
_DebugOut("Client déconnecté! (" & $sIP & ")")
EndFunc
Func _Recv($hSocket, $sIP, $data)
_DebugOut("Reçu de " & $sIP & ": " & $data)
EndFunc
Func _Receiving($hSocket, $sIP, $len)
_DebugOut("Reception en cours de " & $sIP & " (" & $len & ")")
EndFunc
Func _TimedOut($hSocket, $sIP, $len)
EndFunc
; ##############################################################
TCPShutdown()
_AutoItObject_Shutdown(); Client
#NoTrayIcon
#include <Debug.au3>
#include "Client.au3"
_AutoItObject_Startup()
TCPStartup()
Global $runing = 1
; ##############################################################
_DebugSetup("TCP Client - PID: " & @AutoItPID)
$oObj = _Class_TCPClient(@IPAddress1, 8083, "supermotdepassesupersecret")
Do
Until $oObj.connect("_Recv", "_Receiving", "_LostConnection", "_TimedOut") > 0
_DebugOut("Client connecté!")
While $runing
$oObj.Process()
WEnd
Func _Recv($data)
_DebugOut("Reçu: " & $data)
Switch $data
Case "fin"
$oObj.Send("OK, j''ai bien reçu lordre de me déconnecter!")
$runing = 0
Case "ping"
$oObj.Send("pong")
EndSwitch
; ---
$oObj.Send("Bien reçu!")
EndFunc
Func _Receiving($bufferLen)
_DebugOut("Reception ... " & $bufferLen)
EndFunc
Func _LostConnection()
_DebugOut("Connexion perdue!")
EndFunc
Func _TimedOut($lostData_len)
_DebugOut("Timed Out (" & $lostData_len & " perdus)")
EndFunc
; ##############################################################
TCPShutdown()
_AutoItObject_Shutdown()