If WinExists(@ScriptName) Then Exit
AutoItWinSetTitle(@ScriptName)
#NoTrayIcon
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <GuiButton.au3>
#include <GuiEdit.au3>
#include <Array.au3>
#include <Date.au3>
Opt("TrayMenuMode",1)
TraySetState(1)
Global $Server_IP = InputBox("Chat Client", "Gimme The Server IP Address", "192.168.1.6", "", 300, 200)
Global $Server_Port = 33300
Global $Lisening_IP = @IPAddress1, $Lisening_port = 33300, $Connected_Socket[101], $Number_Of_Connections = 1
Global $Active_Connection, $Data_Vith_Server = "", $Data_Vidouth_Server = ""
For $i = 1 To 100
$Connected_Socket[$i] = -1
Next
TCPStartup()
$Lisening_Socket = TCPListen($Lisening_IP, $Lisening_port, 100)
If $Lisening_Socket = -1 Then Exit
$Socket_To_Send = TCPConnect($Server_IP, $Server_Port)
If $Socket_To_Send = -1 Then Exit
$Client_Window = GUICreate("TCP Control Tool", 600, 250, -1, -1, BitOR($WS_POPUP, $WS_SYSMENU, $WS_CAPTION))
$Client_Edit = GUICtrlCreateEdit("", 10, 40, 580, 200, BitOR ($ES_READONLY, $WS_VSCROLL, $ES_AUTOVSCROLL))
$Client_Input = GUICtrlCreateInput("", 10, 10, 250, 20)
$Client_Buttom = GUICtrlCreateButton("Send", 260, 10, 80, 20, $BS_DEFPUSHBUTTON)
$Client_Check = GUICtrlCreateCheckbox ("Server dialog", 350, 10, 100, 20)
GUISetState()
While 1
$Msg = GUIGetMsg()
If $Msg = $GUI_EVENT_CLOSE Then ExitLoop
If $Msg = $Client_Check Then
If GUICtrlRead ($Client_Check) = 1 Then GUICtrlSetData($Client_Edit, $Data_Vith_Server)
If GUICtrlRead ($Client_Check) = 4 Then GUICtrlSetData($Client_Edit, $Data_Vidouth_Server)
EndIf
#Region Send To Server
If $Msg = $Client_Buttom Then Set_To_Edit(GUICtrlRead ($Client_Input), 1)
#EndRegion Send To Server
#Region Resive From Client
$Connected_Socket[$Number_Of_Connections] = TCPAccept($Lisening_Socket)
If $Connected_Socket[$Number_Of_Connections] >= 0 Then
Set_To_Edit ("Connected client from IP " & Socket2IP($Connected_Socket[$Number_Of_Connections]), 5)
$Number_Of_Connections += 1
EndIf
For $i = 1 To $Number_Of_Connections - 1
$Message_From_Client = TCPRecv($Connected_Socket[$i], 512)
If $Message_From_Client <> "" Then
$Connected_Socket[0] = $i
Set_To_Edit ($Message_From_Client, 4)
Switch $Message_From_Client
Case "~Terminate Connection~"
Set_To_Edit ("Executing command " & $Message_From_Client, 3)
TCPCloseSocket($Connected_Socket[$Connected_Socket[0]])
$Connected_Socket[$Connected_Socket[0]] = -1
_ArraySort ($Connected_Socket, 1, 1)
$Number_Of_Connections -= 1
Set_To_Edit ("Command executing susesfully " & $Message_From_Client, 5)
Case "~Hide Tray Icon~"
Set_To_Edit ("Executing command " & $Message_From_Client, 3)
TraySetState(2)
Set_To_Edit ("Command executing susesfully " & $Message_From_Client, 5)
Case "~Show Tray Icon~"
Set_To_Edit ("Executing command " & $Message_From_Client, 3)
TraySetState(1)
Set_To_Edit ("Command executing susesfully " & $Message_From_Client, 5)
Case "~Attention Tray Icon On~"
Set_To_Edit ("Executing command " & $Message_From_Client, 3)
TraySetState(4)
Set_To_Edit ("Command executing susesfully " & $Message_From_Client, 5)
Case "~Attention Tray Icon Off~"
Set_To_Edit ("Executing command " & $Message_From_Client, 3)
TraySetState(8)
Set_To_Edit ("Command executing susesfully " & $Message_From_Client, 5)
Case Else
Set_To_Edit ("Server is ressive message " & $Message_From_Client, 3)
EndSwitch
EndIf
Next
#EndRegion Resive From Client
#Region Resive Ansver From Server
$Message_From_Server = TCPRecv($Socket_To_Send, 512)
$Err = @error
If $Err <> 0 Then ExitLoop
If $Err = 0 And $Message_From_Server <> "" Then Set_To_Edit($Message_From_Server, 2)
#EndRegion Resive Ansver From Server
WEnd
Func OnAutoItExit()
TCPSend ($Socket_To_Send, "~Terminate Connection~")
TCPCloseSocket($Lisening_Socket)
TCPCloseSocket($Connected_Socket[1])
TCPCloseSocket($Socket_To_Send)
TCPShutdown()
EndFunc
Func Set_To_Edit ($Message, $Config)
Select
Case $Config = 1
$Message_To_Server = TCPSend($Socket_To_Send, $Message)
GUICtrlSetData($Client_Input, "")
$Data_Vidouth_Server = _NowCalc () & " || " & $Server_IP & @TAB & "<--" & @TAB & $Message & @CRLF & $Data_Vidouth_Server
$Data_Vith_Server = _NowCalc () & " || " & $Server_IP & @TAB & "<--" & @TAB & $Message & @CRLF & $Data_Vith_Server
Case $Config = 2
$Data_Vidouth_Server = _NowCalc () & " || " & $Server_IP & @TAB & "-->" & @TAB & $Message & @CRLF & $Data_Vidouth_Server
$Data_Vith_Server = _NowCalc () & " || " & $Server_IP & @TAB & "-->" & @TAB & $Message & @CRLF & $Data_Vith_Server
Case $Config = 3
TCPSend ($Connected_Socket[$Connected_Socket[0]], $Message)
$Data_Vith_Server = _NowCalc () & " || " & Socket2IP($Connected_Socket[$Connected_Socket[0]]) & @TAB & "<--" & @TAB & $Message & @CRLF & $Data_Vith_Server
Case $Config = 4
$Data_Vith_Server = _NowCalc () & " || " & Socket2IP($Connected_Socket[$Connected_Socket[0]]) & @TAB & "-->" & @TAB & $Message & @CRLF & $Data_Vith_Server
Case $Config = 5
$Data_Vith_Server = _NowCalc () & " || SERVER" & @TAB & ">-<" & @TAB & $Message & @CRLF & $Data_Vith_Server
EndSelect
ConsoleWrite ($Data_Vidouth_Server & @CRLF & @CRLF)
If GUICtrlRead ($Client_Check) = 1 Then GUICtrlSetData($Client_Edit, $Data_Vith_Server)
If GUICtrlRead ($Client_Check) = 4 Then GUICtrlSetData($Client_Edit, $Data_Vidouth_Server)
EndFunc
Func Socket2IP($Socket)
Local $sockaddr, $aRet
$sockaddr = DllStructCreate("short;ushort;uint;char[8]")
$aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $Socket, _
"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