Jump to content

file transfer


Recommended Posts

hello coders!!

I found a few examples about file transfer over the internet but I can't make them work.

Do you have a working example? I don't need any GUI or complex functions. I just send images and executable files from server to client or viceversa.

thank you for your help! :bye:

ehi ehi ehi, what is your name?

Link to comment
Share on other sites

No, I think you can run it with the stable version.

The file is sent by chunks so you can do something else between each sent chunck like displaying the percent sent and it avoids the script to freeze if you have a GUI.

Br, FireFox.

Link to comment
Share on other sites

alright, please help me to get in the case: it seems it blocks many times: it works a few and then the script holds the image without continuing. when you stop it, the image completes because i guess it was blocked from the script.

how do i need to do for avoiding this kind of event? does it need a script pause before its continuation?

thank you, firefox

ehi ehi ehi, what is your name?

Link to comment
Share on other sites

so, I didn't tried with a progress GUI. I'm just transferring files without any control.

when the server executes send instruction, it seems everything worked fine and server continues to run, but the client blocks itself somewhere: it creates the file but it seems it can't close it: indeed, when you terminate the client, the file is unblocked and complete.

I guess I copied the example on the manual but I don't know how to check where script blocks because every cycle it receives a piece of file and this should generate many check messages.

update: it seems like the end of the file isn't reached.

Edited by binarydigit0101

ehi ehi ehi, what is your name?

Link to comment
Share on other sites

#include <File.au3>

$path = INSERT THE PATH FOR THE FILE

TCPStartup()

$ascolto = TCPListen("127.0.0.1", 5555)

While 1

   $socket = TCPAccept($ascolto)

   If $socket <> -1 Then

   ; Assign a Local variable the size of the file previously chosen.
       Local $iFileSize = FileGetSize($percorso)

       ; Assign a Local variable the handle of the file opened in binary mode.
       Local $hFile = FileOpen($percorso, $FO_BINARY)

       ; Assign a Local variable the offset of the file being read.
       Local $iOffset = 0

       ; Assign a Local variable the number representing 4 KiB.
       Local Const $i4KiB = 4096

       ; Note: The file is send by parts of 4 KiB.

       ; Send the binary data of the file to the server.
       Do
           ; Set the file position to the current offset.
           FileSetPos($hFile, $iOffset, $FILE_BEGIN)

           ; The file is read from the position set to 4 KiB and directly wrapped into the TCPSend function.
           TCPSend($Socket, FileRead($hFile, $i4KiB))

           ; If an error occurred display the error code and return False.
           If @error Then
               $iError = @error
               MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not send the data, Error code: " & $iError)

               ; Close the socket.
               TCPCloseSocket($Socket)
               Return False
           EndIf

           ; Increment the offset of 4 KiB to send the next 4 KiB data.
           $iOffset += $i4KiB
       Until $iOffset >= $iFileSize

       ; Close the file handle.
       FileClose($hFile)
       
       ConsoleWrite("ok")
       
    EndIf
    
WEnd
#include <File.au3>

$file = WHERE TO SAVE THE FILE
 
 TCPStartup()
 
$socket = TCPConnect("127.0.0.1", 5555)
 
 ; Assign a Local variable the handle of the file opened in binary overwrite mode.
    Local $hFile = FileOpen($file, BitOR($FO_BINARY, $FO_OVERWRITE))

    ; Assign Locales Constant variables the number representing 4 KiB; the binary code for the end of the file and the length of the binary code.
    Local Const $i4KiB = 4096, $bEOF = Binary(@CRLF & "{EOF}"), $iEOFLen = BinaryLen($bEOF)

    ; Assign a Local variable the empty binary data which will contain the binary data of the file.
    Local $bData = Binary("")

    ; Assign a Local variable to store the length of the data received.
    Local $iDataLen = 0

    ; Assign a Local variable a boolean.
    Local $fEOFReached = False

    Do
         Do
            $bData = TCPRecv($Socket, $i4KiB, 1)
         Until $bData <> ""

        ; If an error occurred display the error code and return False.
        If @error Then
            $iError = @error
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Connection lost, Error code: " & $iError)
            Return False
        EndIf

        $iDataLen = BinaryLen($bData)

        ; If nothing is received, retry for the incoming data.
        If $iDataLen = 0 Then ContinueLoop

        ; If the end of the file is reached.
        If BinaryMid($bData, 1 + $iDataLen - $iEOFLen, $iEOFLen) = $bEOF Then
            ; Strip the EOF code from the file data.
            $bData = BinaryMid($bData, 1, $iDataLen - $iEOFLen)

            ; Set the EOFReached to True.
            $fEOFReached = True
        EndIf

        FileWrite($hFile, $bData)
    Until $fEOFReached

    ; Close the file handle.
    FileClose($hFile)

    ; Display the successful message.
    MsgBox($MB_SYSTEMMODAL, "", "Client:" & @CRLF & "File received.")

above there is the server, under there is the client. insert your paths and check at runtime - here, it receives the file but the file is blocked until the client is stopped. why? >_<

ehi ehi ehi, what is your name?

Link to comment
Share on other sites

I made these examples, and what I can see is that this important line is missing from the server:

; Tell the client the file is fully sent with a code.
TCPSend($iSocket, @CRLF & "{EOF}")
Br, FireFox.

 

good, my copy-abilities are really good, I saw! I only had to copy!! lol - sorry man, I'm so sorry. :

now, the code itself works great. what don't work are TCP functions (send and receive) before and next the code: I guess this operation take a few time. may I slow it down inserting some sleeps before and next the transfer? because I tested it without any pause and it continues to block again.

if you need code, please tell me. :)

thank you :)

ehi ehi ehi, what is your name?

Link to comment
Share on other sites

perfect!! i tried to reproduce what happens to me and i succeed!

what you have to do is to set-up $yourfile(s) vars with parameters you choose and leave $pause at 0.

running the program let you see the server continues to send messages without wait for client receives them.

client will block at your image receiving code which will create the file, keeping it blocked.

when you kill the client, the file is released so it is received (problem solved) but other data after the file are missed.

the server anyway is ready to receive another connection from the client and same events will happen.

i tried to make it working, simply changing time of $pause: it waits for a time after every send to the client -

- i tried it with 5 seconds and seems to work fine but i guess it is so long for a server application.

where am i wrong? how can i fix it?

; THIS IS THE SERVER

#include <File.au3>

TCPStartup()

$listen = TCPListen("127.0.0.1", 5555)

While 1

   $socket = TCPAccept($listen)

   If $socket <> -1 Then
      example()
   EndIf

WEnd

Func example()
   
   $yourfile = "COMPLETE PATH OF FILE TO SEND"
   $pause = 0
   
   $result = TCPSend($socket, "data")
   ConsoleWrite("it is the hell!")
   Sleep($pause)
   $result = TCPSend($socket, "data")
   ConsoleWrite("it is the hell!")
   Sleep($pause)
   
;~      ; Assign a Local variable the size of the file previously chosen.
    Local $iFileSize = FileGetSize($yourfile)

    ; Assign a Local variable the handle of the file opened in binary mode.
    Local $hFile = FileOpen($yourfile, $FO_BINARY)

    ; Assign a Local variable the offset of the file being read.
    Local $iOffset = 0

    ; Assign a Local variable the number representing 4 KiB.
    Local Const $i4KiB = 4096

    ; Note: The file is send by parts of 4 KiB.

    ; Send the binary data of the file to the server.
    Do
        ; Set the file position to the current offset.
        FileSetPos($hFile, $iOffset, $FILE_BEGIN)

        ; The file is read from the position set to 4 KiB and directly wrapped into the TCPSend function.
        TCPSend($Socket, FileRead($hFile, $i4KiB))

        ; If an error occurred display the error code and return False.
        If @error Then
            $iError = @error
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not send the data, Error code: " & $iError)

            ; Close the socket.
            TCPCloseSocket($Socket)
            Return False
        EndIf

        ; Increment the offset of 4 KiB to send the next 4 KiB data.
        $iOffset += $i4KiB
    Until $iOffset >= $iFileSize

    ; Close the file handle.
    FileClose($hFile)

   ; Tell the server the file is fully sent with a code.
    TCPSend($Socket, @CRLF & "{EOF}")

    ; Display the successful message.
    ; MsgBox($MB_SYSTEMMODAL, "", "Server:" & @CRLF & "File sent.")
   
   Sleep($pause)
   $result = TCPSend($socket, "data")
   ConsoleWrite("it is the hell!")
   Sleep($pause)
   $result = TCPSend($socket, "data")
   ConsoleWrite("it is the hell!")
   Sleep($pause)
   $result = TCPSend($socket, "data")
   ConsoleWrite("it is the hell!")
   Sleep($pause)
   
EndFunc
; THIS IS THE CLIENT

#include <File.au3>

TCPStartup()
 
$socket = TCPConnect("127.0.0.1", 5555)

$yourfile = "COMPLETE PATH OF FILE TO RECEIVE"

Do
   $var1 = TCPRecv($socket, 100)
Until $var1 <> ""
ConsoleWrite("received" & @CRLF)
Do
   $var2 = TCPRecv($socket, 100)
Until $var2 <> ""
ConsoleWrite("received" & @CRLF)

      ; Assign a Local variable the handle of the file opened in binary overwrite mode.
    Local $hFile = FileOpen($yourfile, BitOR($FO_BINARY, $FO_OVERWRITE))

    ; Assign Locales Constant variables the number representing 4 KiB; the binary code for the end of the file and the length of the binary code.
    Local Const $i4KiB = 4096, $bEOF = Binary(@CRLF & "{EOF}"), $iEOFLen = BinaryLen($bEOF)

    ; Assign a Local variable the empty binary data which will contain the binary data of the file.
    Local $bData = Binary("")

    ; Assign a Local variable to store the length of the data received.
    Local $iDataLen = 0

    ; Assign a Local variable a boolean.
    Local $fEOFReached = False

    Do
         Do
            $bData = TCPRecv($Socket, $i4KiB, 1)
         Until $bData <> ""

        ; If an error occurred display the error code and return False.
        If @error Then
            $iError = @error
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Connection lost, Error code: " & $iError)
            Return False
        EndIf

        $iDataLen = BinaryLen($bData)

        ; If nothing is received, retry for the incoming data.
        If $iDataLen = 0 Then ContinueLoop

        ; If the end of the file is reached.
        If BinaryMid($bData, 1 + $iDataLen - $iEOFLen, $iEOFLen) = $bEOF Then
            ; Strip the EOF code from the file data.
            $bData = BinaryMid($bData, 1, $iDataLen - $iEOFLen)

            ; Set the EOFReached to True.
            $fEOFReached = True
        EndIf

        FileWrite($hFile, $bData)
    Until $fEOFReached

    ; Close the file handle.
    FileClose($hFile)

    ; Display the successful message.
    ; MsgBox($MB_SYSTEMMODAL, "", "Client:" & @CRLF & "File received.")

Do
   $var3 = TCPRecv($socket, 100)
Until $var3 <> ""
ConsoleWrite("received" & @CRLF)
Do
   $var4 = TCPRecv($socket, 100)
Until $var4 <> ""
ConsoleWrite("received" & @CRLF)
Do
   $var5 = TCPRecv($socket, 100)
Until $var5 <> ""
ConsoleWrite("received" & @CRLF)

thank you :bye:

ehi ehi ehi, what is your name?

Link to comment
Share on other sites

Actually, this example can't be used to be looped.

Either send to the server something to tell him you know the transfer is completed (then the server can send another file and the client is waiting for this new file) OR remake the script to change the EOF code to a header with the file length so you can directly send files one by one.

Try something, don't use any dirty Sleep it's not necessary. And if you're not able to do it (my first suggestion) then post your code and I will show you your mistakes.

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Either send to the server something to tell him you know the transfer is completed.

; THIS IS THE SERVER

#include <File.au3>

TCPStartup()

$listen = TCPListen("127.0.0.1", 5555)

While 1

   $socket = TCPAccept($listen)

   If $socket <> -1 Then
      example()
   EndIf

WEnd

Func example()
   
   $yourfile = "COMPLETE PATH FOR THE FILE"
   $pause = 0
   
   $result = TCPSend($socket, "data")
   ConsoleWrite("it is the hell!")
   Do
      $canContinue = TCPRecv($socket, 100)
   Until $canContinue <> ""
;~    Sleep($pause)
   $result = TCPSend($socket, "data")
   ConsoleWrite("it is the hell!")
   Do
      $canContinue = TCPRecv($socket, 100)
   Until $canContinue <> ""
;~    Sleep($pause)
   
   #region Br, FireFox
;~      ; Assign a Local variable the size of the file previously chosen.
    Local $iFileSize = FileGetSize($yourfile)

    ; Assign a Local variable the handle of the file opened in binary mode.
    Local $hFile = FileOpen($yourfile, $FO_BINARY)

    ; Assign a Local variable the offset of the file being read.
    Local $iOffset = 0

    ; Assign a Local variable the number representing 4 KiB.
    Local Const $i4KiB = 4096

    ; Note: The file is send by parts of 4 KiB.

    ; Send the binary data of the file to the server.
    Do
        ; Set the file position to the current offset.
        FileSetPos($hFile, $iOffset, $FILE_BEGIN)

        ; The file is read from the position set to 4 KiB and directly wrapped into the TCPSend function.
        TCPSend($Socket, FileRead($hFile, $i4KiB))

        ; If an error occurred display the error code and return False.
        If @error Then
            $iError = @error
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not send the data, Error code: " & $iError)

            ; Close the socket.
            TCPCloseSocket($Socket)
            Return False
        EndIf

        ; Increment the offset of 4 KiB to send the next 4 KiB data.
        $iOffset += $i4KiB
    Until $iOffset >= $iFileSize

    ; Close the file handle.
    FileClose($hFile)

   ; Tell the server the file is fully sent with a code.
    TCPSend($Socket, @CRLF & "{EOF}")

    ; Display the successful message.
    ; MsgBox($MB_SYSTEMMODAL, "", "Server:" & @CRLF & "File sent.")
    #endregion Br, FireFox
   
   Do
      $canContinue = TCPRecv($socket, 100)
   Until $canContinue <> ""
   
;~    Sleep($pause)
   $result = TCPSend($socket, "data")
   ConsoleWrite("it is the hell!")
   Do
      $canContinue = TCPRecv($socket, 100)
   Until $canContinue <> ""
;~    Sleep($pause)
   $result = TCPSend($socket, "data")
   ConsoleWrite("it is the hell!")
   Do
      $canContinue = TCPRecv($socket, 100)
   Until $canContinue <> ""
;~    Sleep($pause)
   $result = TCPSend($socket, "data")
   ConsoleWrite("it is the hell!")
;~    Sleep($pause)
   
EndFunc
; THIS IS THE CLIENT

#include <File.au3>

TCPStartup()
 
$socket = TCPConnect("127.0.0.1", 5555)

$yourfile = "COMPLETE PATH FOR THE FILE"

Do
   $var1 = TCPRecv($socket, 100)
Until $var1 <> ""
ConsoleWrite("var1 received" & @CRLF)
TCPSend($socket, "ok")
Do
   $var2 = TCPRecv($socket, 100)
Until $var2 <> ""
ConsoleWrite("var2 received" & @CRLF)
TCPSend($socket, "ok")

   #region Br, FireFox
      ; Assign a Local variable the handle of the file opened in binary overwrite mode.
    Local $hFile = FileOpen($yourfile, BitOR($FO_BINARY, $FO_OVERWRITE))

    ; Assign Locales Constant variables the number representing 4 KiB; the binary code for the end of the file and the length of the binary code.
    Local Const $i4KiB = 4096, $bEOF = Binary(@CRLF & "{EOF}"), $iEOFLen = BinaryLen($bEOF)

    ; Assign a Local variable the empty binary data which will contain the binary data of the file.
    Local $bData = Binary("")

    ; Assign a Local variable to store the length of the data received.
    Local $iDataLen = 0

    ; Assign a Local variable a boolean.
    Local $fEOFReached = False

    Do
         Do
            $bData = TCPRecv($Socket, $i4KiB, 1)
         Until $bData <> ""

        ; If an error occurred display the error code and return False.
        If @error Then
            $iError = @error
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Connection lost, Error code: " & $iError)
            Return False
        EndIf

        $iDataLen = BinaryLen($bData)

        ; If nothing is received, retry for the incoming data.
        If $iDataLen = 0 Then ContinueLoop

        ; If the end of the file is reached.
        If BinaryMid($bData, 1 + $iDataLen - $iEOFLen, $iEOFLen) = $bEOF Then
            ; Strip the EOF code from the file data.
            $bData = BinaryMid($bData, 1, $iDataLen - $iEOFLen)

            ; Set the EOFReached to True.
            $fEOFReached = True
        EndIf

        FileWrite($hFile, $bData)
    Until $fEOFReached

    ; Close the file handle.
    FileClose($hFile)

    ; Display the successful message.
    ; MsgBox($MB_SYSTEMMODAL, "", "Client:" & @CRLF & "File received.")
    #endregion Br, FireFox
    
    TCPSend($socket, "ok")

Do
   $var3 = TCPRecv($socket, 100)
Until $var3 <> ""
ConsoleWrite("var3 received" & @CRLF)
TCPSend($socket, "ok")
Do
   $var4 = TCPRecv($socket, 100)
Until $var4 <> ""
ConsoleWrite("var4 received" & @CRLF)
TCPSend($socket, "ok")
Do
   $var5 = TCPRecv($socket, 100)
Until $var5 <> ""
ConsoleWrite("var5 received" & @CRLF)

like the server, as such the client. when the client receive a new string, it confirms back it receives it: in this way the client waits for server and viceversa. if one of two block itself, both block themselves.

Remake the script to change the EOF code to a header with the file length so you can directly send files one by one.

I don't understand what you mean and how I can send files using EOF.

P.S.: is it necessary to close a socket when the program ends? is it necessary also shutdown tcp when programs end? because sometimes it runs good, sometimes not. I don't know if it is a bad code I wrote or it is just I miss to initialize another tcp session.

thank you ;)

ehi ehi ehi, what is your name?

Link to comment
Share on other sites

  • 2 months later...

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...