Examples

Here are some examples

Server

; Serveur
#NoTrayIcon

#include "..\Server.au3"
#include <Array.au3>

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>

#region ### START Koda GUI section ###
$GUI_Main = GUICreate("Mini-Server", 408, 228)
$tab = GUICtrlCreateTab(6, 6, 397, 217)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

_AutoItObject_Startup()
TCPStartup()

; Wee need 4 extended vars per client to stor: CtrlIDs of:
;   Tab Ctrl, Edit, Button Send, Button Disconnect, Input
$oSrv = _Class_TCPServer("127.0.0.1", 3258, 5, 5)
If Not $oSrv.Startup("_TCP_NewClient", "_TCP_LostClient", "_TCP_Recv") Then Exit MsgBox(16, "Error", "Cannot startup server")

; ##############################################################

Func _TCP_NewClient($iSocket, $sIP)
    ; We create a tab ctrl for the new client
    ConsoleWrite("New Client!" & @CRLF)
    $oSrv.ClientPropertySet($iSocket, 0, GUICtrlCreateTabItem($sIP))
    $oSrv.ClientPropertySet($iSocket, 1, GUICtrlCreateEdit("", 12, 36, 379, 151, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL), $WS_EX_STATICEDGE))
    $oSrv.ClientPropertySet($iSocket, 2, GUICtrlCreateInput("", 12, 192, 211, 21))
    $oSrv.ClientPropertySet($iSocket, 3, GUICtrlCreateButton("Send", 228, 192, 75, 21))
    $oSrv.ClientPropertySet($iSocket, 4, GUICtrlCreateButton("Disconnect", 312, 192, 75, 21))
    GUICtrlCreateTabItem("")
EndFunc   ;==>_TCP_NewClient

Func _TCP_LostClient($iSocket, $sIP)
    ; we delete the client's tab ctrl
    ConsoleWrite("Lost Client!" & @CRLF)
    Local $tab = $oSrv.ClientPropertyGet($iSocket, 0)
    GUICtrlDelete($tab)
EndFunc   ;==>_TCP_LostClient

Func _TCP_Recv($iSocket, $sIP, $Data)
    ConsoleWrite("Recv from " & $sIP & ": " & $Data & @CRLF)
    Local $edit = $oSrv.ClientPropertyGet($iSocket, 1)
    GUICtrlSetData($edit, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] " & $Data & @CRLF, 1)
EndFunc   ;==>_TCP_Recv

; ##############################################################

Func __Process($msg)
    ; If no message
    If $msg = 0 Then Return
    ; ---
    Local $Sockets = $oSrv.SocketIDList()
    ; If no connected client
    If $Sockets[0] = 0 Then Return
    ; ---
    Local $input, $edit, $b_send, $b_disconnect, $read
    For $i = 1 To $Sockets[0]
        ; Retreive client's ctrlIDs
        $edit = $oSrv.ClientPropertyGet($Sockets[$i], 1)
        $input = $oSrv.ClientPropertyGet($Sockets[$i], 2)
        $b_send = $oSrv.ClientPropertyGet($Sockets[$i], 3)
        $b_disconnect = $oSrv.ClientPropertyGet($Sockets[$i], 4)
        ; ---
        Switch $msg
            Case $b_send ; Send message
                $read = GUICtrlRead($input)
                If $read Then
                    If $oSrv.Send($Sockets[$i], $read) Then
                        ConsoleWrite("Socket " & $Sockets[$i] & " send: " & $read & @CRLF)
                        GUICtrlSetData($edit, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] YOU: " & $read & @CRLF, 1)
                        GUICtrlSetData($input, "")
                    EndIf
                EndIf
            Case $b_disconnect ; Dosconnects client
                ConsoleWrite("Disconnecting socket " & $Sockets[$i] & @CRLF)
                $oSrv.Disconnect($Sockets[$i])
        EndSwitch
    Next
EndFunc   ;==>__Process

; ##############################################################

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    ; ---
    $oSrv.Process()
    __Process($nMsg)
WEnd

Client

; Client
#NoTrayIcon

#include "..\Client.au3"

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

_AutoItObject_Startup()
TCPStartup()

; Connect to server
$oClient = _Class_TCPClient("127.0.0.1", 3258)
If Not $oClient.Connect("_TCP_Recv", "", "_TCP_LostConnection") Then Exit MsgBox(16, "Error", "Cannot connect to server.")

#Region ### START Koda GUI section ### Form=
$GUI_Main = GUICreate("Mini-Client", 301, 197)
$Edit = GUICtrlCreateEdit("", 6, 6, 287, 155, BitOR($ES_AUTOVSCROLL,$ES_READONLY,$ES_WANTRETURN,$WS_VSCROLL), $WS_EX_STATICEDGE)
$Input = GUICtrlCreateInput("", 6, 168, 229, 21)
$B_Send = GUICtrlCreateButton("Send", 240, 168, 51, 21)
Global $accels[1][2] = [["{enter}", $B_Send]]
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $read

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $B_Send
            $read = GuiCtrlRead($Input)
            If $read Then
                If $oClient.Send($read) Then
                    GuiCtrlSetData($Edit, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] " & $read & @CRLF, 1)
                    GuiCtrlSetData($Input, "")
                EndIf
            EndIf
    EndSwitch
    ; ---
    $oClient.Process()
WEnd

Func _TCP_Recv($Data) ; Whene receive data, write it
    GuiCtrlSetData($Edit, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] " & $Data & @CRLF, 1)
EndFunc

Func _TCP_LostConnection() ; Whene connection is lost, Exit
    Exit MsgBox(16, "Disconnected", "Lost connection with server")
EndFunc
Close