Jump to content

AutoIt File Transfer


khui
 Share

Recommended Posts

Hi, im pretty new to AutoIt and this forum but i have experience with other scripting languages but i've hit a bump!

Server:

Func recv()
    If FileExists("C:\Temp\recv\IMG.jpg") Then
        FileDelete("C:\Temp\recv\IMG.jpg")
    EndIf
    
    For $0 = 1 To $maxcon
        $Recv = TCPRecv($SocketData[$0],99999999)
        
        If StringInStr($Recv,"cmd:") Then
            ToolTip("Nr." & $0 & " : " & $Recv,0,0,"[-CLIENT-]")
        EndIf
    
        If $Recv <> "" And $Recv <> StringInStr($Recv,"cmd:") Then
            MsgBox(1,"helo","debug")
            $f = FileOpen("C:\Temp\recv\IMG.jpg",1)
            FileWrite($f,$Recv)
            FileClose($f)
        EndIf
    Next
EndFunc

When delete is commented out and the file doesnt already exsist it works, takes a picture and sends it but only ONCE(then youll have 2 delete pic manually)

When its NOT commented out it keeps deleteing the file because of the For-loop even thought its not inside of it.

My question is: How can i prevent this from happening?

ASAP please.

Edited by khui

i > *

Link to comment
Share on other sites

I don't see any recursion, so as an isolated function, the delete should not loop.

I would suggest looking elsewhere in your code. Something must be happening which causes this function being called over and over.

I am pretty sure of it, but ill paste whole the code this time.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Misc.au3>
_GDIPlus_Startup()
$dll = DllOpen("user32.dll")
    Local $szIPADDRESS = "0.0.0.0"
    Local $nPORT = 28000
    Local $MainSocket
    Local $recv, $maxcon, $nr
    $maxcon = 1337
    $0 = 0
    $00 = 0
    $doOnce = 1
    Dim $SocketData[$maxcon + 1]

    ; Start The TCP Services
    ;==============================================
    TCPStartup()
    ; Create a Listening "SOCKET".
    ;Using your IP Address and Port 28000.
    ;==============================================
    $MainSocket = TCPListen($szIPADDRESS, $nPORT, $maxcon)
    ; If the Socket creation fails, exit.
    If $MainSocket = -1 Then Exit
        
        $hGUI = GUICreate("Mouse Control",@DesktopWidth,@DesktopHeight,0,0)
        $hBitmap = _GDIPlus_BitmapCreateFromFile("C:\Temp\recv\IMG.jpg")
        $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
        
    Main()
Func Main()
    GUISetState(@SW_HIDE)
    
    While 1
        HotKeySet("{F2}","MouseM")
        Sleep(50)
        HotKeySet("^m","send2socket")
        Accept()
        recv()
    WEnd
    
    TCPShutdown()
EndFunc






Func Accept()
    If $SocketData[0] = $maxcon Then Return
    $Accept = TCPAccept($MainSocket)
    If $Accept = -1 Then Return
        For $0 = 1 to $maxcon
            
            
            If $SocketData[$0] = 0 Then
                $SocketData[$0] = $Accept
                $SocketData[$0] += 1
                $00 = "New Client has Connected." & @CRLF
                ToolTip("Nr." & $0 & " : " & $Recv,0,0,"[-SERVER-]")
                $00 = 0
                $0 = 0
                Return
            EndIf
        Next
    Return
EndFunc
    
Func recv()
    If FileExists("C:\Temp\recv\IMG.jpg") Then
        ;FileDelete("C:\Temp\recv\IMG.jpg")
    EndIf
    
    For $0 = 1 To $maxcon
        $Recv = TCPRecv($SocketData[$0],99999999)
        
        If StringInStr($Recv,"cmd:") Then
            ToolTip("Nr." & $0 & " : " & $Recv,0,0,"[-CLIENT-]")
        EndIf
    
        If $Recv <> "" And $Recv <> StringInStr($Recv,"cmd:") Then
            ;MsgBox(1,"helo","debug")
            $f = FileOpen("C:\Temp\recv\IMG.jpg",1)
            FileWrite($f,$Recv)
            FileClose($f)
        EndIf
    Next
EndFunc
    
    
Func send2socket()
    $0 = InputBox ('Message','MessageBox test',"")
    For $00 = 1 To $maxcon
        TCPSend ($SocketData[$00], "cmd:" & $0)
        ;sends to every client.
    Next
    $0 = 0
    $00 = 0
    Return
EndFunc


Func MouseM()
        $nr = InputBox("Client Nr","What is the client number?")
        GUISetState(@SW_SHOW)
        While 1
        HotKeySet("{F2}","Main")
        
        If _IsPressed("01", $dll) Then
            TCPSend ($SocketData[$nr], "cmd:mouseLeft" & @CRLF)
            Sleep(7)
        EndIf
        
        If _IsPressed("02", $dll) Then
            TCPSend ($SocketData[$nr], "cmd:mouseRight" & @CRLF)
            Sleep(7)
        EndIf
        
        Sleep(10)
        
        TCPSend ($SocketData[$nr], "cmd:mousepos:" & MouseGetPos(0) & ":" & MouseGetPos(1) & @CRLF)
        ;sends to every client.
        WEnd
    EndFunc
Edited by khui

i > *

Link to comment
Share on other sites

If you don't delete the file then when you recieve another jpg it will be added to the end of the file and when you view it all you will see is the first image. I would expect that you need 2 as a flag for FileOpen rather than 1. Then it will overwrite whatever was in th efile.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

If you don't delete the file then when you recieve another jpg it will be added to the end of the file and when you view it all you will see is the first image. I would expect that you need 2 as a flag for FileOpen rather than 1. Then it will overwrite whatever was in th efile.

Thanks for your reply, but now i have another problem(might just be me not seeing some simple error):

Func recv()
    
    For $0 = 1 To $maxcon
        $Recv = TCPRecv($SocketData[$0],99999999)
        
        If StringInStr($Recv,"cmd:") Then
            ToolTip("Nr." & $0 & " : " & $Recv,0,0,"[-CLIENT-]")
        EndIf
    
        If $Recv <> "" And $Recv <> StringInStr($Recv,"cmd:") Then
            ;MsgBox(1,"helo","debug")
            $f = FileOpen("C:\Temp\recv\IMG.jpg",2)
            FileWrite($f,$Recv)
            FileClose($f)
        EndIf
    Next
EndFunc

It does write to the file, but the imagefile is corrupt

Yeah, it goes through a for-loop right, and for each one it erases itself now, so i get like, 25% of the file or something like that.

Edited by khui

i > *

Link to comment
Share on other sites

I think my advice was not very clear.

Try something like this

Func recv()
    
Local $f = -1

    For $0 = 1 To $maxcon
        $Recv = TCPRecv($SocketData[$0],99999999)
        
        If StringInStr($Recv,"cmd:") Then
            ToolTip("Nr." & $0 & " : " & $Recv,0,0,"[-CLIENT-]")
        EndIf
    
        If $Recv <> "" And $Recv <> StringInStr($Recv,"cmd:") Then
            ;MsgBox(1,"helo","debug")
            
            if $f = -1 then $f = FileOpen("C:\Temp\recv\IMG.jpg",2)
            FileWrite($f,$Recv)
           
        EndIf
    Next
 if $f <> -1 then FileClose($f)

EndFunc
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I think my advice was not very clear.

Try something like this

Func recv()
    
Local $f = -1

    For $0 = 1 To $maxcon
        $Recv = TCPRecv($SocketData[$0],99999999)
        
        If StringInStr($Recv,"cmd:") Then
            ToolTip("Nr." & $0 & " : " & $Recv,0,0,"[-CLIENT-]")
        EndIf
    
        If $Recv <> "" And $Recv <> StringInStr($Recv,"cmd:") Then
            ;MsgBox(1,"helo","debug")
            
            if $f = -1 then $f = FileOpen("C:\Temp\recv\IMG.jpg",2)
            FileWrite($f,$Recv)
           
        EndIf
    Next
 if $f <> -1 then FileClose($f)

EndFunc

I've tried now for a while, and even if i copied and pasted its still the same thing.

Do you have any idea why?

i > *

Link to comment
Share on other sites

I've tried now for a while, and even if i copied and pasted its still the same thing.

Do you have any idea why?

No idea.

I think you need to post a sample script we can run which sends the file, and a sample script which receives the file and which shows the problem.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

No idea.

I think you need to post a sample script we can run which sends the file, and a sample script which receives the file and which shows the problem.

Alright.

Server:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Misc.au3>

_GDIPlus_Startup()
$dll = DllOpen("user32.dll")


    Local $szIPADDRESS = "0.0.0.0"
    Local $nPORT = 28000
    Local $MainSocket
    Local $recv, $maxcon, $nr
    $maxcon = 1337
    $0 = 0
    $00 = 0
    $doOnce = 1
    Dim $SocketData[$maxcon + 1]


    If $MainSocket = -1 Then Exit
        
        $hGUI = GUICreate("Mouse Control",@DesktopWidth,@DesktopHeight,0,0)
        $hBitmap = _GDIPlus_BitmapCreateFromFile("C:\Temp\recv\IMG.jpg")
        $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
        
    Main()
Func Main()
    GUISetState(@SW_HIDE)
    
    While 1
        HotKeySet("{F2}","MouseM")
        Sleep(50)
        HotKeySet("^m","send2socket")
        Accept()
        recv()
    WEnd
    
    TCPShutdown()
EndFunc






Func Accept()
    If $SocketData[0] = $maxcon Then Return
    $Accept = TCPAccept($MainSocket)
    If $Accept = -1 Then Return
        For $0 = 1 to $maxcon
            
            
            If $SocketData[$0] = 0 Then
                $SocketData[$0] = $Accept
                $SocketData[$0] += 1
                $00 = "New Client has Connected." & @CRLF
                ToolTip("Nr." & $0 & " : " & $Recv,0,0,"[-SERVER-]")
                $00 = 0
                $0 = 0
                Return
            EndIf
        Next
    Return
EndFunc
    
Func recv()
    
Local $f = -1

    For $0 = 1 To $maxcon
        $Recv = TCPRecv($SocketData[$0],99999999)
        
        If StringInStr($Recv,"cmd:") Then
            ToolTip("Nr." & $0 & " : " & $Recv,0,0,"[-CLIENT-]")
        EndIf
    
        If $Recv <> "" And $Recv <> StringInStr($Recv,"cmd:") Then

            If $f = -1 then $f = FileOpen("C:\Temp\recv\IMG.jpg",2)
            FileWrite($f,$Recv)
        EndIf
    Next
    
 If $f <> -1 then FileClose($f)


EndFunc
    
    
Func send2socket()
    $0 = InputBox ('Message','MessageBox test',"")
    For $00 = 1 To $maxcon
        TCPSend ($SocketData[$00], "cmd:" & $0)
        ;sends to every client.
    Next
    $0 = 0
    $00 = 0
    Return
EndFunc


Func MouseM()
        $nr = InputBox("Client Nr","What is the client number?")
        GUISetState(@SW_SHOW)
        While 1
        HotKeySet("{F2}","Main")
        
        If _IsPressed("01", $dll) Then
            TCPSend ($SocketData[$nr], "cmd:mouseLeft" & @CRLF)
            Sleep(7)
        EndIf
        
        If _IsPressed("02", $dll) Then
            TCPSend ($SocketData[$nr], "cmd:mouseRight" & @CRLF)
            Sleep(7)
        EndIf
        
        Sleep(10)
        
        TCPSend ($SocketData[$nr], "cmd:mousepos:" & MouseGetPos(0) & ":" & MouseGetPos(1) & @CRLF)
        ;sends to every client.
        WEnd
    EndFunc

Client:

#include <ScreenCapture.au3>

$myip = "127.0.0.1" ;x.x.x.x
$myport = 28000


Func _FileSend($sFile)
    Local $iMainSocket = -1, $sBuff, $iFileOp,$sRecv
    
    If Not FileExists($sFile) Then Return
    
    $iFileOp = FileOpen($sFile, 16)
    If @error Then Return SetError(3,0,-1)
    $sBuff = Binary(FileRead($iFileOp))
    FileClose($iFileOp)
    TCPSend($sock,$sBuff)
    Return 1
EndFunc


Func StartFunc()
        TCPShutdown()
        Sleep(100)
        TCPStartup()
        Sleep(100)
        $sock = TCPConnect($myip,$myport)
        sleep(2000)
        TCPSend($sock,"cmd:IP:" & @IPAddress1)
    
    
TCPStartup()
$sock = -1

While 1
    If $sock = -1 Then StartFunc()

    sleep(100)
    $recv = TCPRecv($sock,512,0)
    
    
    If StringInStr($recv,"cmd:msg",0) Then
        $STR = StringSplit($recv,":")
        MsgBox(1,"TheItAre",$STR[3])
        TCPSend($sock,"Message sent."& @CRLF)
    EndIf
    
    
    If StringInStr($recv,"cmd:mousepos",0) Then
        $STR = StringSplit($recv,":")
        MouseMove($STR[3],$STR[4],1)
    EndIf
    
    If StringInStr($recv,"cmd:mouseLeft",0) Then
        MouseClick("Left")
    EndIf
    
    
    If StringInStr($recv,"cmd:mouseRight",0) Then
        MouseClick("Right")
    EndIf
    
    
    If StringInStr($recv,"cmd:Prt",0) Then
        _ScreenCapture_SetJPGQuality(30)
        _ScreenCapture_Capture("C:\Temp\IMG.jpg",0,0,@DesktopWidth,@DesktopHeight,True)
        _FileSend("C:\Temp\IMG.jpg")
    EndIf
WEnd

i > *

Link to comment
Share on other sites

Create an account or sign in to comment

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

Create an account

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

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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