Sign in to follow this  
Followers 0
Rux

My attempt at a File Transfer program

1 post in this topic

Hi. I'm new to this forum, but I've been using AutoIt for a few months now. I have already gotten used to the program and have made several things with it.

I don't know if this has been done before (at least successfully) from my searches on the forums, but here is a file transferring program. It is actually 2 programs (a sender and a receiver), and it seems to send files at a nice rate, not super fast or anything, but bearable. I was able to send a 3-4 MB file in about a minute and a half from my computer to another laptop. I don't know how well it does for long distances though, but I'm guessing the same as long as you port forward or whatever. So go and try it out, and tell me what you think.

Be aware it needs my custom library called JDMMisc.au3, I'll post that with the other scripts as well.

CLIENT (Sender)

;JDM File Transfer v.0 (SENDER)

#NoTrayIcon
TCPStartup()
#include <JDMMisc.au3>
#include <String.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiIPAddress.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $pname = "JDM File Transfer v.0 (Sender)", $workdir = ""
Global $connectID, $recv, $timett

Global $SMAIN = GUICreate($pname, 244, 148, 350, 211)
Global $workfileDIR = GUICtrlCreateInput("", 8, 8, 225, 21)
Global $fileBrowseB = GUICtrlCreateButton("Browse", 184, 40, 49, 25)
Global $ipLabel = GUICtrlCreateLabel("IP:", 8, 64, 17, 17)
Global $portLabel = GUICtrlCreateLabel("PORT:", 6, 96, 37, 17)
Global $status = _GUICtrlStatusBar_Create($SMAIN)
_GUICtrlStatusBar_SetSimple($status)
_GUICtrlStatusBar_SetText($status, "Ready")
_GUICtrlStatusBar_SetMinHeight($status, 17)
Global $IPbox = _GUICtrlIpAddress_Create($SMAIN, 32, 64, 101, 17)
_GUICtrlIpAddress_Set($IPbox, "0.0.0.0")
Global $portbox = GUICtrlCreateInput("403", 48, 96, 49, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_NUMBER))
Global $sendB = GUICtrlCreateButton("SEND", 128, 80, 105, 41)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $fileBrowseB
            $workdir = FileOpenDialog($pname, "", "All files (*.*)", 3)
            if @error = 0 AND $workdir <> "" Then
                GUICtrlSetData($workfileDIR, $workdir)
            EndIf
        Case $sendB
            if FileExists(GUICtrlRead($workfileDIR)) = 1 Then
                if sendfile(GUICtrlRead($workfileDIR), _GUICtrlIpAddress_Get($IPbox), GUICtrlRead($portbox)) = 1 Then
                    TCPCloseSocket($connectID)
                    GUICtrlSetState($sendB, $GUI_ENABLE)
                    GUICtrlSetState($portbox, $GUI_ENABLE)
                    _GUICtrlIpAddress_ShowHide($IPbox, @SW_SHOW)
                    GUICtrlSetState($fileBrowseB, $GUI_ENABLE)
                    GUICtrlSetState($workfileDIR, $GUI_ENABLE)
                    _GUICtrlStatusBar_SetText($status, "Ready")
                    MsgBox(64, $pname, "File sent! Time to transfer: "& Round($timett))
                Else
                    TCPCloseSocket($connectID)
                    GUICtrlSetState($sendB, $GUI_ENABLE)
                    GUICtrlSetState($portbox, $GUI_ENABLE)
                    _GUICtrlIpAddress_ShowHide($IPbox, @SW_SHOW)
                    GUICtrlSetState($fileBrowseB, $GUI_ENABLE)
                    GUICtrlSetState($workfileDIR, $GUI_ENABLE)
                    _GUICtrlStatusBar_SetText($status, "Ready")
                    MsgBox(16, $pname, "File was unable to be sent!")
                EndIf
            Else
                MsgBox(16, $pname, "Selected file does not exist.")
            EndIf
    EndSwitch
WEnd

Func sendfile($iFILE, $iIP, $iPORT)
    _GUICtrlStatusBar_SetText($status, "Preparing...")
    Local $data
    
    ;Prepare GUI
    GUICtrlSetState($sendB, $GUI_DISABLE)
    GUICtrlSetState($portbox, $GUI_DISABLE)
    _GUICtrlIpAddress_ShowHide($IPbox, @SW_HIDE)
    GUICtrlSetState($fileBrowseB, $GUI_DISABLE)
    GUICtrlSetState($workfileDIR, $GUI_DISABLE)
    
    ;Prepare Data
    _GUICtrlStatusBar_SetText($status, "Preparing data...")
    $data = FileRead($iFILE)
    $data = _StringToHex($data)
    Local $snddata[Ceiling(StringLen($data) / 100000)]
    $snddata = _splitStrCnt($data, 100000)
    if $snddata = "" Then
        Return 0
    EndIf
    
    ;Connect to reciever.
    _GUICtrlStatusBar_SetText($status, "Connecting...")
    $connectID = TCPConnect($iIP, $iPORT)
    if $connectID = -1 Then Return 0
    
    ;Prepare transfer process.
    _GUICtrlStatusBar_SetText($status, "Preparing transfer...")
    Do
        $recv = TCPRecv($connectID, 1000000)
    Until $recv = "READY1"
    
    Sleep(50)
    TCPSend($connectID, _getFilename($iFILE))
    if @error <> 0 Then
            TCPCloseSocket($connectID)
            MsgBox(63, "TEST", "TEST")
            Return 0
    EndIf
    Sleep(50)
    
    Do
        $recv = TCPRecv($connectID, 1000000)
    Until $recv = "READY2"
    Sleep(1000)
    
    ;Begin transfer
    _GUICtrlStatusBar_SetText($status, "Transfering file...")
    $timett = TimerInit()
    For $count = 0 To Ubound($snddata) - 1
        TCPSend($connectID, $snddata[$count])
        if @error <> 0 Then
            TCPCloseSocket($connectID)
            Return 0
        EndIf
        Sleep(500)
    Next
    TCPSend($connectID, "!DONE!")
    $timett = TimerDiff($timett)
    
    Do
        $recv = TCPRecv($connectID, 1000000)
    Until $recv <> ""
    
    if $recv = "GOOD" Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc

SERVER (Reciever)

;JDM File Transfer v.0 (RECIEVER)

#NoTrayIcon
TCPStartup()
#include <JDMMisc.au3>
#include <String.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $pname = "JDM File Transfer v.0 (Reciever)", $savepath = ""
Global $listen, $accept, $recv, $filename, $timett

Global $RMAIN = GUICreate($pname, 249, 151, 367, 406)
Global $ipLabel = GUICtrlCreateLabel("IP:", 8, 8, 17, 17)
Global $portLabel = GUICtrlCreateLabel("PORT:", 8, 32, 37, 17)
Global $urIP = GUICtrlCreateLabel(@IPAddress1, 24, 8, 93, 17)
Global $portBox = GUICtrlCreateInput("403", 48, 32, 49, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_NUMBER))
Global $status = _GUICtrlStatusBar_Create($RMAIN)
_GUICtrlStatusBar_SetSimple($status)
_GUICtrlStatusBar_SetText($status, "Ready")
_GUICtrlStatusBar_SetMinHeight($status, 17)
Global $transfer2box = GUICtrlCreateInput("", 8, 104, 233, 21)
Global $browseB = GUICtrlCreateButton("Browse", 8, 72, 65, 25)
Global $recieveB = GUICtrlCreateButton("RECIEVE", 120, 8, 121, 81)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $browseB
            $savepath = FileSelectFolder("Choose a directory to save the transfered file.", "", 1)
            if @error = 0 AND $savepath <> "" Then
                GUICtrlSetData($transfer2box, $savepath)
            EndIf
        Case $recieveB
            if FileExists(GUICtrlRead($transfer2box)) = 1 Then
                if recvfile(GUICtrlRead($transfer2box), @IPAddress1, GUICtrlRead($portBox)) = 1 Then
                    _GUICtrlStatusBar_SetText($status, "Ready")
                    GUICtrlSetState($recieveB, $GUI_ENABLE)
                    GUICtrlSetState($portBox, $GUI_ENABLE)
                    GUICtrlSetState($browseB, $GUI_ENABLE)
                    GUICtrlSetState($transfer2box, $GUI_ENABLE)
                    MsgBox(64, $pname, "File recieved! Time to transfer: "& Round($timett))
                Else
                    _GUICtrlStatusBar_SetText($status, "Ready")
                    GUICtrlSetState($recieveB, $GUI_ENABLE)
                    GUICtrlSetState($portBox, $GUI_ENABLE)
                    GUICtrlSetState($browseB, $GUI_ENABLE)
                    GUICtrlSetState($transfer2box, $GUI_ENABLE)
                    MsgBox(16, $pname, "File was not recieved successfully.")
                EndIf
            Else
                MsgBox(16, $pname, "File path does not exist.")
            EndIf
    EndSwitch
WEnd

Func recvfile($iSAVEDIR, $iIP, $iPORT)
    _GUICtrlStatusBar_SetText($status, "Preparing...")
    Local $data = ""
    
    ;Prepare GUI
    GUICtrlSetState($recieveB, $GUI_DISABLE)
    GUICtrlSetState($portBox, $GUI_DISABLE)
    GUICtrlSetState($browseB, $GUI_DISABLE)
    GUICtrlSetState($transfer2box, $GUI_DISABLE)
    
    ;Wait for connection.
    _GUICtrlStatusBar_SetText($status, "Waiting for a sender to connect...")
    $listen = TCPListen($iIP, $iPORT)
    if $listen = -1 Then Return 0
    Do
        $accept = TCPAccept($listen)
    Until $accept <> -1
    
    ;Prepare transfer process
    _GUICtrlStatusBar_SetText($status, "Preparing transfer...")
    Sleep(1000)
    TCPSend($accept, "READY1")
    Sleep(10)
    
    Do
        $recv = TCPRecv($accept, 1000000)
    Until $recv <> ""
    $filename = $recv
    Sleep(1000)
    TCPSend($accept, "READY2")
    Sleep(50)
    
    ;Begin file transfer
    _GUICtrlStatusBar_SetText($status, "Transfering file...")
    $timett = TimerInit()
    Do
        Do
            $recv = TCPRecv($accept, 1000000)
        Until $recv <> ""
        if $recv <> "!DONE!" Then
            $data = $data & _HexToString($recv)
        EndIf
    Until $recv = "!DONE!"
    Sleep(1000)
    FileWrite($iSAVEDIR &"\"& $filename, $data)
    $timett = TimerDiff($timett)
    if @error = 0 AND FileExists($iSAVEDIR &"\"& $filename) Then
        TCPSend($accept, "GOOD")
        TCPCloseSocket($accept)
        TCPCloseSocket($listen)
        Return 1
    Else
        TCPSend($accept, "BAD")
        TCPCloseSocket($accept)
        TCPCloseSocket($listen)
        Return 0
    EndIf
EndFunc

JDMMisc.au3

;JDM MISC. FUNCTIONS
;-------------------
; _getFilename($iDir, $iExt = 1) ;Gets the name of a file in a directory string (without the main path) without the extension.
;$iDir = The directory string.
;$iExt (Default 1) Whether or not to get the file extension. 1 = YES, 0 = NO.
;RETURN: SUCCEDE = The filename. FAIL = 0.
;
;_getFileExt($iDir) ;Retrieves the file extension.
;$iDir = The directory string.
;RETURN: SUCCEDE = The extension. FAIL = 0.
;
;_getFilePath($iDir) ;Returns the file's containing folder without the file name.
;$iDir = The file to get the path from.
;RETURN: SUCCEDE = The containing folder. FAIL = 0.
;
;_splitStrCnt($iSTR, $iNUMB) ;Splits a string every $iNUMB characters.
;$iSTR = The string to work with.
;$iNUMB = The number of characters to split.
;RETURN: SUCCEDE = The array split up. FAIL = "" Blank.
;

#include <String.au3>
#include <Array.au3>
#include <Misc.au3>

Func _getFilename($iDir, $iExt = 1)
    if $iExt <> 0 AND $iExt <> 1 Then
        Return 0
    EndIf
    if $iExt = 1 Then
        $result = StringRight($iDir, StringinStr(_StringReverse($iDir), "\")-1)
        if @error = 0 Then
            Return $result
        Else
            Return 0
        EndIf
    Elseif $iExt = 0 Then
        $result = StringRight($iDir, StringinStr(_StringReverse($iDir), "\")-1)
        $result2 = StringTrimRight($result, StringInStr(_StringReverse($iDir), "."))
        if @error = 0 Then
            Return $result2
        Else
            Return 0
        EndIf
    EndIf
EndFunc

Func _getFileExt($iDir)
    $pos = StringInStr($iDir, ".")
    $result = StringMid($iDir, $pos, -1)
    if @error = 0 Then
        Return $result
    Elseif @error <> 0 OR $pos = 0 Then
        Return 0
    Else
        Return 0
    EndIf
EndFunc

Func _getFilePath($iDir)
    $result = StringTrimRight($iDir, StringInStr(_StringReverse($iDir), "\"))
    if $result = "0" OR @error <> 0 Then
        Return 0
    Else
        Return $result
    EndIf
EndFunc

Func _splitStrCnt($iSTR, $iNUMB)
    Local $result[Ceiling(StringLen($iSTR) / $iNUMB)], $main
    $splits = Ceiling(StringLen($iSTR)) / $iNUMB
    $main = $iSTR
    For $count = 0 To $splits
        $result[$count] = StringLeft($main, $iNUMB)
        if StringLen($main) >= $iNUMB Then
            $main = StringTrimLeft($main, $iNUMB)
        EndIf
    Next
    Return $result
EndFunc

If you don't want to use these scripts, get the compiled version here:

http://www.mediafire.com/?31960i2avmb6fub

Share this post


Link to post
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
Sign in to follow this  
Followers 0