This UDF contains function for simply handling a TCP Client.
This script is an example of using this UDF, it’s a very basic TCP Chat client. You can use it with the TCP Server example.
;----------------------------------------------------------------------------
;
; AutoIt Version: 3.3.6.1
; Author: Matwachich
;
; Script Function:
;
;
;----------------------------------------------------------------------------
#NoTrayIcon
#include <GuiEdit.au3>
#include "TCPClient.au3"
TCPStartup()
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
; On demande un nom d'utilisateur
; Wee need a user name
Do
$__NickName = InputBox("Chat", "Entrez votre pseudo")
If @error Then Exit
Until $__NickName
#Region ### START Koda GUI section ### Form=
$GUI = GUICreate("Chat", 378, 256, 384, 171)
$Edit = GUICtrlCreateEdit("", 6, 6, 365, 215, BitOR($ES_AUTOVSCROLL,$ES_READONLY,$ES_WANTRETURN,$WS_VSCROLL))
$Input = GUICtrlCreateInput("", 6, 228, 283, 21)
$Button1 = GUICtrlCreateButton("Envoyer", 294, 228, 75, 21)
Global $accels[1][2] = [["{enter}", $Button1]]
GuiSetAccelerators($accels)
$accels = 0
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $tmp, $connTimer = TimerInit()
; On créer un client et on lui assigne des fonction callback
; Client creation, and callbacks functions assigning
Global $Client = _TCPClient_Create(@IPAddress1, 53698, "motdepassedecryptagedesdonneessupersecret")
_TCPClient_SetCallbacks($Client, "_TCP_Recv")
; Boucle principale
; Main loop
While 1
Switch GUIGetMsg()
; Avant de quitter, on detruit le client
; Befor exit, we destroy the client
Case $GUI_EVENT_CLOSE
_TCPClient_Destroy($Client)
TCPShutdown()
Exit
; ---
; Envoi d'un message, seulement si le Input contient quelque chose
; Sending message, only if there is something in the Input
Case $Button1
$tmp = GuiCtrlRead($Input)
If $tmp Then
_TCPClient_Send($Client, $tmp)
GuiCtrlSetData($Input, "")
EndIf
; ---
EndSwitch
; ---
; Si le client est déconnecté, on essay de se connecter 1 fois / seconde
; If the client is disconnected, then we indefinitly try to connect once per second
If Not _TCPClient_IsConnected($Client) Then
If TimerDiff($connTimer) >= 1000 Then
_TCPClient_Connect($Client)
$connTimer = TimerInit()
EndIf
EndIf
; ---
; Fonction de traitement
; Processing function
_TCPClient_Process()
WEnd
; Fonction appelée à la reception d'un message depuis le serveur
; Function called when a message is received from the server
Func _TCP_Recv($iClient, $Data)
; Cette ligne ne sert à rien puisque nous n'avons qu'un seul client dans le script, mais si nous
; en avion plusieurs, elle permetterai si tous les clients avait la même fonction callback de réception,
; de savoir de quel client émane le message reçu
; This line isn't very usefull in our example, but it would be usefull in a script containing many clients
; in order to (if all the client had the same callback function) know which client is concerned by the event
If $iClient <> $Client Then Return
; ---
; Si le message reçu est une requète d'identification, alors on s'identify au pres du serveur
; If the message received is an identification request, then we identify ourself to the server
If $Data = "#IDENTIFY#" Then
_TCPClient_Send($iClient, "#NAME#" & $__NickName)
Return
EndIf
; ---
; Si non, ça veut dir que c'est un simple message, alors on l'affiche de le Edit
; Else, it's a simple chat message, so we display it in the Edit
Local $read = GuiCtrlRead($Edit)
GuiCtrlSetData($Edit, $read & $Data & @CRLF)
_GUICtrlEdit_LineScroll($Edit, 0, _GUICtrlEdit_GetLineCount($Edit))
EndFunc| TCPClient | This UDF contains function for simply handling a TCP Client. |
| Creation/ | |
| Functions | |
| _TCPClient_Create | Create a TCP Client |
| _TCPClient_Destroy | Destroy a client |
| _TCPClient_Connect | Order a Client to connect |
| _TCPClient_Disconnect | Order a client to disconnect |
| _TCPClient_IsConnected | Check if a client is connected |
| Configuration | |
| Functions | |
| _TCPClient_Config | Set client’s configuration |
| _TCPClient_SetCallbacks | Set the callback functions that will be called when an event occurs |
| Communication with server | |
| Functions | |
| _TCPClient_Send | Send data to the server |
| Processing | |
| Functions | |
| _TCPClient_Process | This function makes all clients work, all data is processed here, and all callbacks are called from here |
| Functions | |
| _TCPClient_Create | Create a TCP Client |
| _TCPClient_Destroy | Destroy a client |
| _TCPClient_Connect | Order a Client to connect |
| _TCPClient_Disconnect | Order a client to disconnect |
| _TCPClient_IsConnected | Check if a client is connected |
Create a TCP Client
_TCPClient_Create($sIP, $iPort, $sCryPwd = "", $iTimeOut = 60)
| $sIP | Server’s IP Address |
| $iPort | Server’s listening port |
| $sCryPwd | If specified, then the exchanged data between server and clients will be encrypted (must be the same as the client) |
| $iCompressLvl | ZLib compression level, 0 - No compression, 9 - Max compression |
| $iTimeOut | Server Time-out (seconds) |
| Succes | A Client Handle |
| Failed | 0 and set @error to 1 if maximum clients number is already reached, in this case, you must destroy some other client to create a new one (see _TCPClient_Destroy). |
Destroy a client
_TCPClient_Destroy($iClient)
| $iClient | Client Handle (Returned by _TCPClient_Create) |
| Sucess | 1 |
| Failed | 0 and set @error to -1 if the Client Handle isn’t valid |
If the client was connect, then the _TCPClient_Disconnect function is called befor destroying the client. Consequently, the Lost Connexion Callback function will be called for this client.
Order a Client to connect
_TCPClient_Connect($iClient)
| $iClient | Client Handle (Returned by _TCPClient_Create) |
| Succes | 1 |
| Failed | 0 (Check Server IP, and Port) |
Order a client to disconnect
_TCPClient_Disconnect($iClient)
| $iClient | Client Handle (Returned by _TCPClient_Create) |
| Sucess | 1 |
| Failed | 0 and set @error to |
Check if a client is connected
_TCPClient_IsConnected($iClient)
| $iClient | Client Handle (Returned by _TCPClient_Create) |
| Succes | 1 if the client is connected, 0 if it isn’t connect |
| Failed | 0 and set @error to -1 if the Client Handle isn’t valid |
| Functions | |
| _TCPClient_Config | Set client’s configuration |
| _TCPClient_SetCallbacks | Set the callback functions that will be called when an event occurs |
Set client’s configuration
_TCPClient_Config($iClient, $sIP = Default, $iPort = Default, $iTimeOut = Default)
| $iClient | Client Handle (Returned by Creation/Destruction, Connect/Disconnect._TCPClient_Create) |
| $sIP | Server’s IP Address |
| $iPort | Server’s listening port |
| $iTimeOut | Server Time-out (seconds) |
| Succes | 1 |
| Failed | 0 and set @error to -1 if the Client Handle isn’t valid |
Set the callback functions that will be called when an event occurs
_TCPClient_SetCallbacks($iClient, $sCB_Recv = Default, $sCB_Receiving = Default, $sCB_LostConnection = Default, $sCB_TimedOut = Default)
| $iClient | Client Handle (Returned by Creation/Destruction, Connect/Disconnect._TCPClient_Create) |
| $sCB_Recv | Function called when data is received from the server |
| $sCB_Receiving | Function called when being receiving data from the server |
| $sCB_LostConnection | Function called when disconnecting from the server |
| $sCB_TimedOut | Function called when the server is timed out |
| Succes | 1 |
| Failed | 0 and set @error to -1 if the Client Handle isn’t valid |
The syntax for each callback function is:
| $iClient | Client Handle concerned by the event |
| $Data | Data received, either String or Binary (is it was sent) |
| $iBufferLenght | Lenght of the buffer at the moment whet the function is called |
When the server is timed out, the buffer will be flushed, so $iBufferLenght corresponds to the amount of lost data.
| Functions | |
| _TCPClient_Send | Send data to the server |
Send data to the server
_TCPClient_Send($iClient, $Data)
| $iClient | Client Handle (Returned by Creation/Destruction, Connect/Disconnect._TCPClient_Create) |
| $Data | Data we want to send (See remark) |
| Succes | Returns number of bytes sent. |
| Failed | 0 and set @error to |
About $Data parameter, anything other than Binary data will be sent as string, and received as a string. Binary data will be sent as it is, and received as binary too.
| Functions | |
| _TCPClient_Process | This function makes all clients work, all data is processed here, and all callbacks are called from here |