faustf Posted July 17, 2016 Share Posted July 17, 2016 hi guy i have create 2 script one is master-server and second is client-slave in practic i want move a mouse in master-server pc and thesame movement and click , send in client-slave ,( ofcourse if i active a client-slave prog.) but i dontknow why the client-slave tell me arrydimension exceed script 1 master-server expandcollapse popup#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.8.1 (Or greater) Author: Ken Piper Script Function: Template multi-client server base code. Use as a base for making an efficient server program. This base will just accept connections and echo back what it receives, and kill the connection if it is dead or inactive for x seconds. It will not do any other work, that must be added seperately! #ce ---------------------------------------------------------------------------- #include <Misc.au3> TCPStartup() Opt("TCPTimeout", 0) #Region ;Safe-to-edit things are below Global $BindIP = "0.0.0.0" ;Listen on all addresses Global $BindPort = 33569 ;Listen on port 8080 Global $Timeout = 31536000000 ;Max idle time is one year seconds before calling a connection "dead" Global $PacketSize = 2048 ;Max packet size per-check is 2KB Global $MaxClients = 50 ;Max simultaneous clients is 50 #EndRegion ;Safe-to-edit things are below Global $Listen Global $Clients[1][4] ;[Index][Socket, IP, Timestamp, Buffer] Global $Ws2_32 = DllOpen("Ws2_32.dll") ;Open Ws2_32.dll, it might get used a lot Global $NTDLL = DllOpen("ntdll.dll") ;Open ntdll.dll, it WILL get used a lot Global $CleanupTimer = TimerInit() ;This is used to time when things should be cleaned up Global $hDLL = DllOpen("user32.dll") Global $Mouse_button_dx, $Mouse_button_sx OnAutoItExitRegister("Close") ;Register this function to be called if the server needs to exit $Clients[0][0] = 0 $Listen = TCPListen($BindIP, $BindPort, $MaxClients) ;Start listening on the given IP/port If @error Then Exit 1 ;Exit with return code 1 if something was already bound to that IP and port While 1 USleep(5000, $NTDLL) ;This is needed because TCPTimeout is disabled. Without this it will run one core at ~100%. ;The USleep function takes MICROseconds, not milliseconds, so 1000 = 1ms delay. ;When working with this granularity, you have to take in to account the time it takes to complete USleep(). ;1000us (1ms) is about as fast as this should be set. If you need more performance, set this from 5000 to 1000, ;but doing so will make it consume a bit more CPU time to get that extra bit of performance. Check() ;Check recv buffers and do things If TimerDiff($CleanupTimer) > 1000 Then ;If it has been more than 1000ms since Cleanup() was last called, call it now $CleanupTimer = TimerInit() ;Reset $CleanupTimer, so it is ready to be called again Cleanup() ;Clean up the dead connections EndIf Local $iSock = TCPAccept($Listen) ;See if anything wants to connect If $iSock = -1 Then ContinueLoop ;If nothing wants to connect, restart at the top of the loop Local $iSize = UBound($Clients, 1) ;Something wants to connect, so get the number of people currently connected here If $iSize - 1 > $MaxClients And $MaxClients > 0 Then ;If $MaxClients is greater than 0 (meaning if there is a max connection limit) then check if that has been reached TCPCloseSocket($iSock) ;It has been reached, close the new connection and continue back at the top of the loop ContinueLoop EndIf ReDim $Clients[$iSize + 1][4] ;There is room for a new connection, allocate space for it here $Clients[0][0] = $iSize ;Update the number of connected clients $Clients[$iSize][0] = $iSock ;Set the socket ID of the connection $Clients[$iSize][1] = SocketToIP($iSock, $Ws2_32) ;Set the IP Address the connection is from $Clients[$iSize][2] = TimerInit() ;Set the timestamp for the last known activity timer $Clients[$iSize][3] = "" ;Blank the recv buffer WEnd Func Check() ;Function for processing If $Clients[0][0] < 1 Then Return ;If there are no clients connected, stop the function right now For $i = 1 To $Clients[0][0] ;Loop through all connected clients Local $pos = MouseGetPos() If _IsPressed("01", $hDLL) Then $Mouse_button_sx = 1 ElseIf _IsPressed("02", $hDLL) Then $Mouse_button_dx = 1 EndIf TCPSend($Clients[$i][0], $pos[0] & " " & $pos[1] & " " & $Mouse_button_sx & " " & $Mouse_button_dx & @CRLF) $sRecv = TCPRecv($Clients[$i][0], $PacketSize) ;Read $PacketSize bytes from the current client's buffer ;ConsoleWrite($pos[0] & " " & $pos[1] & " - " & $Mouse_button_sx & " - " & $Mouse_button_dx & @CRLF) $Mouse_button_dx = "" $Mouse_button_sx = "" #cs If $sRecv <> "" Then $Clients[$i][3] &= $sRecv ;If there was more data sent from the client, add it to the buffer If $Clients[$i][3] = "" Then ContinueLoop ;If the buffer is empty, stop right here and check more clients $Clients[$i][2] = TimerInit() ;If it got this far, there is data to be parsed, so update the activity timer #region ;Example packet processing stuff here. This is handling for a simple "echo" server with per-packet handling $sRecv = StringLeft($Clients[$i][3], StringInStr($Clients[$i][3], @CRLF, 0, -1)) ;Pull all data to the left of the last @CRLF in the buffer ;This does NOT pull the first complete packet, this pulls ALL complete packets, leaving only potentially incomplete packets in the buffer If $sRecv = "" Then ContinueLoop ;Check if there were any complete "packets" $Clients[$i][3] = StringTrimLeft($Clients[$i][3], StringLen($sRecv) + 1) ;remove what was just read from the client's buffer $sPacket = StringSplit($sRecv, @CRLF, 1) ;Split all complete packets up in to an array, so it is easy to work with them For $j = 1 To $sPacket[0] ;Loop through each complete packet; This is where any packet processing should be done TCPSend($Clients[$i][0], "Echoing line: " & $sPacket[$j] & @CRLF) ;Echo back the packet the client sent Next #endregion ;Example #ce Next EndFunc ;==>Check Func Cleanup() ;Clean up any disconnected clients to regain resources If $Clients[0][0] < 1 Then Return ;If no clients are connected then return Local $iNewSize = 0 For $i = 1 To $Clients[0][0] ;Loop through all connected clients $Clients[$i][3] &= TCPRecv($Clients[$i][0], $PacketSize) ;Dump any data not-yet-seen in to their recv buffer If @error > 0 Or TimerDiff($Clients[$i][2]) > $Timeout Then ;Check to see if the connection has been inactive for a while or if there was an error TCPCloseSocket($Clients[$i][0]) ;If yes, close the connection $Clients[$i][0] = -1 ;Set the socket ID to an invalid socket Else $iNewSize += 1 EndIf Next If $iNewSize < $Clients[0][0] Then ;If any dead connections were found, drop them from the client array and resize the array Local $iSize = UBound($Clients, 2) - 1 Local $aTemp[$iNewSize + 1][$iSize + 1] Local $iCount = 1 For $i = 1 To $Clients[0][0] If $Clients[$i][0] = -1 Then ContinueLoop For $j = 0 To $iSize $aTemp[$iCount][$j] = $Clients[$i][$j] Next $iCount += 1 Next $aTemp[0][0] = $iNewSize $Clients = $aTemp EndIf EndFunc ;==>Cleanup Func Close() DllClose($Ws2_32) ;Close the open handle to Ws2_32.dll DllClose($NTDLL) ;Close the open handle to ntdll.dll For $i = 1 To $Clients[0][0] ;Loop through the connected clients TCPCloseSocket($Clients[$i][0]) ;Force the client's connection closed Next TCPShutdown() ;Shut down networking stuff EndFunc ;==>Close Func SocketToIP($iSock, $hDLL = "Ws2_32.dll") ;A rewrite of that _SocketToIP function that has been floating around for ages Local $structName = DllStructCreate("short;ushort;uint;char[8]") Local $sRet = DllCall($hDLL, "int", "getpeername", "int", $iSock, "ptr", DllStructGetPtr($structName), "int*", DllStructGetSize($structName)) If Not @error Then $sRet = DllCall($hDLL, "str", "inet_ntoa", "int", DllStructGetData($structName, 3)) If Not @error Then Return $sRet[0] EndIf Return "0.0.0.0" ;Something went wrong, return an invalid IP EndFunc ;==>SocketToIP Func USleep($iUsec, $hDLL = "ntdll.dll") ;A rewrite of the _HighPrecisionSleep function made by monoceres (Thanks!) Local $hStruct = DllStructCreate("int64") DllStructSetData($hStruct, 1, -1 * ($iUsec * 10)) DllCall($hDLL, "dword", "ZwDelayExecution", "int", 0, "ptr", DllStructGetPtr($hStruct)) EndFunc ;==>USleep 2 script client-slave expandcollapse popup#include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <Array.au3> TCPStartup() ; Start the TCP service. ; Register OnAutoItExit to be called when the script is closed. OnAutoItExitRegister("OnAutoItExit") ; Assign Local variables the loopback IP Address and the Port. Local $sIPAddress = "192.168.1.103" ; This IP Address only works for testing on your own computer. Local $iPort = 33569 ; Port used for the connection. While 1 Local $Cordinate = MyTCP_Client($sIPAddress, $iPort) ConsoleWrite($Cordinate) ;If $Cordinate <> "" Then Local $xyPush = StringSplit($Cordinate, " ") MouseMove($xyPush[1], $xyPush[2], 3) If $xyPush[3] = 1 Then MouseClick($MOUSE_CLICK_LEFT) ElseIf $xyPush[4] = 1 Then MouseClick($MOUSE_CLICK_RIGHT) EndIf ;EndIf ;_ArrayDisplay($xyPush) Sleep(10) WEnd Func MyTCP_Client($sIPAddress, $iPort) ; Assign a Local variable the socket and connect to a listening socket with the IP Address and Port specified. Local $iSocket = TCPConnect($sIPAddress, $iPort) Local $iError = 0 ; If an error occurred display the error code and return False. If @error Then ; The server is probably offline/port is not opened on the server. $iError = @error ;MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not connect, Error code: " & $iError) ;Return False EndIf Local $sReceived = TCPRecv($iSocket, 30) Return $sReceived ;MsgBox($MB_SYSTEMMODAL, "", "Server:" & @CRLF & "Received: " & $sReceived) ; If an error occurred display the error code and return False. If @error Then $iError = @error MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not send the data, Error code: " & $iError) Return False EndIf ; Close the socket. TCPCloseSocket($iSocket) EndFunc ;==>MyTCP_Client Func OnAutoItExit() TCPShutdown() ; Close the TCP service. EndFunc ;==>OnAutoItExit the problem is in line 18 MouseMove($xyPush[1], $xyPush[2], 3) thankz for help at all Link to comment Share on other sites More sharing options...
Danyfirex Posted July 18, 2016 Share Posted July 18, 2016 Hello... Debug your code. Local $xyPush = StringSplit($Cordinate, " ") _arraydisplay($xyPush) ;here you see what is happening... MouseMove($xyPush[1], $xyPush[2], 3) Saludos Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
AutoBert Posted July 18, 2016 Share Posted July 18, 2016 The problem is $xyPush isnn't a array, so check before. What is console output from $coordinate? Link to comment Share on other sites More sharing options...
faustf Posted July 18, 2016 Author Share Posted July 18, 2016 but why is not array ??? i see with _arraydisplay in console coordinate is x-y mouse and 1-1 when is pushed button left have 1 and when i push right mouse button have 1 usally i have this 120 202 space space Link to comment Share on other sites More sharing options...
AutoBert Posted July 18, 2016 Share Posted July 18, 2016 post console output (at least the lastconsolewrite until crash) and i can tell you what's exactly wrong. Link to comment Share on other sites More sharing options...
faustf Posted July 18, 2016 Author Share Posted July 18, 2016 Thnakz so much "C:\Users\pc\Desktop\slave.au3" (19) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: MouseMove($xyPush[1], $xyPush[2], 3) MouseMove($xyPush[1], ^ ERROR >Exit code: 1 Time: 0.2829 Link to comment Share on other sites More sharing options...
AutoBert Posted July 18, 2016 Share Posted July 18, 2016 (edited) Yes it's a array, but only $xyPush[0] = 1 and not > 1 as you exspexted, so test this way: While 1 Local $Cordinate = MyTCP_Client($sIPAddress, $iPort) ConsoleWrite($Cordinate) Local $xyPush = StringSplit($Cordinate, " ") If IsArray($xyPush) Then _ArrayDisplay($xyPush) If $xyPush[0] > 1 Then MouseMove($xyPush[1], $xyPush[2], 3) If ($xyPush[0] > 2) And $xyPush[3] Then MouseClick($MOUSE_CLICK_LEFT) ElseIf ($xyPush[0] > 3) And $xyPush[4] Then MouseClick($MOUSE_CLICK_RIGHT) EndIf EndIf Sleep(10) WEnd or this way: While 1 Local $Cordinate = MyTCP_Client($sIPAddress, $iPort) ConsoleWrite($Cordinate) $Cordinate &= '0 0 0 0'; bewaring from to small array ;only affect's unnececarry mousmoves to 0,0 ;if $coordinate before not as exspected Local $xyPush = StringSplit($Cordinate, " ") ;_ArrayDisplay($xyPush) MouseMove($xyPush[1], $xyPush[2], 3) If $xyPush[3] Then MouseClick($MOUSE_CLICK_LEFT) ElseIf And $xyPush[4] Then MouseClick($MOUSE_CLICK_RIGHT) EndIf EndIf Sleep(10) WEnd Edited July 18, 2016 by AutoBert faustf 1 Link to comment Share on other sites More sharing options...
faustf Posted July 18, 2016 Author Share Posted July 18, 2016 ok work good , i used the first solution , i have questions why you use $xyPush[0] > 1 >??? of 1 and after 2 and after 3 ???? Link to comment Share on other sites More sharing options...
AutoBert Posted July 18, 2016 Share Posted July 18, 2016 $xyPush[0] holds the count of elements after the split. And yes you are right but when testing once, like this: While 1 Local $Cordinate = MyTCP_Client($sIPAddress, $iPort) ConsoleWrite($Cordinate) Local $xyPush = StringSplit($Cordinate, " ") If IsArray($xyPush) Then If ($xyPush[0] > 3) Then _ArrayDisplay($xyPush) MouseMove($xyPush[1], $xyPush[2], 3) If $xyPush[3] Then MouseClick($MOUSE_CLICK_LEFT) ElseIf $xyPush[4] Then MouseClick($MOUSE_CLICK_RIGHT) EndIf EndIf EndIf Sleep(10) WEnd some (unnececcary?) mousemoves and also left Mouseclicks are not done, because i check arrayelemntcount >3. So i suggest think about your concept and send Mouse- X| Mouse- Y|Mousebutton ("primary" or "secondary") and use only the MousClick func which is able to click at secified coordinates. So the errorchecking is very easy: While 1 Local $Cordinate = MyTCP_Client($sIPAddress, $iPort)&"|" ConsoleWrite($Cordinate) Local $xyPush = StringSplit($Cordinate, "|") If IsArray($xyPush) Then ;is a Array? ;yes is a array so do: ;_ArrayDisplay($xyPush) If $xyPush[0] > 2 Then ;Are at least 3 elements in array? ;yes so do: MouseClick($xyPush[3],$xyPush[1],$xyPush[2]) EndIf EndIf Sleep(10) WEnd Link to comment Share on other sites More sharing options...
faustf Posted July 18, 2016 Author Share Posted July 18, 2016 but have another little problem i dont know why after 40-42 mailings-coordinate the client-slave stop to work o_O but not send error >"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "\\192.168.1.7\Public (Read Write)\replicator\slave.au3" 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 133 504 >Exit code: 0 Time: 66.91 Link to comment Share on other sites More sharing options...
faustf Posted July 19, 2016 Author Share Posted July 19, 2016 hi @autobert i try to use your script but , i have this problem in console go right , i see a coordinate and 1 , when push , but the pointer of mouse stay quiet this is last script used #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <Array.au3> #include <Misc.au3> TCPStartup() ; Start the TCP service. ; Register OnAutoItExit to be called when the script is closed. OnAutoItExitRegister("OnAutoItExit") ; Assign Local variables the loopback IP Address and the Port. Local $sIPAddress = "192.168.1.103" ; This IP Address only works for testing on your own computer. Local $iPort = 33569 ; Port used for the connection. Global $iSocket = TCPConnect($sIPAddress, $iPort) While 1 While _IsPressed("11") And _IsPressed("12") And _IsPressed("58") Exit WEnd Local $Cordinate = TCPRecv($iSocket, 30) & "|" ConsoleWrite($Cordinate) Local $xyPush = StringSplit($Cordinate, "|") If IsArray($xyPush) Then ;is a Array? ;yes is a array so do: ;_ArrayDisplay($xyPush) If $xyPush[0] > 2 Then ;Are at least 3 elements in array? ;yes so do: MouseClick($xyPush[3], $xyPush[1], $xyPush[2]) EndIf EndIf Sleep(10) WEnd ;#ce Func OnAutoItExit() TCPShutdown() ; Close the TCP service. EndFunc ;==>OnAutoItExit Link to comment Share on other sites More sharing options...
faustf Posted July 19, 2016 Author Share Posted July 19, 2016 $xyPush[0] is always 2 never change Link to comment Share on other sites More sharing options...
AutoBert Posted July 19, 2016 Share Posted July 19, 2016 (edited) 1 hour ago, faustf said: hi @autobert i try to use your script i never post a script only snipets and for the one you using now, you have to change your server as i suggested: 23 hours ago, AutoBert said: So i suggest think about your concept and send Mouse- X| Mouse- Y|Mousebutton ("primary" or "secondary") and use only the MousClick func which is able to click at secified coordinates. without changing server to Func Check() ;Function for processing Local $Mouse_button If $Clients[0][0] < 1 Then Return ;If there are no clients connected, stop the function right now For $i = 1 To $Clients[0][0] ;Loop through all connected clients Local $pos = MouseGetPos() If _IsPressed("01", $hDLL) Then $Mouse_button = 'primary' ElseIf _IsPressed("02", $hDLL) Then $Mouse_button= 'secondary' EndIf TCPSend($Clients[$i][0], $pos[0] & "|" & $pos[1] & "|" & $Mouse_button & @CRLF) $sRecv = TCPRecv($Clients[$i][0], $PacketSize) ;Read $PacketSize bytes from the current client's buffer ;ConsoleWrite($pos[0] & " " & $pos[1] & " - " & $Mouse_button & & @CRLF) Next EndFunc ;==>Check the client couldn't receive data in now exspected format: X|Y|Button, where x=integer y=integer and Button a string with these possible values: "" or "primary" or "secondary". Edited July 19, 2016 by AutoBert Link to comment Share on other sites More sharing options...
faustf Posted July 19, 2016 Author Share Posted July 19, 2016 questions , i have modify the master-server and client -slave , but now if i run it the mouse mouve like drunked like arrive random move , i try to understund why , and i see , i dont know mixed a coordinate and increase a array , but why ??? o_O this is a client-slave expandcollapse popup#include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <Array.au3> #include <Misc.au3> TCPStartup() ; Start the TCP service. ; Register OnAutoItExit to be called when the script is closed. OnAutoItExitRegister("OnAutoItExit") ; Assign Local variables the loopback IP Address and the Port. Local $sIPAddress = "192.168.1.6" ; This IP Address only works for testing on your own computer. Local $iPort = 33569 ; Port used for the connection. Global $iSocket = TCPConnect($sIPAddress, $iPort) Global $X, $Y While 1 While _IsPressed("11") And _IsPressed("12") And _IsPressed("58") Exit WEnd Local $Cordinate = TCPRecv($iSocket, 30) ;& "|" ConsoleWrite($Cordinate) Local $xyPush = StringSplit($Cordinate, " ") _ArrayDisplay($xyPush) If IsArray($xyPush) Then ;is a Array? ;If $X <> $xyPush[1] And $Y <> $xyPush[2] Then ;MsgBox(0,'',$xyPush[0]) ;yes is a array so do: ;_ArrayDisplay($xyPush) If $xyPush[0] > 1 Then MouseMove($xyPush[1], $xyPush[2], 3) EndIf ;$X = $xyPush[1] ;MsgBox (0,'X',$X) ;MsgBox (0,'XY',$xyPush[1]) ;$Y = $xyPush[2] ;EndIf If $xyPush[0] > 2 Then ;Are at least 3 elements in array? ;yes so do: If $xyPush[3] = 1 Then MouseClick($MOUSE_CLICK_LEFT) ElseIf $xyPush[4] = 1 Then MouseClick($MOUSE_CLICK_RIGHT) EndIf ;MsgBox(0, '', 'sono qui') MouseClick($xyPush[3], $xyPush[1], $xyPush[2]) EndIf ;_ArrayDelete($xyPush,UBound($xyPush)) EndIf Sleep(10) WEnd Func OnAutoItExit() TCPShutdown() ; Close the TCP service. EndFunc ;==>OnAutoItExit this is a master-server expandcollapse popup#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.8.1 (Or greater) Author: Ken Piper Script Function: Template multi-client server base code. Use as a base for making an efficient server program. This base will just accept connections and echo back what it receives, and kill the connection if it is dead or inactive for x seconds. It will not do any other work, that must be added seperately! #ce ---------------------------------------------------------------------------- #include <Misc.au3> TCPStartup() Opt("TCPTimeout", 0) #Region ;Safe-to-edit things are below Global $BindIP = "0.0.0.0" ;Listen on all addresses Global $BindPort = 33569 ;Listen on port 8080 Global $Timeout = 31536000000 ;Max idle time is one year seconds before calling a connection "dead" Global $PacketSize = 2048 ;Max packet size per-check is 2KB Global $MaxClients = 50 ;Max simultaneous clients is 50 #EndRegion ;Safe-to-edit things are below Global $Listen Global $Clients[1][4] ;[Index][Socket, IP, Timestamp, Buffer] Global $Ws2_32 = DllOpen("Ws2_32.dll") ;Open Ws2_32.dll, it might get used a lot Global $NTDLL = DllOpen("ntdll.dll") ;Open ntdll.dll, it WILL get used a lot Global $CleanupTimer = TimerInit() ;This is used to time when things should be cleaned up Global $hDLL = DllOpen("user32.dll") Global $Mouse_button_dx, $Mouse_button_sx OnAutoItExitRegister("Close") ;Register this function to be called if the server needs to exit Global $X=0, $Y=0 $Clients[0][0] = 0 $Listen = TCPListen($BindIP, $BindPort, $MaxClients) ;Start listening on the given IP/port If @error Then Exit 1 ;Exit with return code 1 if something was already bound to that IP and port While 1 USleep(5000, $NTDLL) ;This is needed because TCPTimeout is disabled. Without this it will run one core at ~100%. ;The USleep function takes MICROseconds, not milliseconds, so 1000 = 1ms delay. ;When working with this granularity, you have to take in to account the time it takes to complete USleep(). ;1000us (1ms) is about as fast as this should be set. If you need more performance, set this from 5000 to 1000, ;but doing so will make it consume a bit more CPU time to get that extra bit of performance. Check() ;Check recv buffers and do things If TimerDiff($CleanupTimer) > 1000 Then ;If it has been more than 1000ms since Cleanup() was last called, call it now $CleanupTimer = TimerInit() ;Reset $CleanupTimer, so it is ready to be called again Cleanup() ;Clean up the dead connections EndIf Local $iSock = TCPAccept($Listen) ;See if anything wants to connect If $iSock = -1 Then ContinueLoop ;If nothing wants to connect, restart at the top of the loop Local $iSize = UBound($Clients, 1) ;Something wants to connect, so get the number of people currently connected here If $iSize - 1 > $MaxClients And $MaxClients > 0 Then ;If $MaxClients is greater than 0 (meaning if there is a max connection limit) then check if that has been reached TCPCloseSocket($iSock) ;It has been reached, close the new connection and continue back at the top of the loop ContinueLoop EndIf ReDim $Clients[$iSize + 1][4] ;There is room for a new connection, allocate space for it here $Clients[0][0] = $iSize ;Update the number of connected clients $Clients[$iSize][0] = $iSock ;Set the socket ID of the connection $Clients[$iSize][1] = SocketToIP($iSock, $Ws2_32) ;Set the IP Address the connection is from $Clients[$iSize][2] = TimerInit() ;Set the timestamp for the last known activity timer $Clients[$iSize][3] = "" ;Blank the recv buffer WEnd Func Check() ;Function for processing If $Clients[0][0] < 1 Then Return ;If there are no clients connected, stop the function right now For $i = 1 To $Clients[0][0] ;Loop through all connected clients Local $pos = MouseGetPos() If _IsPressed("01", $hDLL) Then $Mouse_button_sx = 1 ElseIf _IsPressed("02", $hDLL) Then $Mouse_button_dx = 1 EndIf Select Case $X <> $pos[0] And $Y <> $pos[1] TCPSend($Clients[$i][0], $pos[0] & " " & $pos[1] & " " & $Mouse_button_sx & " " & $Mouse_button_dx & @CRLF) ;$sRecv = TCPRecv($Clients[$i][0], $PacketSize) ;Read $PacketSize bytes from the current client's buffer ;ConsoleWrite($pos[0] & " " & $pos[1] & " - " & $Mouse_button_sx & " - " & $Mouse_button_dx & @CRLF) $Mouse_button_dx = "" $Mouse_button_sx = "" $X=$pos[0] $Y=$pos[1] Case $X = $pos[0] And $Y = $pos[1] And $Mouse_button_sx <> "" Or $Mouse_button_dx <> "" TCPSend($Clients[$i][0], $pos[0] & " " & $pos[1] & " " & $Mouse_button_sx & " " & $Mouse_button_dx & @CRLF) $Mouse_button_dx = "" $Mouse_button_sx = "" EndSelect #cs If $sRecv <> "" Then $Clients[$i][3] &= $sRecv ;If there was more data sent from the client, add it to the buffer If $Clients[$i][3] = "" Then ContinueLoop ;If the buffer is empty, stop right here and check more clients $Clients[$i][2] = TimerInit() ;If it got this far, there is data to be parsed, so update the activity timer #region ;Example packet processing stuff here. This is handling for a simple "echo" server with per-packet handling $sRecv = StringLeft($Clients[$i][3], StringInStr($Clients[$i][3], @CRLF, 0, -1)) ;Pull all data to the left of the last @CRLF in the buffer ;This does NOT pull the first complete packet, this pulls ALL complete packets, leaving only potentially incomplete packets in the buffer If $sRecv = "" Then ContinueLoop ;Check if there were any complete "packets" $Clients[$i][3] = StringTrimLeft($Clients[$i][3], StringLen($sRecv) + 1) ;remove what was just read from the client's buffer $sPacket = StringSplit($sRecv, @CRLF, 1) ;Split all complete packets up in to an array, so it is easy to work with them For $j = 1 To $sPacket[0] ;Loop through each complete packet; This is where any packet processing should be done TCPSend($Clients[$i][0], "Echoing line: " & $sPacket[$j] & @CRLF) ;Echo back the packet the client sent Next #endregion ;Example #ce Next EndFunc ;==>Check Func Cleanup() ;Clean up any disconnected clients to regain resources If $Clients[0][0] < 1 Then Return ;If no clients are connected then return Local $iNewSize = 0 For $i = 1 To $Clients[0][0] ;Loop through all connected clients $Clients[$i][3] &= TCPRecv($Clients[$i][0], $PacketSize) ;Dump any data not-yet-seen in to their recv buffer If @error > 0 Or TimerDiff($Clients[$i][2]) > $Timeout Then ;Check to see if the connection has been inactive for a while or if there was an error TCPCloseSocket($Clients[$i][0]) ;If yes, close the connection $Clients[$i][0] = -1 ;Set the socket ID to an invalid socket Else $iNewSize += 1 EndIf Next If $iNewSize < $Clients[0][0] Then ;If any dead connections were found, drop them from the client array and resize the array Local $iSize = UBound($Clients, 2) - 1 Local $aTemp[$iNewSize + 1][$iSize + 1] Local $iCount = 1 For $i = 1 To $Clients[0][0] If $Clients[$i][0] = -1 Then ContinueLoop For $j = 0 To $iSize $aTemp[$iCount][$j] = $Clients[$i][$j] Next $iCount += 1 Next $aTemp[0][0] = $iNewSize $Clients = $aTemp EndIf EndFunc ;==>Cleanup Func Close() DllClose($Ws2_32) ;Close the open handle to Ws2_32.dll DllClose($NTDLL) ;Close the open handle to ntdll.dll For $i = 1 To $Clients[0][0] ;Loop through the connected clients TCPCloseSocket($Clients[$i][0]) ;Force the client's connection closed Next TCPShutdown() ;Shut down networking stuff EndFunc ;==>Close Func SocketToIP($iSock, $hDLL = "Ws2_32.dll") ;A rewrite of that _SocketToIP function that has been floating around for ages Local $structName = DllStructCreate("short;ushort;uint;char[8]") Local $sRet = DllCall($hDLL, "int", "getpeername", "int", $iSock, "ptr", DllStructGetPtr($structName), "int*", DllStructGetSize($structName)) If Not @error Then $sRet = DllCall($hDLL, "str", "inet_ntoa", "int", DllStructGetData($structName, 3)) If Not @error Then Return $sRet[0] EndIf Return "0.0.0.0" ;Something went wrong, return an invalid IP EndFunc ;==>SocketToIP Func USleep($iUsec, $hDLL = "ntdll.dll") ;A rewrite of the _HighPrecisionSleep function made by monoceres (Thanks!) Local $hStruct = DllStructCreate("int64") DllStructSetData($hStruct, 1, -1 * ($iUsec * 10)) DllCall($hDLL, "dword", "ZwDelayExecution", "int", 0, "ptr", DllStructGetPtr($hStruct)) EndFunc ;==>USleep Link to comment Share on other sites More sharing options...
faustf Posted July 22, 2016 Author Share Posted July 22, 2016 hi guy someone can help me ?? i have big problem and i dont understund how must do i have 2 script , the problem is , tcpsend , send data witout logic , i have created this TCPSend($Clients[$i][0], "|" &$pos[0] & "|" & $pos[1] & "|" & $Mouse_button_sx & "|" & $Mouse_button_dx & @CRLF) in console write good , because i see a coordinate like |122|12|| but after string split create random array , what i could do ?? close connection after one send coordinate and reopen it??? thankz Link to comment Share on other sites More sharing options...
AutoBert Posted July 22, 2016 Share Posted July 22, 2016 as i suggested: On 19.7.2016 at 0:37 PM, AutoBert said: without changing server to Func Check() ;Function for processing Local $Mouse_button If $Clients[0][0] < 1 Then Return ;If there are no clients connected, stop the function right now For $i = 1 To $Clients[0][0] ;Loop through all connected clients Local $pos = MouseGetPos() If _IsPressed("01", $hDLL) Then $Mouse_button = 'primary' ElseIf _IsPressed("02", $hDLL) Then $Mouse_button= 'secondary' EndIf TCPSend($Clients[$i][0], $pos[0] & "|" & $pos[1] & "|" & $Mouse_button & @CRLF) $sRecv = TCPRecv($Clients[$i][0], $PacketSize) ;Read $PacketSize bytes from the current client's buffer ;ConsoleWrite($pos[0] & " " & $pos[1] & " - " & $Mouse_button & & @CRLF) Next EndFunc ;==>Check the client couldn't receive data in now exspected format: X|Y|Button, where x=integer y=integer and Button a string with these possible values: "" or "primary" or "secondary". and client as i suggest: On 18.7.2016 at 1:19 PM, AutoBert said: So i suggest think about your concept and send Mouse- X| Mouse- Y|Mousebutton ("primary" or "secondary") and use only the MousClick func which is able to click at secified coordinates. So the errorchecking is very easy: While 1 Local $Cordinate = MyTCP_Client($sIPAddress, $iPort)&"|" ConsoleWrite($Cordinate) Local $xyPush = StringSplit($Cordinate, "|") If IsArray($xyPush) Then ;is a Array? ;yes is a array so do: ;_ArrayDisplay($xyPush) If $xyPush[0] > 2 Then ;Are at least 3 elements in array? ;yes so do: MouseClick($xyPush[3],$xyPush[1],$xyPush[2]) EndIf EndIf Sleep(10) WEnd both must follow same logic. If this not works, go back to solution you tested here: On 18.7.2016 at 0:40 PM, faustf said: ok work good , i used the first solution Link to comment Share on other sites More sharing options...
faustf Posted July 22, 2016 Author Share Posted July 22, 2016 i modify little bit server and client but the èroblem is , when you move mouse you create many coordinates and this create problem , look and try master-server expandcollapse popup#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.8.1 (Or greater) Author: Ken Piper Script Function: Template multi-client server base code. Use as a base for making an efficient server program. This base will just accept connections and echo back what it receives, and kill the connection if it is dead or inactive for x seconds. It will not do any other work, that must be added seperately! #ce ---------------------------------------------------------------------------- #include <Misc.au3> #include <Array.au3> SoundPlay (@ScriptDir &"\darth_vader.mp3",0) TCPStartup() Opt("TCPTimeout", 0) #Region ;Safe-to-edit things are below Global $BindIP = "0.0.0.0" ;Listen on all addresses Global $BindPort = 33569 ;Listen on port 8080 Global $Timeout = 31536000000 ;Max idle time is one year seconds before calling a connection "dead" Global $PacketSize = 2048 ;Max packet size per-check is 2KB Global $MaxClients = 50 ;Max simultaneous clients is 50 #EndRegion ;Safe-to-edit things are below Global $Listen Global $Clients[1][4] ;[Index][Socket, IP, Timestamp, Buffer] Global $Ws2_32 = DllOpen("Ws2_32.dll") ;Open Ws2_32.dll, it might get used a lot Global $NTDLL = DllOpen("ntdll.dll") ;Open ntdll.dll, it WILL get used a lot Global $CleanupTimer = TimerInit() ;This is used to time when things should be cleaned up Global $hDLL = DllOpen("user32.dll") Global $Mouse_button_dx, $Mouse_button_sx OnAutoItExitRegister("Close") ;Register this function to be called if the server needs to exit Global $X=0, $Y=0 $Clients[0][0] = 0 $Listen = TCPListen($BindIP, $BindPort, $MaxClients) ;Start listening on the given IP/port If @error Then Exit 1 ;Exit with return code 1 if something was already bound to that IP and port While 1 USleep(5000, $NTDLL) ;This is needed because TCPTimeout is disabled. Without this it will run one core at ~100%. ;The USleep function takes MICROseconds, not milliseconds, so 1000 = 1ms delay. ;When working with this granularity, you have to take in to account the time it takes to complete USleep(). ;1000us (1ms) is about as fast as this should be set. If you need more performance, set this from 5000 to 1000, ;but doing so will make it consume a bit more CPU time to get that extra bit of performance. Check() ;Check recv buffers and do things If TimerDiff($CleanupTimer) > 1000 Then ;If it has been more than 1000ms since Cleanup() was last called, call it now $CleanupTimer = TimerInit() ;Reset $CleanupTimer, so it is ready to be called again Cleanup() ;Clean up the dead connections EndIf Local $iSock = TCPAccept($Listen) ;See if anything wants to connect If $iSock = -1 Then ContinueLoop ;If nothing wants to connect, restart at the top of the loop Local $iSize = UBound($Clients, 1) ;Something wants to connect, so get the number of people currently connected here If $iSize - 1 > $MaxClients And $MaxClients > 0 Then ;If $MaxClients is greater than 0 (meaning if there is a max connection limit) then check if that has been reached TCPCloseSocket($iSock) ;It has been reached, close the new connection and continue back at the top of the loop ContinueLoop EndIf ReDim $Clients[$iSize + 1][4] ;There is room for a new connection, allocate space for it here $Clients[0][0] = $iSize ;Update the number of connected clients $Clients[$iSize][0] = $iSock ;Set the socket ID of the connection $Clients[$iSize][1] = SocketToIP($iSock, $Ws2_32) ;Set the IP Address the connection is from $Clients[$iSize][2] = TimerInit() ;Set the timestamp for the last known activity timer $Clients[$iSize][3] = "" ;Blank the recv buffer WEnd Func Check() ;Function for processing If $Clients[0][0] < 1 Then Return ;If there are no clients connected, stop the function right now For $i = 1 To $Clients[0][0] ;Loop through all connected clients Local $pos = MouseGetPos() ;_ArrayDisplay($pos) If _IsPressed("01", $hDLL) Then $Mouse_button_sx = "sinistro" ElseIf _IsPressed("02", $hDLL) Then $Mouse_button_dx = "destro" EndIf Select Case $X <> $pos[0] And $Y <> $pos[1] ;TCPSend($Clients[$i][0], $pos[0] & " " & $pos[1] & " " & $Mouse_button_sx & " " & $Mouse_button_dx & @CRLF) Local $stringa = ("|START"& "|" &$pos[0] & "|" & $pos[1] & "|" & $Mouse_button_sx & "|" & $Mouse_button_dx &"STOP|"& @CRLF ) ;TCPSend($Clients[$i][0], "|" &$pos[0] & "|" & $pos[1] & "|" & $Mouse_button_sx & "|" & $Mouse_button_dx & @CRLF) TCPSend($Clients[$i][0], $stringa ) Sleep (1111) ;$sRecv = TCPRecv($Clients[$i][0], $PacketSize) ;Read $PacketSize bytes from the current client's buffer ;ConsoleWrite($pos[0] & " " & $pos[1] & " - " & $Mouse_button_sx & " - " & $Mouse_button_dx & @CRLF) $Mouse_button_dx = "" $Mouse_button_sx = "" $X=$pos[0] $Y=$pos[1] Case $X = $pos[0] And $Y = $pos[1] And $Mouse_button_sx <> "" Or $Mouse_button_dx <> "" TCPSend($Clients[$i][0], "|" &$pos[0] & "|" & $pos[1] & "|" & $Mouse_button_sx & "|" & $Mouse_button_dx & @CRLF) $Mouse_button_dx = "" $Mouse_button_sx = "" EndSelect #cs If $sRecv <> "" Then $Clients[$i][3] &= $sRecv ;If there was more data sent from the client, add it to the buffer If $Clients[$i][3] = "" Then ContinueLoop ;If the buffer is empty, stop right here and check more clients $Clients[$i][2] = TimerInit() ;If it got this far, there is data to be parsed, so update the activity timer #region ;Example packet processing stuff here. This is handling for a simple "echo" server with per-packet handling $sRecv = StringLeft($Clients[$i][3], StringInStr($Clients[$i][3], @CRLF, 0, -1)) ;Pull all data to the left of the last @CRLF in the buffer ;This does NOT pull the first complete packet, this pulls ALL complete packets, leaving only potentially incomplete packets in the buffer If $sRecv = "" Then ContinueLoop ;Check if there were any complete "packets" $Clients[$i][3] = StringTrimLeft($Clients[$i][3], StringLen($sRecv) + 1) ;remove what was just read from the client's buffer $sPacket = StringSplit($sRecv, @CRLF, 1) ;Split all complete packets up in to an array, so it is easy to work with them For $j = 1 To $sPacket[0] ;Loop through each complete packet; This is where any packet processing should be done TCPSend($Clients[$i][0], "Echoing line: " & $sPacket[$j] & @CRLF) ;Echo back the packet the client sent Next #endregion ;Example #ce Next EndFunc ;==>Check Func Cleanup() ;Clean up any disconnected clients to regain resources If $Clients[0][0] < 1 Then Return ;If no clients are connected then return Local $iNewSize = 0 For $i = 1 To $Clients[0][0] ;Loop through all connected clients $Clients[$i][3] &= TCPRecv($Clients[$i][0], $PacketSize) ;Dump any data not-yet-seen in to their recv buffer If @error > 0 Or TimerDiff($Clients[$i][2]) > $Timeout Then ;Check to see if the connection has been inactive for a while or if there was an error TCPCloseSocket($Clients[$i][0]) ;If yes, close the connection $Clients[$i][0] = -1 ;Set the socket ID to an invalid socket Else $iNewSize += 1 EndIf Next If $iNewSize < $Clients[0][0] Then ;If any dead connections were found, drop them from the client array and resize the array Local $iSize = UBound($Clients, 2) - 1 Local $aTemp[$iNewSize + 1][$iSize + 1] Local $iCount = 1 For $i = 1 To $Clients[0][0] If $Clients[$i][0] = -1 Then ContinueLoop For $j = 0 To $iSize $aTemp[$iCount][$j] = $Clients[$i][$j] Next $iCount += 1 Next $aTemp[0][0] = $iNewSize $Clients = $aTemp EndIf EndFunc ;==>Cleanup Func Close() DllClose($Ws2_32) ;Close the open handle to Ws2_32.dll DllClose($NTDLL) ;Close the open handle to ntdll.dll For $i = 1 To $Clients[0][0] ;Loop through the connected clients TCPCloseSocket($Clients[$i][0]) ;Force the client's connection closed Next TCPShutdown() ;Shut down networking stuff EndFunc ;==>Close Func SocketToIP($iSock, $hDLL = "Ws2_32.dll") ;A rewrite of that _SocketToIP function that has been floating around for ages Local $structName = DllStructCreate("short;ushort;uint;char[8]") Local $sRet = DllCall($hDLL, "int", "getpeername", "int", $iSock, "ptr", DllStructGetPtr($structName), "int*", DllStructGetSize($structName)) If Not @error Then $sRet = DllCall($hDLL, "str", "inet_ntoa", "int", DllStructGetData($structName, 3)) If Not @error Then Return $sRet[0] EndIf Return "0.0.0.0" ;Something went wrong, return an invalid IP EndFunc ;==>SocketToIP Func USleep($iUsec, $hDLL = "ntdll.dll") ;A rewrite of the _HighPrecisionSleep function made by monoceres (Thanks!) Local $hStruct = DllStructCreate("int64") DllStructSetData($hStruct, 1, -1 * ($iUsec * 10)) DllCall($hDLL, "dword", "ZwDelayExecution", "int", 0, "ptr", DllStructGetPtr($hStruct)) EndFunc ;==>USleep client-slave expandcollapse popup#include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <Array.au3> #include <Misc.au3> TCPStartup() ; Start the TCP service. ; Register OnAutoItExit to be called when the script is closed. OnAutoItExitRegister("OnAutoItExit") ; Assign Local variables the loopback IP Address and the Port. ;Local $sIPAddress = "192.168.1.6" ; This IP Address only works for testing on your own computer. Local $sIPAddress = "127.0.0.1" ; This IP Address only works for testing on your own computer. Local $iPort = 33569 ; Port used for the connection. Global $iSocket = TCPConnect($sIPAddress, $iPort) Global $X, $Y While 1 While _IsPressed("11") And _IsPressed("12") And _IsPressed("58") Exit WEnd Local $Cordinate = TCPRecv($iSocket, 30); & "|" ;MsgBox (0,'',$Cordinate) ConsoleWrite($Cordinate) ;MsgBox (0,'',$Cordinate) Local $xyPush = StringSplit($Cordinate, "|") ;Local $xyPush2 = StringSplit($xyPush[1], " ") _ArrayDisplay($xyPush) If IsArray($xyPush) Then ;is a Array? ;yes is a array so do: ;_ArrayDisplay($xyPush) If $xyPush[1] <> "" Then MouseMove($xyPush[1], $xyPush[2], 3) EndIf If $xyPush[0] > 2 Then ;Are at least 3 elements in array? ;yes so do: If $xyPush[3] = "sinistro" Then MouseClick($MOUSE_CLICK_LEFT) ElseIf $xyPush[4] = "destro" Then MouseClick($MOUSE_CLICK_RIGHT) EndIf ;MsgBox(0, '', 'sono qui') MouseClick($xyPush[3], $xyPush[1], $xyPush[2]) EndIf ;_ArrayDelete($xyPush,UBound($xyPush)) EndIf Sleep(10) WEnd Func OnAutoItExit() TCPShutdown() ; Close the TCP service. EndFunc ;==>OnAutoItExit Link to comment Share on other sites More sharing options...
faustf Posted July 23, 2016 Author Share Posted July 23, 2016 hi gui why tcp send not send only string of packet but send sometime one or two or half string ??? this is a result that i have |START|521|753 ||STOP| |START|359|668|sinist ro|STOP| |START|307|568||STOP |START|760|381||STOP| |START|1117|746|sinistro|STOP| |START|1111|745|sinistro|STO P| |START|773|266|sinistro|ST OP| |773|266|sinistro| |773| 266|sinistro| |773|266|sinist ro| |773|266|sinistro| |773| 266|sinistro| |773|266|sinist ro| |773|266|sinistro| |773| 266|sinistro| |773|266|sinist ro| |773|266|sinistro| |773| 266|sinistro| |773|266|sinist ro| |773|266|sinistro| |773| 266|sinistro| |773|266|sinist ro| |773|266|sinistro| |773| 266|sinistro| |773|266|sinist ro| |773|266|sinistro| |773| 266|sinistro| |773|266|sinist Link to comment Share on other sites More sharing options...
AutoBert Posted July 23, 2016 Share Posted July 23, 2016 i don't invest time in testing when you are changing logic (the format of the tcp-packet), as i can see in #18. None output line is in the format i suggested. Maybe i didn't realy understand what's your need. 16 hours ago, AutoBert said: If this not works, go back to solution you tested here: On 18.7.2016 at 0:40 PM, faustf said: ok work good , i used the first solution i'm out of this thread. Link to comment Share on other sites More sharing options...
faustf Posted July 23, 2016 Author Share Posted July 23, 2016 i tested solution and in first time i belive go good because , in consolewrite , wrtie good but when operate with tcprecev not work good because the problem is a streaming of packet ..... suppose: i create a PACKET like |STRAT|1233|122|primary|STOP| or |STRAT|1233|122|scondary|STOP| or |STRAT|1233|122||STOP| this 3 packet is possibility to send but when recive a client it recive in this mode |START|521|753 <--------- first array ||STOP| <-------- second array |START|359|668|sinist <------- 3° arrayro|STOP||START|307|568||STOP tcprecive , sometime create array of 6 after 12 , after 13 after 9 after 4 in random , and breaks the coordinate or text , in random , and this is impossible for me rebuild it for send a command to a movemouse how is possible avoid this problem? thankz again , 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