lgvlgv Posted September 21, 2011 Author Share Posted September 21, 2011 (edited) ok the quote for autoit script desent work for me any more??? u have to cut and paste the client adds this values when connection u can put the in as varibles or tell me and i send the client aswell.. _TCP_Send($hSocket, "~~username|" & @username & '|' & @ComputerName & "|" & @IPAddress1 & "|" & $PC_ID & "->" & @ComputerName) ------------------------------------ #include <Array.au3> #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <GuiStatusBar.au3> #include <GuiListBox.au3> #include <GuiListView.au3> #include <ListViewConstants.au3> #include <StaticConstants.au3> #include <TabConstants.au3> #include <WindowsConstants.au3> #include "TCP.au3" Global $ListViewClients ToolTip("SERVER: Creating server...", 10, 30) $hServer = _TCP_Server_Create(1451); A server. Tadaa! _TCP_RegisterEvent($hServer, $TCP_NEWCLIENT, "NewClient"); Whooooo! Now, this function (NewClient) get's called when a new client connects to the server. _TCP_RegisterEvent($hServer, $TCP_DISCONNECT, "Disconnect"); And this,... this will get called when a client disconnects. _TCP_RegisterEvent($hServer, $TCP_SEND, "Send") _TCP_RegisterEvent($hServer, $TCP_RECEIVE, "Received"); Function "Received" will get called when something is received Opt("GUIOnEventMode", 1) #Region ### START Koda GUI section ### Form=H:\Jobb\scripts\Dev\ChargerServer 2\ChargeServer.kxf $MainForm = GUICreate("Ch@rge Remote Control", 998, 647, 393, 348, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_CAPTION, $WS_OVERLAPPEDWINDOW, $WS_TILEDWINDOW, $WS_MINIMIZE, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_TABSTOP, $WS_BORDER, $WS_CLIPSIBLINGS)) GUISetIcon("logo.ico", 0) $MenuItemArkiv = GUICtrlCreateMenu("&Arkiv") $MenuItemExit = GUICtrlCreateMenuItem("Exit", $MenuItemArkiv) $MenuItemUpdate = GUICtrlCreateMenuItem("Update", $MenuItemArkiv) $MenuHelp = GUICtrlCreateMenu("&Help") $MenuItemAbout = GUICtrlCreateMenuItem("About", $MenuHelp) GUISetOnEvent($GUI_EVENT_CLOSE, "MainFormClose") GUISetOnEvent($GUI_EVENT_MINIMIZE, "MainFormMinimize") GUISetOnEvent($GUI_EVENT_MAXIMIZE, "MainFormMaximize") GUISetOnEvent($GUI_EVENT_RESTORE, "MainFormRestore") $ListViewClients = GUICtrlCreateListView("UserName|ComputerName|IpAddress|Ping|PCID|Socket", 0, 40, 377, 561) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 100) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 50) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 50) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 3, 50) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 4, 50) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 5, 50) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 6, 50) GUICtrlSetResizing(-1, $GUI_DOCKAUTO) GUICtrlSetOnEvent(-1, "ListViewClientsClick") $StatusBar1 = _GUICtrlStatusBar_Create($MainForm) _GUICtrlStatusBar_SetMinHeight($StatusBar1, 23) $TabMain = GUICtrlCreateTab(384, 40, 609, 561) GUICtrlSetResizing(-1, $GUI_DOCKAUTO + $GUI_DOCKWIDTH + $GUI_DOCKHEIGHT) $TabConsole = GUICtrlCreateTabItem("Console") $ListViewConsole = GUICtrlCreateListView("", 392, 104, 577, 449, BitOR($LVS_LIST, $LVS_SINGLESEL, $LVS_SHOWSELALWAYS, $WS_HSCROLL)) GUICtrlSetResizing(-1, $GUI_DOCKAUTO) GUICtrlSetOnEvent(-1, "ListViewConsoleClick") $InputSendCommand = GUICtrlCreateInput("Type a command....", 388, 561, 489, 21) GUICtrlSetOnEvent(-1, "InputSendCommandChange") $ButtonSendCommand = GUICtrlCreateButton("Send", 884, 561, 73, 25, $WS_GROUP) GUICtrlSetOnEvent(-1, "ButtonSendCommandClick") $TabSystemInfo = GUICtrlCreateTabItem("System Info") GUICtrlSetState(-1, $GUI_SHOW) $Input1 = GUICtrlCreateInput("Input1", 496, 136, 169, 21) GUICtrlSetOnEvent(-1, "Input1Change") $ComputerName = GUICtrlCreateLabel("Computer Name:", 408, 138, 83, 17) GUICtrlSetOnEvent(-1, "ComputerNameClick") $Input2 = GUICtrlCreateInput("Input1", 496, 160, 169, 21) GUICtrlSetOnEvent(-1, "Input2Change") $Label1 = GUICtrlCreateLabel("User Name:", 431, 162, 60, 17) GUICtrlSetOnEvent(-1, "Label1Click") $Input3 = GUICtrlCreateInput("Input1", 496, 208, 169, 21) GUICtrlSetOnEvent(-1, "Input3Change") $Label2 = GUICtrlCreateLabel("Primary Owner:", 416, 210, 75, 17) GUICtrlSetOnEvent(-1, "Label2Click") $Label3 = GUICtrlCreateLabel("Operating System:", 401, 186, 90, 17) GUICtrlSetOnEvent(-1, "Label3Click") $Input4 = GUICtrlCreateInput("Input1", 496, 184, 169, 21) GUICtrlSetOnEvent(-1, "Input4Change") $Input5 = GUICtrlCreateInput("Input1", 777, 160, 169, 21) GUICtrlSetOnEvent(-1, "Input5Change") $Input6 = GUICtrlCreateInput("Input1", 777, 184, 169, 21) GUICtrlSetOnEvent(-1, "Input6Change") $Label4 = GUICtrlCreateLabel("MAC Adress:", 706, 186, 65, 17) GUICtrlSetOnEvent(-1, "Label4Click") $Label5 = GUICtrlCreateLabel("IP Adress:", 719, 162, 52, 17) GUICtrlSetOnEvent(-1, "Label5Click") $Label6 = GUICtrlCreateLabel("Domain/ WorkGrp:", 677, 138, 94, 17) GUICtrlSetOnEvent(-1, "Label6Click") $Label7 = GUICtrlCreateLabel("Date / Time:", 427, 234, 64, 17) GUICtrlSetOnEvent(-1, "Label7Click") $Input7 = GUICtrlCreateInput("Input1", 497, 232, 169, 21) GUICtrlSetOnEvent(-1, "Input7Change") $Input8 = GUICtrlCreateInput("Input1", 777, 136, 169, 21) GUICtrlSetOnEvent(-1, "Input8Change") $Input9 = GUICtrlCreateInput("Input1", 461, 457, 201, 21) GUICtrlSetOnEvent(-1, "Input9Change") $Label8 = GUICtrlCreateLabel("Description:", 405, 459, 60, 17) GUICtrlSetOnEvent(-1, "Label8Click") $Label9 = GUICtrlCreateLabel("Processor #1:", 395, 435, 70, 17) GUICtrlSetOnEvent(-1, "Label9Click") $Input10 = GUICtrlCreateInput("Input1", 461, 433, 201, 21) GUICtrlSetOnEvent(-1, "Input10Change") $Input11 = GUICtrlCreateInput("Input1", 461, 370, 201, 21) GUICtrlSetOnEvent(-1, "Input11Change") $Label10 = GUICtrlCreateLabel("BIOS Date:", 407, 371, 58, 17) GUICtrlSetOnEvent(-1, "Label10Click") $Input12 = GUICtrlCreateInput("Input1", 461, 345, 201, 21) GUICtrlSetOnEvent(-1, "Input12Change") $Label11 = GUICtrlCreateLabel("BIOS:", 433, 347, 32, 17) GUICtrlSetOnEvent(-1, "Label11Click") $Input13 = GUICtrlCreateInput("Input1", 461, 321, 201, 21) GUICtrlSetOnEvent(-1, "Input13Change") $Label12 = GUICtrlCreateLabel("Motherboard:", 398, 323, 67, 17) GUICtrlSetOnEvent(-1, "Label12Click") $Input14 = GUICtrlCreateInput("Input1", 776, 257, 169, 21) GUICtrlSetOnEvent(-1, "Input14Change") $Label13 = GUICtrlCreateLabel("DeskTop Res:", 698, 259, 73, 17) GUICtrlSetOnEvent(-1, "Label13Click") $Input15 = GUICtrlCreateInput("Input1", 776, 233, 169, 21) GUICtrlSetOnEvent(-1, "Input15Change") $Label14 = GUICtrlCreateLabel("Startup Options:", 691, 235, 80, 17) GUICtrlSetOnEvent(-1, "Label14Click") $Input16 = GUICtrlCreateInput("Input1", 776, 209, 169, 21) GUICtrlSetOnEvent(-1, "Input16Change") $Label15 = GUICtrlCreateLabel("System Type:", 703, 211, 68, 17) GUICtrlSetOnEvent(-1, "Label15Click") $Group1 = GUICtrlCreateGroup(" General Information ", 392, 104, 593, 185) GUICtrlCreateGroup("", -99, -99, 1, 1) $Input17 = GUICtrlCreateInput("Input1", 461, 394, 201, 21) GUICtrlSetOnEvent(-1, "Input17Change") $Label16 = GUICtrlCreateLabel("SrNr BIOS:", 405, 396, 56, 17) GUICtrlSetOnEvent(-1, "Label16Click") $Label17 = GUICtrlCreateLabel("Socket:", 424, 556, 41, 17) GUICtrlSetOnEvent(-1, "Label17Click") $Input18 = GUICtrlCreateInput("Input1", 461, 530, 201, 21) GUICtrlSetOnEvent(-1, "Input18Change") $Label18 = GUICtrlCreateLabel("L2 Cache:", 412, 532, 53, 17) GUICtrlSetOnEvent(-1, "Label18Click") $Label19 = GUICtrlCreateLabel("AddressWith:", 398, 508, 67, 17) GUICtrlSetOnEvent(-1, "Label19Click") $Input19 = GUICtrlCreateInput("Input1", 461, 506, 201, 21) GUICtrlSetOnEvent(-1, "Input19Change") $Input20 = GUICtrlCreateInput("Input1", 461, 481, 201, 21) GUICtrlSetOnEvent(-1, "Input20Change") $Label20 = GUICtrlCreateLabel("ClockSpeed:", 400, 484, 65, 17) GUICtrlSetOnEvent(-1, "Label20Click") $Input21 = GUICtrlCreateInput("Input1", 461, 554, 201, 21) GUICtrlSetOnEvent(-1, "Input21Change") $Input22 = GUICtrlCreateInput("Input1", 776, 531, 201, 21) GUICtrlSetOnEvent(-1, "Input22Change") $Input23 = GUICtrlCreateInput("Input1", 776, 555, 201, 21) GUICtrlSetOnEvent(-1, "Input23Change") $Input24 = GUICtrlCreateInput("Input1", 776, 507, 201, 21) GUICtrlSetOnEvent(-1, "Input24Change") $Input25 = GUICtrlCreateInput("Input1", 776, 483, 201, 21) GUICtrlSetOnEvent(-1, "Input25Change") $Input26 = GUICtrlCreateInput("Input1", 776, 459, 201, 21) GUICtrlSetOnEvent(-1, "Input26Change") $Input27 = GUICtrlCreateInput("Input1", 776, 434, 201, 21) GUICtrlSetOnEvent(-1, "Input27Change") $Input28 = GUICtrlCreateInput("Input1", 776, 394, 201, 21) GUICtrlSetOnEvent(-1, "Input28Change") $Input29 = GUICtrlCreateInput("Input1", 776, 370, 201, 21) GUICtrlSetOnEvent(-1, "Input29Change") $Input30 = GUICtrlCreateInput("Input1", 776, 346, 201, 21) GUICtrlSetOnEvent(-1, "Input30Change") $Input31 = GUICtrlCreateInput("Input1", 776, 322, 201, 21) GUICtrlSetOnEvent(-1, "Input31Change") $Label21 = GUICtrlCreateLabel("Total Memory:", 707, 324, 71, 17) GUICtrlSetOnEvent(-1, "Label21Click") $Label22 = GUICtrlCreateLabel("Nr of RAM slots:", 697, 348, 81, 17) GUICtrlSetOnEvent(-1, "Label22Click") $Label23 = GUICtrlCreateLabel("Chip Capacity:", 706, 372, 72, 17) GUICtrlSetOnEvent(-1, "Label23Click") $Label24 = GUICtrlCreateLabel("Chip speed:", 718, 396, 60, 17) GUICtrlSetOnEvent(-1, "Label24Click") $Label25 = GUICtrlCreateLabel("Nr of Videocards:", 692, 436, 86, 17) GUICtrlSetOnEvent(-1, "Label25Click") $Label26 = GUICtrlCreateLabel("Videocard name:", 694, 461, 84, 17) GUICtrlSetOnEvent(-1, "Label26Click") $Label27 = GUICtrlCreateLabel("Processor:", 724, 485, 54, 17) GUICtrlSetOnEvent(-1, "Label27Click") $Label28 = GUICtrlCreateLabel("Memory:", 734, 509, 44, 17) GUICtrlSetOnEvent(-1, "Label28Click") $Label29 = GUICtrlCreateLabel("Mode:", 744, 533, 34, 17) GUICtrlSetOnEvent(-1, "Label29Click") $Label30 = GUICtrlCreateLabel("Refresh rate:", 713, 560, 65, 17) GUICtrlSetOnEvent(-1, "Label30Click") $Hardware = GUICtrlCreateGroup("Hardware", 392, 304, 593, 281) GUICtrlCreateGroup("", -99, -99, 1, 1) $ButtonUpdate = GUICtrlCreateButton("Update", 416, 72, 89, 25, $WS_GROUP) GUICtrlSetOnEvent(-1, "ButtonUpdateClick") $TaBControls = GUICtrlCreateTabItem("Controls") $TabFileExplorer = GUICtrlCreateTabItem("File Explorer") $TabPlugin = GUICtrlCreateTabItem("Plugins") $TabProcess = GUICtrlCreateTabItem("Processes") $TabService = GUICtrlCreateTabItem("Services") $TabLocalUser = GUICtrlCreateTabItem("Local Users") $TabEvents = GUICtrlCreateTabItem("Event Viewer") $TabSoftware = GUICtrlCreateTabItem("Software") $TabTasks = GUICtrlCreateTabItem("Schedule tasks") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### GUICtrlSetTip(-1, "Coming soon...") GUICtrlCreateTabItem("") GUICtrlSetTip(-1, "Coming soon...") GUICtrlSetOnEvent(-1, "TabMainChange") GUICtrlSetOnEvent(-1, "MenuItemArkivClick") GUICtrlSetOnEvent(-1, "MenuItemExitClick") GUICtrlSetOnEvent(-1, "MenuItemUpdateClick") GUICtrlSetOnEvent(-1, "MenuHelpClick") GUICtrlSetOnEvent(-1, "MenuItemAboutClick") While 1 Sleep(10) WEnd Func ButtonSendCommandClick() EndFunc ;==>ButtonSendCommandClick Func ButtonUpdateClick() EndFunc ;==>ButtonUpdateClick Func ComputerNameClick() EndFunc ;==>ComputerNameClick Func Input10Change() EndFunc ;==>Input10Change Func Input11Change() EndFunc ;==>Input11Change Func Input12Change() EndFunc ;==>Input12Change Func Input13Change() EndFunc ;==>Input13Change Func Input14Change() EndFunc ;==>Input14Change Func Input15Change() EndFunc ;==>Input15Change Func Input16Change() EndFunc ;==>Input16Change Func Input17Change() EndFunc ;==>Input17Change Func Input18Change() EndFunc ;==>Input18Change Func Input19Change() EndFunc ;==>Input19Change Func Input1Change() EndFunc ;==>Input1Change Func Input20Change() EndFunc ;==>Input20Change Func Input21Change() EndFunc ;==>Input21Change Func Input22Change() EndFunc ;==>Input22Change Func Input23Change() EndFunc ;==>Input23Change Func Input24Change() EndFunc ;==>Input24Change Func Input25Change() EndFunc ;==>Input25Change Func Input26Change() EndFunc ;==>Input26Change Func Input27Change() EndFunc ;==>Input27Change Func Input28Change() EndFunc ;==>Input28Change Func Input29Change() EndFunc ;==>Input29Change Func Input2Change() EndFunc ;==>Input2Change Func Input30Change() EndFunc ;==>Input30Change Func Input31Change() EndFunc ;==>Input31Change Func Input3Change() EndFunc ;==>Input3Change Func Input4Change() EndFunc ;==>Input4Change Func Input5Change() EndFunc ;==>Input5Change Func Input6Change() EndFunc ;==>Input6Change Func Input7Change() EndFunc ;==>Input7Change Func Input8Change() EndFunc ;==>Input8Change Func Input9Change() EndFunc ;==>Input9Change Func InputSendCommandChange() EndFunc ;==>InputSendCommandChange Func Label10Click() EndFunc ;==>Label10Click Func Label11Click() EndFunc ;==>Label11Click Func Label12Click() EndFunc ;==>Label12Click Func Label13Click() EndFunc ;==>Label13Click Func Label14Click() EndFunc ;==>Label14Click Func Label15Click() EndFunc ;==>Label15Click Func Label16Click() EndFunc ;==>Label16Click Func Label17Click() EndFunc ;==>Label17Click Func Label18Click() EndFunc ;==>Label18Click Func Label19Click() EndFunc ;==>Label19Click Func Label1Click() EndFunc ;==>Label1Click Func Label20Click() EndFunc ;==>Label20Click Func Label21Click() EndFunc ;==>Label21Click Func Label22Click() EndFunc ;==>Label22Click Func Label23Click() EndFunc ;==>Label23Click Func Label24Click() EndFunc ;==>Label24Click Func Label25Click() EndFunc ;==>Label25Click Func Label26Click() EndFunc ;==>Label26Click Func Label27Click() EndFunc ;==>Label27Click Func Label28Click() EndFunc ;==>Label28Click Func Label29Click() EndFunc ;==>Label29Click Func Label2Click() EndFunc ;==>Label2Click Func Label30Click() EndFunc ;==>Label30Click Func Label3Click() EndFunc ;==>Label3Click Func Label4Click() EndFunc ;==>Label4Click Func Label5Click() EndFunc ;==>Label5Click Func Label6Click() EndFunc ;==>Label6Click Func Label7Click() EndFunc ;==>Label7Click Func Label8Click() EndFunc ;==>Label8Click Func Label9Click() EndFunc ;==>Label9Click Func ListViewClientsClick() EndFunc ;==>ListViewClientsClick Func ListViewConsoleClick() EndFunc ;==>ListViewConsoleClick Func MainFormClose() Exit EndFunc ;==>MainFormClose Func MainFormMaximize() EndFunc ;==>MainFormMaximize Func MainFormMinimize() EndFunc ;==>MainFormMinimize Func MainFormRestore() EndFunc ;==>MainFormRestore Func MenuHelpClick() EndFunc ;==>MenuHelpClick Func MenuItemAboutClick() EndFunc ;==>MenuItemAboutClick Func MenuItemArkivClick() EndFunc ;==>MenuItemArkivClick Func MenuItemUpdateClick() _GUICtrlListView_DeleteItemsSelected($ListViewClients) EndFunc ;==>MenuItemExitClick Func MenuItemExitClick() Exit EndFunc ;==>MenuItemExitClick Func TabMainChange() EndFunc ;==>TabMainChange Func Received($hSocket, $sReceived, $iError); And we also registered this! Our homemade do-it-yourself function gets called when something is received. ToolTip("SERVER: We received this: " & $sReceived, 50, 150); (and we'll display it) Local $Packet_Buffer, $Packet_Buffer1, $new_client, $iI Global $ListViewClients If $iError Then ; $hClient = _TCP_Client_Create(@IPAddress1, 1451) ConsoleWrite('@@(266) :(' & @MIN & ':' & @SEC & ')' & "Error" & $iError & @CR) ;### Function Trace Exit EndIf $Packet_Buffer &= $sReceived $Packet_Buffer1 &= $sReceived If StringInStr($Packet_Buffer1, '~~') Then $new_client = StringSplit($Packet_Buffer1, "~~", 1) For $packetcount1 In $new_client If StringInStr($packetcount1, 'username', 1, 1) Then $packetcount1 = StringTrimLeft($packetcount1, 9) ; ConsoleWrite('@@(266) :(' & @MIN & ':' & @SEC & ')' & "-0: " & $packetcount1 & @CR) ;### Function Trace Dim $packetcount2[1][2] ; $packetcount2[0][0]=$hSocket $packetcount2 = StringSplit($packetcount1, "|", 2) ToolTip("SERVER: We received this: " & $packetcount2[0], 50, 150); (and we'll display it) ConsoleWrite('@@(266) :(' & @MIN & ':' & @SEC & ')' & "1: " & $packetcount2[0] & $packetcount2[1] & $packetcount2[2] & @CR) ;### Function Trace ;$ListViewClients = GUICtrlCreateListView("UserName|ComputerName|IpAddress|Ping|PCID|Socket", 0, 40, 377, 561) ; _($hClient, "~~username|" & $aList[0] &"|"& @ComputerName & '|' & $kund & "|" & $IP_Address & "|" & $PC_ID & "-" & @ComputerName) Dim $aItems[1][6] For $iI = 0 To UBound($aItems) - 1 $aItems[$iI][0] = $packetcount2[0] $aItems[$iI][1] = $packetcount2[1] $aItems[$iI][2] = $packetcount2[2] $aItems[$iI][3] = "test" $aItems[$iI][4] = $packetcount2[3] $aItems[$iI][5] = $hSocket Next Local $allclients = _TCP_Server_ClientList() ConsoleWrite('@@(266) :(' & @MIN & ':' & @SEC & ')' & $allclients[1] & "3: " & $aItems[0][0] & @CR) ;### Function Trace _GUICtrlListView_AddArray($ListViewClients, $aItems) $packetcount2 = "" $aItems = "" EndIf Next EndIf If StringRight($Packet_Buffer, 1) = Chr(127) Then $Buffer_Split = StringSplit($Packet_Buffer, Chr(127)) ConsoleWrite('@@(266) :(' & @MIN & ':' & @SEC & ')' & "Working: $sReceived," & $Packet_Buffer & " " & $Buffer_Split & @CR) ;### Function Trace For $packetcount = 1 To $Buffer_Split[0] Step 1 If $Buffer_Split[$packetcount] <> "" Then If $Buffer_Split <> "" Then ; ConsoleWrite('@@(266) :(' & @MIN & ':' & @SEC & ')' & $Buffer_Split[$packetcount] & @CR) ;### Function Trace EndIf ; ConsoleWrite('@@(266) :(' & @MIN & ':' & @SEC & ')' & "Working: " & $Buffer_Split[$packetcount] & @CR) ;### Function Trace $Current_Packet = StringSplit($Buffer_Split[$packetcount], "|") For $iI = 1 To $Current_Packet[0] Step 1 $Current_Packet[$iI] = StringReplace($Current_Packet[$iI], "@PIPE", "|") Next Switch $Current_Packet[1] Case "~~Ping" SendPacketToServer($hSocket, "~~Ping") Case "~~Exit_Client" ; Disconnected($hServer, "") Case "~~Disconnect_Client" ; Disconnected($hServer, "") $again = "1" Case "~~UserName" Case Else ; SendPacketToServer("~~ConsoleMessage|Error Unknown Command: " & $Current_Packet[1]) EndSwitch EndIf Next SetClientStatus(0, "Idle") $Packet_Buffer = "" EndIf SetClientStatus(0, "Idle") $recived = "" $Packet_Buffer = "" $Buffer_Split = "" EndFunc ;==>Received Func NewClient($hSocket, $iError); Yo, check this out! It's a $iError parameter! (In case you didn't noticed: It's in every function) ToolTip("SERVER: New client connected." & @CRLF & "Sending this:~~username", 10, 30) _TCP_Send($hSocket, "~~username" & Chr(127)) ; _TCP_Send($hSocket, "Bleh!"); Sending: "Bleh!" to the new client. (Yes, that's right: $hSocket is the socket of the new client.) EndFunc ;==>NewClient Func _getSocket($hSocket) EndFunc ;==>_getSocket Func Disconnect($hSocket, $iError); Damn, we lost a client. Time of death: @Hour & @Min & @Sec Local $shit = "" ; Items created using built-in function, pass the control ID Local $iIndex = _GUICtrlListView_FindInText($ListViewClients, $hSocket) $shit &= "Index = " & $iIndex & " - " & _GUICtrlListView_DeleteItem($ListViewClients, $iIndex) ConsoleWrite('@@(266) :(' & @MIN & ':' & @SEC & ')' & "$shit " & $shit & @CR) ;### Function Trace ToolTip("SERVER: Client disconnected. $shit = " & $shit, 10, 30); Placing a tooltip right under the tooltips of the client. EndFunc ;==>Disconnect Func SetClientStatus($IsBusy = 0, $Status_Description = "Idle") Return _TCP_Send($hServer, "~~SetClientStatus|" & $IsBusy & "|" & $Status_Description & "|" & Chr(127)) EndFunc ;==>SetClientStatus Func SendPacketToServer($htempt, $Packet_Data) Return _TCP_Send($htempt, $Packet_Data & Chr(127)) EndFunc ;==>SendPacketToServer Edited September 21, 2011 by lgvlgv Link to comment Share on other sites More sharing options...
lgvlgv Posted September 21, 2011 Author Share Posted September 21, 2011 tcp.au3 ------------------#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.0.0 Author: Kip Script Function: TCP UDF v3#ce ----------------------------------------------------------------------------; Script Start - Add your code below here#csFunctions: _TCP_Server_Create($iPort, $sIP="0.0.0.0") _TCP_Server_Broadcast($sData) _TCP_Server_ClientList() _TCP_Server_ClientIP($hSocket) _TCP_Server_DisconnectClient($hSocket) _TCP_Server_Stop() _TCP_Client_Create($sIP , $iPort) _TCP_Client_Stop($hSocket) _TCP_Send($hSocket, $sText) _TCP_RegisterEvent($hSocket, $iEvent, $sFunction)Register event values: $TCP_SEND ; Function ($hSocket, $iError) $TCP_RECEIVE ; Function ($hSocket, $sReceived, $iError) $TCP_CONNECT ; Function ($hSocket, $iError) => Client only $TCP_DISCONNECT ; Function ($hSocket, $iError) $TCP_NEWCLIENT ; Function ($hSocket, $iError) => Server only#ceGlobal Const $FD_READ = 1Global Const $FD_WRITE = 2Global Const $FD_OOB = 4Global Const $FD_ACCEPT = 8Global Const $FD_CONNECT = 16Global Const $FD_CLOSE = 32Global $hWs2_32 = -1Global Const $TCP_SEND = 1Global Const $TCP_RECEIVE = 2Global Const $TCP_CONNECT = 4Global Const $TCP_DISCONNECT = 8Global Const $TCP_NEWCLIENT = 16TCPStartup()Global Const $__TCP_WINDOW = GUICreate("Async Sockets UDF")Global $__TCP_SOCKETS[1][7] ; #FUNCTION# ;===============================================================================;; Name...........: _TCP_Server_Create; Description ...: Initializes the server.; Syntax.........: _TCP_Server_Create($iPort, $sIP="0.0.0.0"); Parameters ....: $iPort - The port number the server should listen to.; $sIP - IP address. (Default = "0.0.0.0"); Return values .: The socket handle.; Author ........: Kip; Modified.......:; Remarks .......: Only 1 server can be created per script.; Related .......:; Link ..........;; Example .......;;; ;==========================================================================================Func _TCP_Server_Create($iPort, $sIP="0.0.0.0") Local $hListenSocket = ___ASocket() ___ASockSelect( $hListenSocket, $__TCP_WINDOW, 0x0400, $FD_ACCEPT) GUIRegisterMsg( 0x0400, "___TCP_OnAccept" ) ___ASockListen( $hListenSocket, $sIP, $iPort ) $__TCP_SOCKETS[0][0] = $hListenSocket $__TCP_SOCKETS[0][1] = 0x0400 Return $hListenSocketEndFunc Func ___TCP_OnAccept($hWnd, $iMsgID, $WParam, $LParam) Local $hSocket = $WParam Local $iError = ___HiWord( $LParam ) Local $iEvent = ___LoWord( $LParam ) Local $hClient, $uBound Abs($hWnd) ; Stupid AU3Check... If $iMsgID = $__TCP_SOCKETS[0][1] Then If $iEvent = $FD_ACCEPT Then If Not $iError Then ReDim $__TCP_SOCKETS[uBound($__TCP_SOCKETS)+1][7] $uBound = UBound($__TCP_SOCKETS) $hClient = TCPAccept($hSocket) ___ASockSelect($hClient, $__TCP_WINDOW, 0x0400 + $uBound - 1, BitOR($FD_READ, $FD_WRITE, $FD_CLOSE)) GUIRegisterMsg(0x0400 + $uBound - 1, "___TCP_Server_OnSocketEvent" ) $__TCP_SOCKETS[uBound($__TCP_SOCKETS)-1][0] = $hClient $__TCP_SOCKETS[uBound($__TCP_SOCKETS)-1][1] = 0x0400 + $uBound - 1 Call($__TCP_SOCKETS[0][6], $hClient, $iError) Else Call($__TCP_SOCKETS[0][6], 0, $iError) EndIf ElseIf $iEvent = $FD_CONNECT Then Call($__TCP_SOCKETS[0][4], $hSocket, $iError) EndIf EndIfEndFunc ; #FUNCTION# ;===============================================================================;; Name...........: _TCP_Client_Stop; Description ...: Stops the client.; Syntax.........: _TCP_Client_Stop($hSocket); Parameters ....: $hSocket - Client socket.; Return values .: Success - True; Failure - False; Author ........: Kip; Modified.......:; Remarks .......: The client socket is the return value of _TCP_Client_Create().; Related .......:; Link ..........;; Example .......;;; ;==========================================================================================Func _TCP_Client_Stop($hSocket) Local $iElement, $i $iElement = 0 For $i = 1 to UBound($__TCP_SOCKETS)-1 If $__TCP_SOCKETS[$i][0] = $hSocket Then $iElement = $i ExitLoop EndIf Next If $iElement Then ___ASockShutdown($__TCP_SOCKETS[$iElement][0]) TCPCloseSocket($__TCP_SOCKETS[$iElement][0]) ___ArrayDelete($__TCP_SOCKETS, $iElement) Return True EndIf Return FalseEndFunc ; #FUNCTION# ;===============================================================================;; Name...........: _TCP_Server_Stop; Description ...: Stops the server, and closes all client connections.; Syntax.........: _TCP_Server_Stop(); Parameters ....:; Return values .: True; Author ........: Kip; Modified.......:; Remarks .......:; Related .......:; Link ..........;; Example .......;;; ;==========================================================================================Func _TCP_Server_Stop() Local $i ___ASockShutdown($__TCP_SOCKETS[0][0]) TCPCloseSocket($__TCP_SOCKETS[0][0]) $__TCP_SOCKETS[0][0] = "" $__TCP_SOCKETS[0][1] = "" $__TCP_SOCKETS[0][2] = "" $__TCP_SOCKETS[0][3] = "" $__TCP_SOCKETS[0][4] = "" $__TCP_SOCKETS[0][5] = "" $__TCP_SOCKETS[0][6] = "" For $i = UBound($__TCP_SOCKETS)-1 to 1 Step -1 ___ArrayDelete($__TCP_SOCKETS, $i) Next Return TrueEndFunc Func ___TCP_Server_OnSocketEvent( $hWnd, $iMsgID, $WParam, $LParam ) Local $hSocket = $WParam Local $iError = ___HiWord( $LParam ) Local $iEvent = ___LoWord( $LParam ) Local $sDataBuff, $iElement, $i Abs($hWnd) $hSocket = 0 $iElement = 0 For $i = 1 to UBound($__TCP_SOCKETS)-1 If $__TCP_SOCKETS[$i][1] = $iMsgID Then $hSocket = $__TCP_SOCKETS[$i][0] $iElement = $i ExitLoop EndIf Next If $hSocket Then Switch $iEvent Case $FD_READ $sDataBuff = TCPRecv($hSocket, 1024) Call($__TCP_SOCKETS[0][2], $hSocket, $sDataBuff, $iError) Case $FD_WRITE Call($__TCP_SOCKETS[0][3], $hSocket, $iError) Case $FD_CLOSE ___ASockShutdown($hSocket) TCPCloseSocket($hSocket) Call($__TCP_SOCKETS[0][5], $hSocket, $iError) ___ArrayDelete($__TCP_SOCKETS, $iElement) EndSwitch EndIfEndFunc ; #FUNCTION# ;===============================================================================;; Name...........: _TCP_Server_DisconnectClient; Description ...: Disconnects a client of the server.; Syntax.........: _TCP_Server_DisconnectClient($hSocket); Parameters ....: $hSocket - Client socket.; Return values .: Success - True; Failure - False; Author ........: Kip; Modified.......:; Remarks .......: The client socket is the $hSocket parameter of a _TCP_RegisterEvent callback function.; Related .......:; Link ..........;; Example .......;;; ;==========================================================================================Func _TCP_Server_DisconnectClient($hSocket) Local $iElement, $i $iElement = 0 For $i = 1 to UBound($__TCP_SOCKETS)-1 If $__TCP_SOCKETS[$i][0] = $hSocket Then $iElement = $i ExitLoop EndIf Next If $iElement Then ___ASockShutdown($hSocket) TCPCloseSocket($hSocket) ___ArrayDelete($__TCP_SOCKETS, $iElement) Return True EndIf Return FalseEndFunc ; #FUNCTION# ;===============================================================================;; Name...........: _TCP_Server_ClientList; Description ...: Returns the sockets of all connected clients.; Syntax.........: _TCP_Server_ClientList(); Parameters ....:; Return values .: An 1 dimensional array of all connected clients.; Author ........: Kip; Modified.......:; Remarks .......:; Related .......:; Link ..........;; Example .......;;; ;==========================================================================================Func _TCP_Server_ClientList() Local $aReturn[1], $i For $i = 1 to UBound($__TCP_SOCKETS)-1 If $__TCP_SOCKETS[$i][0] Then ReDim $aReturn[uBound($aReturn)+1] $aReturn[uBound($aReturn)-1] = $__TCP_SOCKETS[$i][0] EndIf Next $aReturn[0] = UBound($aReturn)-1 Return $aReturnEndFunc ; #FUNCTION# ;===============================================================================;; Name...........: _TCP_Server_Broadcast; Description ...: Sends data to all connected clients.; Syntax.........: _TCP_Server_Broadcast($sData); Parameters ....: $sData - The data to send.; Return values .: True; Author ........: Kip; Modified.......:; Remarks .......:; Related .......:; Link ..........;; Example .......;;; ;==========================================================================================Func _TCP_Server_Broadcast($sData) Local $i For $i = 1 to UBound($__TCP_SOCKETS)-1 If $__TCP_SOCKETS[$i][0] Then TCPSend($__TCP_SOCKETS[$i][0], $sData) Next Return TrueEndFunc ; #FUNCTION# ;===============================================================================;; Name...........: _TCP_Client_Create; Description ...: Creates a new client.; Syntax.........: _TCP_Client_Create($sIP , $iPort); Parameters ....: $sIP - The IP address to connect to.; $iPort - Port on which the created socket will be connected.; Return values .: Client socket handle.; Author ........: Kip; Modified.......:; Remarks .......:; Related .......:; Link ..........;; Example .......;;; ;==========================================================================================Func _TCP_Client_Create($sIP , $iPort) ReDim $__TCP_SOCKETS[uBound($__TCP_SOCKETS)+1][7] local $hSocket = ___ASocket() $__TCP_SOCKETS[uBound($__TCP_SOCKETS)-1][0] = $hSocket $__TCP_SOCKETS[uBound($__TCP_SOCKETS)-1][1] = 0x0400 + (UBound($__TCP_SOCKETS)-1) ___ASockSelect( $hSocket, $__TCP_WINDOW, 0x0400 + (UBound($__TCP_SOCKETS)-1), BitOR( $FD_READ, $FD_WRITE, $FD_CONNECT, $FD_CLOSE ) ) GUIRegisterMsg( 0x0400 + (UBound($__TCP_SOCKETS)-1), "___TCP_Client_OnSocketEvent" ) ___ASockConnect( $hSocket, $sIP, $iPort ) Return $hSocketEndFunc ; #FUNCTION# ;===============================================================================;; Name...........: _TCP_RegisterEvent; Description ...: Registers an event.; Syntax.........: _TCP_RegisterEvent($hSocket, $iEvent, $sFunction); Parameters ....: $hSocket - Socket of the server or a client.; $iEvent - Event number. It can be any these values:; * $TCP_SEND; * $TCP_RECEIVE; * $TCP_CONNECT => Client only; * $TCP_DISCONNECT; * $TCP_NEWCLIENT => Server only; Return values .: Success - True; Failure - False; Author ........: Kip; Modified.......:; Remarks .......:; Related .......:; Link ..........;; Example .......;;; ;==========================================================================================Func _TCP_RegisterEvent($hSocket, $iEvent, $sFunction) Local $iSelected = 0 Local $i If $__TCP_SOCKETS[0][0] Then $iSelected = 0 Else For $i = 0 to UBound($__TCP_SOCKETS)-1 If $__TCP_SOCKETS[$i][0] = $hSocket Then $iSelected = $i ExitLoop EndIf Next If Not $iSelected Then Return 0 EndIf Switch $iEvent Case $TCP_SEND $__TCP_SOCKETS[$iSelected][3] = $sFunction Case $TCP_RECEIVE $__TCP_SOCKETS[$iSelected][2] = $sFunction Case $TCP_CONNECT $__TCP_SOCKETS[$iSelected][4] = $sFunction Case $TCP_DISCONNECT $__TCP_SOCKETS[$iSelected][5] = $sFunction Case $TCP_NEWCLIENT $__TCP_SOCKETS[$iSelected][6] = $sFunction Case Else Return False EndSwitch Return TrueEndFunc ; #FUNCTION# ;===============================================================================;; Name...........: _TCP_Server_ClientIP; Description ...: Converts a client socket handle to IP address.; Syntax.........: _TCP_Server_ClientIP($hSocket); Parameters ....: $hSocket - Client socket handle.; Return values .: A string with the IP address.; Author ........: Unknown; Modified.......: Kip; Remarks .......:; Related .......:; Link ..........;; Example .......;;; ;==========================================================================================Func _TCP_Server_ClientIP($hSocket) Local $pSocketAddress, $aReturn $pSocketAddress = DllStructCreate("short;ushort;uint;char[8]") $aReturn = DllCall("Ws2_32.dll", "int", "getpeername", "int", $hSocket, "ptr", DllStructGetPtr($pSocketAddress), "int*", DllStructGetSize($pSocketAddress)) If @error Or $aReturn[0] <> 0 Then Return 0 $aReturn = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($pSocketAddress, 3)) If @error Then Return 0 $pSocketAddress = 0 Return $aReturn[0]EndFunc ; #FUNCTION# ;===============================================================================;; Name...........: _TCP_Send; Description ...: Sends data to a server or client.; Syntax.........: _TCP_Send($hSocket, $sText); Parameters ....: $hSocket - Client or server socket handle.; $sText - Data to send.; Return values .:; Author ........: Kip; Modified.......:; Remarks .......:; Related .......:; Link ..........;; Example .......;;; ;==========================================================================================Func _TCP_Send($hSocket, $sText) Return TCPSend($hSocket, $sText)EndFunc Func ___TCP_Client_OnSocketEvent( $hWnd, $iMsgID, $WParam, $LParam ) Local $iError = ___HiWord( $LParam ) Local $iEvent = ___LoWord( $LParam ) Local $hSocket, $iElement, $i, $sDataBuff Abs($hWnd) Abs($WParam) $hSocket = 0 $iElement = 0 For $i = 1 to UBound($__TCP_SOCKETS)-1 If $__TCP_SOCKETS[$i][1] = $iMsgID Then $hSocket = $__TCP_SOCKETS[$i][0] $iElement = $i ExitLoop EndIf Next If $hSocket Then Switch $iEvent Case $FD_READ; Data has arrived! $sDataBuff = TCPRecv( $hSocket, 1024) Call($__TCP_SOCKETS[$i][2], $hSocket, $sDataBuff, $iError) $sDataBuff = "" Case $FD_WRITE Call($__TCP_SOCKETS[$i][3], $hSocket, $iError) Case $FD_CONNECT Call($__TCP_SOCKETS[$i][4], $hSocket, $iError) Case $FD_CLOSE ___ASockShutdown( $hSocket ) TCPCloseSocket( $hSocket ) Call($__TCP_SOCKETS[$i][5], $hSocket, $iError) ___ArrayDelete($__TCP_SOCKETS, $iElement) EndSwitch EndIfEndFunc ;==================================================================================================================;; Zatorg's Asynchronous Sockets UDF Starts from here.;;==================================================================================================================Func ___ASocket($iAddressFamily = 2, $iType = 1, $iProtocol = 6) If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" ) Local $hSocket = DllCall($hWs2_32, "uint", "socket", "int", $iAddressFamily, "int", $iType, "int", $iProtocol) If @error Then SetError(1, @error) Return -1 EndIf If $hSocket[ 0 ] = -1 Then SetError(2, ___WSAGetLastError()) Return -1 EndIf Return $hSocket[ 0 ]EndFunc ;==>_ASocketFunc ___ASockShutdown($hSocket) If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" ) Local $iRet = DllCall($hWs2_32, "int", "shutdown", "uint", $hSocket, "int", 2) If @error Then SetError(1, @error) Return False EndIf If $iRet[ 0 ] <> 0 Then SetError(2, ___WSAGetLastError()) Return False EndIf Return TrueEndFunc ;==>_ASockShutdownFunc ___ASockClose($hSocket) If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" ) Local $iRet = DllCall($hWs2_32, "int", "closesocket", "uint", $hSocket) If @error Then SetError(1, @error) Return False EndIf If $iRet[ 0 ] <> 0 Then SetError(2, ___WSAGetLastError()) Return False EndIf Return TrueEndFunc ;==>_ASockCloseFunc ___ASockSelect($hSocket, $hWnd, $uiMsg, $iEvent) If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" ) Local $iRet = DllCall( _ $hWs2_32, _ "int", "WSAAsyncSelect", _ "uint", $hSocket, _ "hwnd", $hWnd, _ "uint", $uiMsg, _ "int", $iEvent _ ) If @error Then SetError(1, @error) Return False EndIf If $iRet[ 0 ] <> 0 Then SetError(2, ___WSAGetLastError()) Return False EndIf Return TrueEndFunc ;==>_ASockSelect; Note: you can see that $iMaxPending is set to 5 by default.; IT DOES NOT MEAN THAT DEFAULT = 5 PENDING CONNECTIONS; 5 == SOMAXCONN, so don't worry be happyFunc ___ASockListen($hSocket, $sIP, $uiPort, $iMaxPending = 5); 5 == SOMAXCONN => No need to change it. Local $iRet Local $stAddress If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" ) $stAddress = ___SockAddr($sIP, $uiPort) If @error Then SetError(@error, @extended) Return False EndIf $iRet = DllCall($hWs2_32, "int", "bind", "uint", $hSocket, "ptr", DllStructGetPtr($stAddress), "int", DllStructGetSize($stAddress)) If @error Then SetError(3, @error) Return False EndIf If $iRet[ 0 ] <> 0 Then $stAddress = 0; Deallocate SetError(4, ___WSAGetLastError()) Return False EndIf $iRet = DllCall($hWs2_32, "int", "listen", "uint", $hSocket, "int", $iMaxPending) If @error Then SetError(5, @error) Return False EndIf If $iRet[ 0 ] <> 0 Then $stAddress = 0; Deallocate SetError(6, ___WSAGetLastError()) Return False EndIf Return TrueEndFunc ;==>_ASockListenFunc ___ASockConnect($hSocket, $sIP, $uiPort) Local $iRet Local $stAddress If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" ) $stAddress = ___SockAddr($sIP, $uiPort) If @error Then SetError(@error, @extended) Return False EndIf $iRet = DllCall($hWs2_32, "int", "connect", "uint", $hSocket, "ptr", DllStructGetPtr($stAddress), "int", DllStructGetSize($stAddress)) If @error Then SetError(3, @error) Return False EndIf $iRet = ___WSAGetLastError() If $iRet = 10035 Then; WSAEWOULDBLOCK Return True; Asynchronous connect attempt has been started. EndIf SetExtended(1); Connected immediately Return TrueEndFunc ;==>_ASockConnect; A wrapper function to ease all the pain in creating and filling the sockaddr structFunc ___SockAddr($sIP, $iPort, $iAddressFamily = 2) Local $iRet Local $stAddress If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" ) $stAddress = DllStructCreate("short; ushort; uint; char[8]") If @error Then SetError(1, @error) Return False EndIf DllStructSetData($stAddress, 1, $iAddressFamily) $iRet = DllCall($hWs2_32, "ushort", "htons", "ushort", $iPort) DllStructSetData($stAddress, 2, $iRet[ 0 ]) $iRet = DllCall($hWs2_32, "uint", "inet_addr", "str", $sIP) If $iRet[ 0 ] = 0xffffffff Then; INADDR_NONE $stAddress = 0; Deallocate SetError(2, ___WSAGetLastError()) Return False EndIf DllStructSetData($stAddress, 3, $iRet[ 0 ]) Return $stAddressEndFunc ;==>__SockAddrFunc ___WSAGetLastError() If $hWs2_32 = -1 Then $hWs2_32 = DllOpen( "Ws2_32.dll" ) Local $iRet = DllCall($hWs2_32, "int", "WSAGetLastError") If @error Then ;ConsoleWrite("+> _WSAGetLastError(): WSAGetLastError() failed. Script line number: " & @ScriptLineNumber & @CRLF) SetExtended(1) Return 0 EndIf Return $iRet[ 0 ]EndFunc ;==>_WSAGetLastError; Got these here:; http://www.autoitscript.com/forum/index.php?showtopic=5620&hl=MAKELONGFunc ___MakeLong($LoWord, $HiWord) Return BitOR($HiWord * 0x10000, BitAND($LoWord, 0xFFFF)); Thanks LarryEndFunc ;==>_MakeLongFunc ___HiWord($Long) Return BitShift($Long, 16); Thanks ValikEndFunc ;==>_HiWordFunc ___LoWord($Long) Return BitAND($Long, 0xFFFF); Thanks ValikEndFunc ;==>_LoWord; ========================================= Array functions; #FUNCTION# ====================================================================================================================; Name...........: _ArrayDelete; Description ...: Deletes the specified element from the given array.; Syntax.........: _ArrayDelete(ByRef $avArray, $iElement); Parameters ....: $avArray - Array to modify; $iElement - Element to delete; Return values .: Success - New size of the array; Failure - 0, sets @error to:; |1 - $avArray is not an array; |3 - $avArray has too many dimensions (only up to 2D supported); |(2 - Deprecated error code); Author ........: Cephas <cephas at clergy dot net>; Modified.......: Jos van der Zande <jdeb at autoitscript dot com> - array passed ByRef, Ultima - 2D arrays supported, reworked function (no longer needs temporary array; faster when deleting from end); Remarks .......: If the array has one element left (or one row for 2D arrays), it will be set to "" after _ArrayDelete() is used on it.; Related .......: _ArrayAdd, _ArrayInsert, _ArrayPop, _ArrayPush; Link ..........;; Example .......; Yes; ===============================================================================================================================Func ___ArrayDelete(ByRef $avArray, $iElement) If Not IsArray($avArray) Then Return SetError(1, 0, 0) Local $iUBound = UBound($avArray, 1) - 1 If Not $iUBound Then ; $avArray = "" Return 0 EndIf ; Bounds checking If $iElement < 0 Then $iElement = 0 If $iElement > $iUBound Then $iElement = $iUBound ; Move items after $iElement up by 1 Switch UBound($avArray, 0) Case 1 For $i = $iElement To $iUBound - 1 $avArray[$i] = $avArray[$i + 1] Next ReDim $avArray[$iUBound] Case 2 Local $iSubMax = UBound($avArray, 2) - 1 For $i = $iElement To $iUBound - 1 For $j = 0 To $iSubMax $avArray[$i][$j] = $avArray[$i + 1][$j] Next Next ReDim $avArray[$iUBound][$iSubMax + 1] Case Else Return SetError(3, 0, 0) EndSwitch Return $iUBoundEndFunc ;==>_ArrayDelete Link to comment Share on other sites More sharing options...
lgvlgv Posted September 21, 2011 Author Share Posted September 21, 2011 (edited) client.au3 #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_outfile=client.exe #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include "TCP.au3" if @OSArch = "X64" Then Global $key7 = "HKLM64" Else Global $key7 = "HKLM" EndIf Global $RemoteServer = RegRead($key7 & "\SOFTWARE\Charge\ChargeDeamon", "RemoteServer") Global $RemoteServerPort = RegRead($key7 & "\SOFTWARE\Charge\ChargeDeamon", "RemoteServerPort") Global $MySQLServer = RegRead($key7 & "\SOFTWARE\Charge\ChargeDeamon", "MySQLServer") Global $MySQLServerPort = RegRead($key7 & "\SOFTWARE\Charge\ChargeDeamon", "MySQLServerPort") Global $s_MailTo = RegRead($key7 & "\SOFTWARE\Charge\ChargeDeamon", "MailTo") Global $kund = RegRead($key7 & "\SOFTWARE\Charge\ChargeDeamon", "Kund") ToolTip("CLIENT: Connecting...", 10, 10) $hClient = _TCP_Client_Create($RemoteServer, $RemoteServerPort); Create the client. Which will connect to the local ip address on port 88 _TCP_RegisterEvent($hClient, $TCP_RECEIVE, "Received"); Function "Received" will get called when something is received _TCP_RegisterEvent($hClient, $TCP_CONNECT, "Connected"); And func "Connected" will get called when the client is connected. _TCP_RegisterEvent($hClient, $TCP_DISCONNECT, "Disconnected"); And "Disconnected" will get called when the server disconnects us, or when the connection is lost. While 1 sleep(10) ; just to keep the program running WEnd Func Connected($hSocket, $iError); We registered this (you see?), When we're connected (or not) this function will be called. If Not $iError Then; If there is no error... ToolTip("CLIENT: Connected!", 10, 10); ... we're connected. $PC_ID = RegRead($key7 & '\SOFTWARE\Microsoft\Cryptography', 'MachineGuid') Local $aList = WMI_GetLoggedInUser2() _TCP_Send($hSocket, "~~username|" & $aList[0] & '|' & @ComputerName & "|" & @IPAddress1 & "|" & $PC_ID & "->" & @ComputerName) ConsoleWrite('@@(266) :(' & @MIN & ':' & @SEC & ')' & "CLIENT: RemoteServer is responding!!-> " & @UserName & '@' & $PC_ID & @CR) ;### Function Trace Else; ,else... ToolTip("CLIENT: Could not connect. Are you sure the server is running?", 10, 10); ... we aren't. EndIf EndFunc ;==>Connected Func Received($hSocket, $sReceived, $iError); And we also registered this! Our homemade do-it-yourself function gets called when something is received. ToolTip("CLIENT: We received this: " & $sReceived, 10, 10); (and we'll display it) EndFunc ;==>Received Func Disconnected($hSocket, $iError); Our disconnect function. Notice that all functions should have an $iError parameter. ToolTip("CLIENT: Connection closed or lost.", 10, 10) _TCP_Client_Stop($hSocket) Connected($hSocket, $iError) EndFunc ;==>Disconnected Func WMI_GetLoggedInUser2() ;coded by UEZ 2011 local $i="0",$aResult[1],$test="" $strComputer = "localhost" $oService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2") If @error Then Return SetError(3, 0, 0) ;~ Local $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $host & "\root\cimv2") Local $colItems = $oService.ExecQuery("SELECT UserName FROM Win32_ComputerSystem") If IsObj($colItems) Then For $objItem In $colItems $test = StringSplit($objItem.UserName, "\") $aResult[$i] = $test[2] $i += 1 ; If $i = UBound($aResult) Then ExitLoop Next ; ReDim $aResult[$i] ; $aResult[0] = $i - 1 if $aResult[0] = "" Then $aResult[0]="System" EndIf Return $aResult EndIf Return SetError(4, 0, 0) EndFunc ;==>WMI_GetLoggedInUser2 Edited September 21, 2011 by lgvlgv Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 21, 2011 Moderators Share Posted September 21, 2011 lgvlgv,It looks as if we have run into the old problem of UDF functions needing handles rather than ControlIDs. Try using this function:Func Disconnect($hSocket, $iError); Damn, we lost a client. Time of death: @Hour & @Min & @Sec :P Local $shit = "" ; Items created using built-in function, pass the control ID Local $iIndex = _GUICtrlListView_FindInText($ListViewClients, $hSocket) $shit &= "Index = " & $iIndex & " - " & _GUICtrlListView_DeleteItem(GUICtrlGetHandle($ListViewClients), $iIndex) ConsoleWrite('@@(266) :(' & @MIN & ':' & @SEC & ')' & "$shit " & $shit & @CR) ;### Function Trace ToolTip("SERVER: Client disconnected. $shit = " & $shit, 10, 30); Placing a tooltip right under the tooltips of the client. EndFunc ;==>DisconnectThis works in your script when I test it with dummy data - see if it does for you with live data. M23  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
BrewManNH Posted September 21, 2011 Share Posted September 21, 2011 The ListView UDF seems to have a problem with deleting control added with no control ID if you send it the control ID of the ListView rather than its handle. I've run into this several times before where either GUICtrlDelete, _GUICtrlListView_GetItemEx, or _GUICtrlListView_GetItemParam doesn't play nice with the control ID of the LV rather than its handle If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator Link to comment Share on other sites More sharing options...
lgvlgv Posted September 21, 2011 Author Share Posted September 21, 2011 lgvlgv, It looks as if we have run into the old problem of UDF functions needing handles rather than ControlIDs. Try using this function: Func Disconnect($hSocket, $iError); Damn, we lost a client. Time of death: @Hour & @Min & @Sec :P Local $shit = "" ; Items created using built-in function, pass the control ID Local $iIndex = _GUICtrlListView_FindInText($ListViewClients, $hSocket) $shit &= "Index = " & $iIndex & " - " & _GUICtrlListView_DeleteItem(GUICtrlGetHandle($ListViewClients), $iIndex) ConsoleWrite('@@(266) :(' & @MIN & ':' & @SEC & ')' & "$shit " & $shit & @CR) ;### Function Trace ToolTip("SERVER: Client disconnected. $shit = " & $shit, 10, 30); Placing a tooltip right under the tooltips of the client. EndFunc ;==>Disconnect This works in your script when I test it with dummy data - see if it does for you with live data. M23 it returns true @@(266) :(58:41)$shit Index = 0 - True but in the list i still see it, do i need to update the listview some how? Link to comment Share on other sites More sharing options...
lgvlgv Posted September 21, 2011 Author Share Posted September 21, 2011 The ListView UDF seems to have a problem with deleting control added with no control ID if you send it the control ID of the ListView rather than its handle. I've run into this several times before where either GUICtrlDelete, _GUICtrlListView_GetItemEx, or _GUICtrlListView_GetItemParam doesn't play nice with the control ID of the LV rather than its handle how do i include a "control ID" the code that adds it to list looks like this.. Dim $aItems[1][6] For $iI = 0 To UBound($aItems) - 1 $aItems[$iI][0] = $packetcount2[0] $aItems[$iI][1] = $packetcount2[1] $aItems[$iI][2] = $packetcount2[2] $aItems[$iI][3] = "test" $aItems[$iI][4] = $packetcount2[3] $aItems[$iI][5] = $hSocket Next local $allclients=_TCP_Server_ClientList() ConsoleWrite('@@(266) :(' & @MIN & ':' & @SEC & ')' & $allclients[1] &"3: " & $aItems[0][0] & @CR) ;### Function Trace _GUICtrlListView_AddArray($ListViewClients, $aItems) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 21, 2011 Moderators Share Posted September 21, 2011 BrewManNH,I did ask a while back (Post #15) whether the OP was using a native or UDF created ListView and suggested the GUICtrlGetHandle solution in the former case. I think it got lost in the noise. lgvlgv,If the _GUICtrlListView_DeleteItem function returns True then the item should be deleted from the ListView - as was the case in the test scripts I posted. Are you saying that the ListView does not update? If so then I am at a complete loss. M23  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
BrewManNH Posted September 21, 2011 Share Posted September 21, 2011 how do i include a "control ID" the code that adds it to list looks like this..You don't, you send it the handle of the listview. When using _GUICtrlListView_AddArray your items are added to the listview with no native control ID, just handles, so to delete them you need to delete them using the handle of the listview and not its control ID. The Listview is created using GUICtrlCreateListview, which returns a control ID, so you HAVE to get the handle of the listview, Melba23 shows how that is done, when you try to delete an item using _GUICtrlListview_DeleteItem. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator Link to comment Share on other sites More sharing options...
lgvlgv Posted September 21, 2011 Author Share Posted September 21, 2011 You don't, you send it the handle of the listview. When using _GUICtrlListView_AddArray your items are added to the listview with no native control ID, just handles, so to delete them you need to delete them using the handle of the listview and not its control ID. The Listview is created using GUICtrlCreateListview, which returns a control ID, so you HAVE to get the handle of the listview, Melba23 shows how that is done, when you try to delete an item using _GUICtrlListview_DeleteItem.i read this 3 times now still dont get it:)how to i get the handle and how do i delete the handle? Or is its easyer to add the item to the list in another way, so i get a CONTROL ID??? God i need a drink now... Link to comment Share on other sites More sharing options...
BrewManNH Posted September 21, 2011 Share Posted September 21, 2011 You need to read what Melba23 wrote about how to get the handle of the LISTVIEW, NOT the handle of the ITEMS in the listview. You have the handle of the items, you don't have the handle of the listview. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator Link to comment Share on other sites More sharing options...
lgvlgv Posted September 21, 2011 Author Share Posted September 21, 2011 LOL, i cant handle this anymore i will go back and see what Melba wrote Thx for trying to teach me, u guys show great patience. Link to comment Share on other sites More sharing options...
lgvlgv Posted September 21, 2011 Author Share Posted September 21, 2011 BrewManNH, I did ask a while back (Post #15) whether the OP was using a native or UDF created ListView and suggested the GUICtrlGetHandle solution in the former case. I think it got lost in the noise. lgvlgv, If the _GUICtrlListView_DeleteItem function returns True then the item should be deleted from the ListView - as was the case in the test scripts I posted. Are you saying that the ListView does not update? If so then I am at a complete loss. M23 yeah, i cut and pasted ur script it returns true but dosent "update".. what is the best way do insert the Item with a control ID? i just used arrayadd becose it was the first i tryed and it worked, but dont need to do it that way .. Link to comment Share on other sites More sharing options...
lgvlgv Posted September 21, 2011 Author Share Posted September 21, 2011 (edited) Func Disconnect($hSocket, $iError); Damn, we lost a client. Time of death: @Hour & @Min & @Sec :P Local $shit = "" ; Items created using built-in function, pass the control ID Local $iIndex = _GUICtrlListView_FindInText($ListViewClients, $hSocket) $shit &= "Index = " & $iIndex & " - " & _GUICtrlListView_DeleteItem(GUICtrlGetHandle($ListViewClients), $iIndex) ConsoleWrite('@@(266) :(' & @MIN & ':' & @SEC & ')' & "$shit " & $shit & @CR) ;### Function Trace ToolTip("SERVER: Client disconnected. $shit = " & $shit, 10, 30); Placing a tooltip right under the tooltips of the client. EndFunc ;==>Disconnect IT WORKS!!!must have done something wrong when i saved the last time!!!Thx to all of u, i love u!!!!! Edited September 21, 2011 by lgvlgv Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 21, 2011 Moderators Share Posted September 21, 2011 lgvlgv, Hooray! M23 P.S. Although more out of relief than anything else!  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  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