SlowCoder74 Posted June 17, 2014 Share Posted June 17, 2014 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.au3TCP Server.au3TCP Client.au3 Luigi and mLipok 2 Link to comment Share on other sites More sharing options...
guinness Posted June 18, 2014 Share Posted June 18, 2014 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 parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
Luigi Posted December 29, 2014 Share Posted December 29, 2014 (edited) 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 December 29, 2014 by Detefon Visit my repository Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now