Jump to content
Sign in to follow this  
poisonkiller

Slow file sending

Recommended Posts

poisonkiller

I'm making a file sending program (uses TCP and binary like always), but it's sending speed is sooo low and I can't find my mistake. At the moment, only selected file sending is working correctly, so try to look at only that.

Here's the script (quite long, because server and client are in one script):

#include <GuiConstants.au3>
#include <GuiList.au3>
#include <String.au3>
Dim $connect_socket, $connect_socket1, $connect_socket2, $main_socket, $exit = 0, $count = 0, $count2 = 0, $split = "", $i = 1, $file_path_copy = ""
Global $file_cache, $err = 0, $save_buffer = "", $terminate = 0, $recv_all = 0, $received_all = 0, $file_path_recv, $file_path_send, $file_save_path
Global $file, $open, $add, $list, $send, $send_all, $del_selected, $del_all, $progress_bar
TCPStartup()
$sendrecvgui = GuiCreate("Send/Recive file", 169, 143,-1, -1)
GuiCtrlCreateLabel("Senders/" &@CRLF& "Recivers IP:", 10, 10, 60, 50)
$ip1 = GuiCtrlCreateInput(@IPAddress1, 75, 20, 90, 20)
GuiCtrlCreateLabel("Port:", 10, 50, 50, 20)
$port = GuiCtrlCreateInput("8000", 70, 50, 80, 20)
$updown = GuiCtrlCreateUpdown($port)
GuiCtrlSetLimit($updown, 30000, 1)
$connect = GuiCtrlCreateButton("Connect", 55, 118, 60, 20)
$radio1 = GuiCtrlCreateRadio("", 5, 75, 120, 20)
GuiCtrlSetState($radio1, $GUI_CHECKED)
GuiCtrlSetData($radio1, "Start first(Sender)")
$radio2 = GuiCtrlCreateRadio("", 5, 95, 120, 20)
GuiCtrlSetData($radio2, "Start last(Reciver)")
GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        Exit
    Case $msg = $connect
        GuiSetState(@SW_HIDE)
        $1stIP = GuiCTrlRead($ip1)
        $ports = GuiCtrlRead($port)
$wait = GuiCreate("Please wait...", 336, 103,-1, -1)
$label = GuiCtrlCreateLabel("Please wait... Connecting with files reciver/sender.", 40, 20, 260, 60)
GuiCtrlSetFont($label, 16)
GuiSetState()
    If GuiCtrlRead($radio1) = $GUI_CHECKED Then
        $connect_socket = TCPConnect(GuiCTrlRead($ip1), GuiCtrlRead($port))
        $main_socket = TCPListen(GuiCTrlRead($ip1), GuiCtrlRead($port))
        Do
                $msg = GUIGetMsg()
                $connect_socket1 = TCPAccept($main_socket)
                If $msg = $GUI_EVENT_CLOSE Then
                    Exit
                EndIf
            Until $connect_socket1 > 0
        ElseIf GuiCtrlRead($radio2) = $GUI_CHECKED Then
        Sleep(500)
                $connect_socket1 = TCPConnect(GuiCtrlRead($ip1), GuiCtrlRead($port))
            EndIf
    ExitLoop
    Case Else
        ;;;
    EndSelect
WEnd
GuiDelete($sendrecvgui)
Global $bin_end
$bin_end = Binary("RECIVE FILE END")
SendGui()
;--------------------------------------------
Func SendGui()
    GuiDelete($sendrecvgui)
    GuiDelete($wait)
$sendgui = GuiCreate("Send/Recive file(s)", 349, 236,-1, -1)
$file = GuiCtrlCreateInput("", 10, 10, 220, 20)
$open = GuiCtrlCreateButton("Open...", 235, 10, 50, 20)
$add = GuiCtrlCreateButton("Add", 290, 10, 50, 20)
$list = GuiCtrlCreateList("", 10, 40, 330, 97)
$send = GuiCtrlCreateButton("Send selected", 10, 180, 90, 20)
$send_all = GuiCtrlCreateButton("Send all", 240, 180, 100, 20)
$del_selected = GuiCtrlCreateButton("Delete selected", 10, 150, 90, 20)
$del_all = GuiCtrlCreateButton("Delete all", 240, 150, 100, 20)
$progress_bar = GuiCtrlCreateProgress(10, 210, 330, 20)
$progress_lab = GuiCtrlCreateLabel("Progress: 0%", 110, 186, 130, 15)
GuiSetState()
While 1
    $msg = GuiGetMsg()
$msg_recv = TCPRecv($connect_socket1, 512)
    Select
        Case $msg_recv = "EXIT"
            MsgBox(64, "Error", "Connection lost! Press OK to exit...")
            $exit = 1
            Exit
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $open
        $dialog = FileOpenDialog("Select file to send...", @DesktopDir, "All (*.*)")
        If @error then
            ;;;
        Else
            GuiCtrlSetData($file, $dialog)
            EndIf
        $dialog = 0
    Case $msg = $add
        If StringInStr(_GuiCTrlListGetText($list, _GuiCtrlListFindString($list, GuiCtrlRead($file))), GuiCtrlRead($file)) = 0 Then
        GuiCtrlSetData($list, GuiCtrlRead($file)&"|")
        Else
            MsgBox(64, "Error", "Error! File already exists!")
            EndIf
    Case $msg = $del_selected
        _GuiCtrlListDeleteItem($list, _GuiCtrlListSelectedIndex($list))
    Case $msg = $del_all
        _GuiCtrlListClear($list)
    Case $msg = $send
    Send_File($list, $msg_recv, 0, "")
Case $msg = $send_all
    If _GuiCtrlListCount($list) < 1 Then
        MsgBox(64, "Error", "Error! No files to send!")
        Else
    For $i = 1 to _GuiCtrlListCount($list)
        Send_File($list, $msg_recv, 1, _GuiCtrlListGetText($list, $i-1))
        Sleep(100)
        If $i = _GuiCtrlListCount($list) Then
TCPSend($connect_socket1, "RECEIVED ALL")
Else
TCPSend($connect_socket1, "NOT RECEIVED ALL")
EndIf
        If $i = 1 Then
        $file_path_send =  _GuiCtrlListGetText($list, $i-1)
    Else
        $file_path_send = $file_path_send & @CRLF & _GuiCtrlListGetText($list, $i-1)
    EndIf
Next
MsgBox(64, "Done", "Done! You sended files:" & @CRLF & $file_path_send)
    EndIf
Case StringInStr($msg_recv, "RECIVE")
                Recv_File($msg_recv)
                If $received_all = 1 Then
MsgBox(64, "Done", "Done! You received files:" & @CRLF & $file_path_recv)
EndIf
    Case Else
        ;;;
    EndSelect
WEnd
Exit
EndFunc

Func OnAutoitExit()
    If $exit <> 1 Then
        TCPSend($connect_socket1, "EXIT")
    EndIf
    TCPCloseSocket($main_socket)
    TCPCloseSocket($connect_socket)
    TCPCloseSocket($connect_socket1)
    TCPShutdown()
EndFunc

Func FileReadAll($File)
   $OpenFile = FileOpen($File, 0)
   If $OpenFile = -1 Then Return -1
   $ReadAll = FileRead($OpenFile, FileGetSize($File))
   FileClose($OpenFile)
   Return $ReadAll
EndFunc   ;==>FileReadAll

Func Cancel()
TCPSend($connect_socket1, "TERMINATE")
    $terminate = 1
EndFunc

Func Send_File($list, $msg_recv, $all, $text)
    GuiCtrlSetState($file, $GUI_DISABLE)
    GuiCtrlSetState($open, $GUI_DISABLE)
    GuiCtrlSetState($add, $GUI_DISABLE)
    GuiCtrlSetState($list, $GUI_DISABLE)
    GuiCtrlSetState($send, $GUI_DISABLE)
    GuiCtrlSetState($send_all, $GUI_DISABLE)
    GuiCtrlSetState($del_selected, $GUI_DISABLE)
    GuiCtrlSetState($del_all, $GUI_DISABLE)
    If $all = 0 Then
    ;-----------------------------------------SEND-----------------------------------------------------
        TCPSend($connect_socket1, "RECIVE" & _GuiCtrlListGetText($list, _GuiCtrlListSelectedIndex($list)))
        Do
            $msg = GuiGetMsg()
            If $msg = $GUI_EVENT_CLOSE Then Exit
            $msg_recv = TCPRecv($connect_socket1, 512)
    If StringInStr($msg_recv, "EXIT") Then
        MsgBox(16, "Error", "Connection lost! Press OK to exit.")
        Exit
    EndIf
            Until $msg_recv == "RECIVE YES" or $msg_recv == "RECIVE NO"
            If $msg_recv == "RECIVE NO" Then
                MsgBox(64, "Sending rejected", "Other side rejected your file. Press OK to continue...")
            Else
                ;-------------------------------------------------------Filecopy, 1%---------------------------------------------------
                $file_path = _GuiCtrlListGetText($list, _GuiCtrlListSelectedIndex($list))
                $file_path_copy = $file_path
                $file_open = FileOpen($file_path,16)
$file_content = Binary(FileRead($file_open))
$file_bin = BinaryLen($file_content)
FileClose($file_open)
;$1 = Number(BinaryLen($file_content) / 100)
                ;$1 = Round(Number($1), 0) + 1
                HotKeySet("^c", "Cancel")
                TrayTip("Sending...", "Press Ctrl+C to cancel.", 5, 1)
                TCPSend($connect_socket1, "RECIVE FILE START" & $file_bin)
                Sleep(100)
                ;MsgBox(0, "", "File opened, Start sended")
                ;---------------------------------------------For loop-------------------------------------------------------
$file_len = BinaryLen($file_content)/100
$progress_len = 0
Do
    $msg = GuiGetMsg()
            If $msg = $GUI_EVENT_CLOSE Then Exit
        $send = TCPSend($connect_socket1, BinaryMid($file_content, 1, 65000))
        ;$file_content = BinaryMid($file_content, $send+1, $file_bin - $send)
        $file_content = BinaryMid($file_content, $send+1, $file_bin - 65000)
        $progress_len += $send*100/$file_bin
        GuiCtrlSetData($progress_bar, $progress_len)
    If $terminate = 1 Then 
        ExitLoop
    EndIf
    $msg_recv = TCPRecv($connect_socket1, 512)
    If StringInStr($msg_recv, "TERMINATE") Then 
        $terminate = 1
        ExitLoop
EndIf
    If StringInStr($msg_recv, "EXIT") Then
        MsgBox(16, "Error", "Connection lost! Press OK to exit.")
        Exit
    EndIf
Until BinaryLen($file_content) == 0
TCPSend($connect_socket1, $bin_end)
TrayTip("", "", 0)
MsgBox(64, "Done", "Done! You sended file:" & @CRLF & $file_path, 5)
    If $terminate == 0 Then

Else
    TrayTip("", "", 0)
    MsgBox(16, "Error", "Error! Sending terminated!" & @CRLF & "Press OK to cancel.")
EndIf
HotKeySet("^c")
                ;--------------------------------------------------------Send end--------------------------------------------------------
                EndIf
                ;-------------------------------RECIVE----------------------------------------------------------
            $msg_recv = ""
        Else
            ;-----------------------------------------SEND-----------------------------------------------------
        TCPSend($connect_socket1, "1RECIVE" & $text)
        Do
            $msg = GuiGetMsg()
            If $msg = $GUI_EVENT_CLOSE Then Exit
            $msg_recv = TCPRecv($connect_socket1, 512)
    If StringInStr($msg_recv, "EXIT") Then
        MsgBox(16, "Error", "Connection lost! Press OK to exit.")
        Exit
    EndIf
            Until $msg_recv = "RECIVE YES" or $msg_recv = "RECIVE NO"
            If $msg_recv = "RECIVE NO" Then
                MsgBox(64, "Sending rejected", "Other side rejected your file. Press OK to continue...")
            Else
                ;-------------------------------------------------------Filecopy, 1%---------------------------------------------------
                $file_path = $text
                $file_path_copy = $file_path
                $file_open = FileOpen($file_path,16)
$file_content = Binary(FileRead($file_open))
FileClose($file_open)
;$1 = Number(BinaryLen($file_content) / 100)
                ;$1 = Round(Number($1), 0) + 1
                HotKeySet("^c", "Cancel")
                TrayTip("Sending...", "Press Ctrl+C to cancel.", 5, 1)
                TCPSend($connect_socket1, "RECIVE FILE START" & BinaryLen($file_content))
                Sleep(100)
                ;MsgBox(0, "", "File opened, Start sended")
                ;---------------------------------------------For loop-------------------------------------------------------
$file_len = BinaryLen($file_content)/100
$progress_len = 0
Do
    $msg = GuiGetMsg()
            If $msg = $GUI_EVENT_CLOSE Then Exit
        $send = TCPSend($connect_socket1, $file_content)
        $file_content = BinaryMid($file_content, $send+1, BinaryLen($file_content) - $send)
    If $terminate = 1 Then 
        ExitLoop
    EndIf
    $msg_recv = TCPRecv($connect_socket1, 512)
    If StringInStr($msg_recv, "TERMINATE") Then 
        $terminate = 1
        ExitLoop
EndIf
    If StringInStr($msg_recv, "EXIT") Then
        MsgBox(16, "Error", "Connection lost! Press OK to exit.")
        Exit
    EndIf
Until BinaryLen($file_content) == 0
    If $terminate = 0 Then
    TCPSend($connect_socket1, Binary("RECIVE FILE END"))
                    TrayTip("", "", 0)
Else
    TrayTip("", "", 0)
    MsgBox(16, "Error", "Error! Sending terminated!" & @CRLF & "Press OK to cancel.")
EndIf
HotKeySet("^c")
                ;--------------------------------------------------------Send end--------------------------------------------------------
                EndIf
                ;-------------------------------RECIVE----------------------------------------------------------
            $msg_recv = ""
        EndIf
    GuiCtrlSetState($file, $GUI_ENABLE)
    GuiCtrlSetState($open, $GUI_ENABLE)
    GuiCtrlSetState($add, $GUI_ENABLE)
    GuiCtrlSetState($list, $GUI_ENABLE)
    GuiCtrlSetState($send, $GUI_ENABLE)
    GuiCtrlSetState($send_all, $GUI_ENABLE)
    GuiCtrlSetState($del_selected, $GUI_ENABLE)
    GuiCtrlSetState($del_all, $GUI_ENABLE)
EndFunc

Func Recv_File($msg_recv)
    GuiCtrlSetState($file, $GUI_DISABLE)
    GuiCtrlSetState($open, $GUI_DISABLE)
    GuiCtrlSetState($add, $GUI_DISABLE)
    GuiCtrlSetState($list, $GUI_DISABLE)
    GuiCtrlSetState($send, $GUI_DISABLE)
    GuiCtrlSetState($send_all, $GUI_DISABLE)
    GuiCtrlSetState($del_selected, $GUI_DISABLE)
    GuiCtrlSetState($del_all, $GUI_DISABLE)
    If StringLeft($msg_recv, 1) = "1" Then
        $msg_recv = StringTrimLeft($msg_recv, 1)
        While 1
            If $msg_recv = "" Then
            Do
                $msg = GuiGetMsg()
            If $msg = $GUI_EVENT_CLOSE Then Exit
            $msg_recv = TCPRecv($connect_socket1, 512)
    If StringInStr($msg_recv, "EXIT") Then
        MsgBox(16, "Error", "Connection lost! Press OK to exit.")
        FileDelete($file_save_path)
        Exit
    EndIf
Until StringInStr($msg_recv, "1RECIVE")
EndIf
$msg_recv = StringTrimLeft($msg_recv, 1)
            $file_path_right_split = StringSplit(StringRight($msg_recv, 6), ".")
                $file_path_right = $file_path_right_split[$file_path_right_split[0]]
            $split = StringSplit(StringTrimLeft($msg_recv, 6), "\")
                $file_open_path = $split[$split[0]]
                    If $file_path_recv = "" Then
                    $file_path_recv = $file_open_path
                Else
                    $file_path_recv = $file_path_recv & @CRLF & $file_open_path
                EndIf
            $yesno = MsgBox(64 + 4, "Incoming file", "Other side is trying to send a file." &@CRLF& "Do you want to download" &@CRLF& $file_open_path & "?")
            If $yesno = 6 Then
                ;-------------------------------------------Save, recive, start---------------------------------------------------------
                $file_save_path = FileSaveDialog("Save as...", @DesktopDir & "\", "All (*.*)", -1, $file_open_path)
                HotKeySet("^c", "Cancel")
                TrayTip("Receiving...", "Press Ctrl+C to cancel.", 5, 1)
                If @error then
                    TCPSend($connect_socket1, "RECIVE NO")
                Else
                TCPSend($connect_socket1, "RECIVE YES")
        FileDelete($file_save_path)
    $file_cache = Binary($file_cache)
        Do
                $msg = GuiGetMsg()
            If $msg = $GUI_EVENT_CLOSE Then Exit
            $msg_recv = TCPRecv($connect_socket1, 512)
    If StringInStr($msg_recv, "EXIT") Then
        MsgBox(16, "Error", "Connection lost! Press OK to exit.")
        FileDelete($file_save_path)
        Exit
    EndIf
Until StringInStr($msg_recv, "RECIVE FILE START")
$file_len = Number(StringReplace($msg_recv, "RECIVE FILE START", ""))/100
$file_open = FileOpen($file_save_path,17)
;--------------------------------------------------For loop start-----------------------------------------------------
$progress_len = 0
Do
    $msg = GuiGetMsg()
            If $msg = $GUI_EVENT_CLOSE Then Exit
            Do
    $msg_recv = TCPRecv($connect_socket1, 65535)
    Until StringLen($msg_recv)
    $msg_recv_string = BinaryToString($msg_recv)
    If StringInStr($msg_recv_string, "RECIVE FILE END") Then 
         $msg_recv_string = StringReplace($msg_recv_string, "RECIVE FILE END", "")
         FileWrite($file_open, Binary($msg_recv_string))
        ExitLoop
    EndIf
    If $terminate = 1 Then
        ExitLoop
    EndIf
    If @error Then ExitLoop
If StringInStr($msg_recv, "EXIT") Then
        MsgBox(16, "Error", "Connection lost! Press OK to exit.")
        FileDelete($file_save_path)
        Exit
    EndIf
    If StringLen($msg_recv) > 1 Then 
FileWrite($file_open, $msg_recv)
$progress_len = $msg_recv/$file_len+$progress_len
$msg_recv = 0
EndIf
Until StringInStr($msg_recv_string, "RECIVE FILE END")
;------------------------------------------------For loop end-------------------------------------------------------------
If $terminate = 1 Then
MsgBox(16, "Error", "Error! Download terminated!" & @CRLF & "Press OK to cancel.")
TrayTip("", "", 0)
Else
FileClose($file_open)
TrayTip("", "", 0)
EndIf
HotKeySet("^c")
Do
                $msg = GuiGetMsg()
            If $msg = $GUI_EVENT_CLOSE Then Exit
            $msg_recv = TCPRecv($connect_socket1, 512)
    If StringInStr($msg_recv, "EXIT") Then
        MsgBox(16, "Error", "Connection lost! Press OK to exit.")
        FileDelete($file_save_path)
        Exit
    EndIf
Until StringInStr($msg_recv, "RECEIVED ALL") or StringInStr($msg_recv, "NOT RECEIVED ALL")
If StringInStr($msg_recv, "NOT RECEIVED ALL") Then
    $received_all = 0
Else
    $received_all = 1
EndIf
    ;-----------------------------------------------------------Recive end-------------------------------------------------------------
        EndIf
        Else
            TCPSend($connect_socket1, "RECIVE NO")
        EndIf
    $msg_recv = ""
    If $received_all = 1 Then ExitLoop
        Wend
    Else
    $file_path_right_split = StringSplit(StringRight($msg_recv, 6), ".")
                $file_path_right = $file_path_right_split[$file_path_right_split[0]]
            $split = StringSplit(StringTrimLeft($msg_recv, 6), "\")
                $file_open_path = $split[$split[0]]
            $yesno = MsgBox(64 + 4, "Incoming file", "Other side is trying to send a file." &@CRLF& "Do you want to download" &@CRLF& $file_open_path & "?")
            If $yesno = 6 Then
                ;-------------------------------------------Save, recive, start---------------------------------------------------------
                $file_save_path = FileSaveDialog("Save as...", @DesktopDir & "\", "All (*.*)", -1, $file_open_path)
                HotKeySet("^c", "Cancel")
                TrayTip("Receiving...", "Press Ctrl+C to cancel.", 5, 1)
                If @error then
                    TCPSend($connect_socket1, "RECIVE NO")
                Else
                TCPSend($connect_socket1, "RECIVE YES")
        FileDelete($file_save_path)
    $file_cache = Binary($file_cache)
        Do
                $msg = GuiGetMsg()
            If $msg = $GUI_EVENT_CLOSE Then Exit
            $msg_recv = TCPRecv($connect_socket1, 512)
    If StringInStr($msg_recv, "EXIT") Then
        MsgBox(16, "Error", "Connection lost! Press OK to exit.")
        FileDelete($file_save_path)
        Exit
    EndIf
Until StringInStr($msg_recv, "RECIVE FILE START")
$file_len = Number(StringReplace($msg_recv, "RECIVE FILE START", ""))/100
$file_open = FileOpen($file_save_path,17)
;--------------------------------------------------For loop start-----------------------------------------------------
$progress_len = 0
While 1
    $msg = GuiGetMsg()
            If $msg = $GUI_EVENT_CLOSE Then Exit
            Do
    $msg_recv = TCPRecv($connect_socket1, 65535)
    Until StringLen($msg_recv)
    $msg_recv_string = BinaryToString($msg_recv)
    If StringInStr($msg_recv_string, "RECIVE FILE END") Then 
         $msg_recv_string = StringReplace($msg_recv_string, "RECIVE FILE END", "")
         FileWrite($file_open, Binary($msg_recv_string))
        ExitLoop
    EndIf
    If $terminate = 1 Then
        ExitLoop
    EndIf
    If @error Then ExitLoop
If StringInStr($msg_recv, "EXIT") Then
        MsgBox(16, "Error", "Connection lost! Press OK to exit.")
        FileDelete($file_save_path)
        Exit
    EndIf
    If StringLen($msg_recv) > 1 Then 
FileWrite($file_open, $msg_recv)
$progress_len = $msg_recv/$file_len+$progress_len
;ProgressSet($progress_len, "Recived " & Round($progress_len, 0) & "%")
$msg_recv = 0
EndIf
Wend
;------------------------------------------------For loop end-------------------------------------------------------------
If $terminate = 1 Then
MsgBox(16, "Error", "Error! Download terminated!" & @CRLF & "Press OK to cancel.")
TrayTip("", "", 0)
Else
FileClose($file_open)
TrayTip("", "", 0)
MsgBox(64, "Done", "Done! You recived file:" & @CRLF & $file_save_path, 5)
EndIf
HotKeySet("^c")
    ;-----------------------------------------------------------Recive end-------------------------------------------------------------
        EndIf
        Else
            TCPSend($connect_socket1, "RECIVE NO")
        EndIf
    $msg_recv = ""
EndIf
    GuiCtrlSetState($file, $GUI_ENABLE)
    GuiCtrlSetState($open, $GUI_ENABLE)
    GuiCtrlSetState($add, $GUI_ENABLE)
    GuiCtrlSetState($list, $GUI_ENABLE)
    GuiCtrlSetState($send, $GUI_ENABLE)
    GuiCtrlSetState($send_all, $GUI_ENABLE)
    GuiCtrlSetState($del_selected, $GUI_ENABLE)
    GuiCtrlSetState($del_all, $GUI_ENABLE)
EndFunc

Please help!

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  

×