Jump to content

YATCP (Yet another TCP) client/server UDF


SlowCoder74
 Share

Recommended Posts

As part of one of my projects at work that needs TCP communication, I decided to build my own TCP server/client pair.  I tried to build it so that it is fairly self-managed, easy to implement into network projects, and contains a lot of useful inbuilt metrics.

Supported metrics (supported both server and client side):

- Number of current connections

- Client and server connection test

- Bytes sent / received / processed

- Packets sent / received / processed

- Number of clients connected

- Sockets / IP address / computer name

- Connection timestamp / elapsed time

- Last packet received timestamp / elapsed time

- Last packet sent timestamp / elapsed time

Functionality (on top of standard TCP functions):

- Configurable to send an initial "connection packet" upon connection, client or server-side.

- Connection keep-alive packets at specific intervals

- "Stale" connection disconnects for old, idle connections

- Selective client broadcasting.  On the server, clients can be individually selected to rebroadcast data received from them.

- Automatic IP/socket/computer name detection

- Connection state change detection.  e.g. This facilitates updating a GUI connection list when clients have connected or disconnected.

I would appreciate your constructive comments and suggestions.

I am including the UDF, and sample server and client.

Routines_TCP.au3

TCP Server.au3

TCP Client.au3

Link to comment
Share on other sites

The first issue I notice is declaring Global variables inside a function is bad UDF design. Then you may wish to run your UDF through Jos' Tidy program and  Au3Check with these parameters at the top of your script.

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • 6 months later...

I study this script, but I can't understand because sometimes in the code the author use a loop for iteration... like this...

for $iIter = 1 to 5

I comment this loop and it seems nothing is changing... why this loop?

Br, Detefon

; Routines_TCP.au3 line ~280
;-------------------------------------------------------------------------------------------
;-- FUNCTION: _TCPServer_GetPacket - Updated 20140611 --------------------------------------
;-------------------------------------------------------------------------------------------
Func _TCPServer_GetPacket($iTCPClientElem)
    ;parse specified client's data queue and retrieve next packet
    ;returns full packet

    if Not _TCP_ConnInfo("IsConnected",$iTCPClientElem) then Return

    ;first, see if there is any data in the TCP stack to add to the client's pre-process queue
    Local $sReceivedData = ""
    for $iIter = 1 to 5
        $sReceivedData = TCPRecv($aTCPClientSocket[$iTCPClientElem], $iTCPReceiveChars)
        Switch @error
            Case 0 ;no error.  process normally
                if $sReceivedData <> "" Then
                    ;add received data to client's data queue
                    $aTCPClientReceivedData[$iTCPClientElem] = $aTCPClientReceivedData[$iTCPClientElem] & $sReceivedData
                    $aTCPClientDataLastReceivedTimestamp[$iTCPClientElem] = _NowCalc() ;mark the last data received timestamp
                EndIf
            Case -1 ;if using AutoIt 3.3.10.0 or higher, this error is actually "no data received", not an error.  This error code does not exist in previous versions of AutoIt.
                $sReceivedData = ""
            Case Else ;TCP error.  disconnect
                _TCPServer_Disconnect($iTCPClientElem)
                $sReceivedData = ""
        EndSwitch
        if $sReceivedData = "" then ExitLoop
    Next
Edited by Detefon

Visit my repository

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...