Jump to content

FileRecv algoritm ProgressOn


Recommended Posts

Hello

I function below to receive a file sent by the client, how to implement function ProgressOn can see the percentage of receipt of the file.

I tried to find an algorithm for it, but ..unsuccessful

Thanks !

Func _TCPFileRecv($sock, $rFile, $offset = 0, $DataSize = 1024)
Local $i, $x, $lpszBuff, $nBytes, $rBuff, $hFile
$nBytes = 0
$lpszBuff = DllStructCreate("byte["&$DataSize&"]")
$hFile = _WinAPI_CreateFile($rFile,3,4,4)
If $hFile = 0 Then
_WinAPI_CloseHandle($hFile)
SetError(1)
SetExtended($nBytes)
Return 0
EndIf
While 1
If _WinAPI_SetFilePointer($hFile, $offset, 0) = -1 Then
_WinAPI_CloseHandle($hFile)
SetError(2)
SetExtended($nBytes)
Return 0
EndIf
$rBuff = ""
$rBuff = TCPRecv($sock, $DataSize)
If $rBuff <> "" Then
If @error = -1 Then
_WinAPI_CloseHandle($hFile)
SetError(3)
SetExtended($nBytes)
Return 0
EndIf
If $rBuff = @CRLF&@CRLF Then
ExitLoop
Else
DllStructSetData($lpszBuff, 1, $rBuff)
$x = StringLen(BinaryToString($rBuff))
If _WinAPI_WriteFile($hFile, DllStructGetPtr($lpszBuff), $x, $i) = False Then
_WinAPI_CloseHandle($hFile)
SetError(4)
SetExtended($nBytes)
Return 0
Else
$nBytes += $i
$offset += $i
EndIf
EndIf
EndIf
WEnd
_WinAPI_CloseHandle($hFile)
SetExtended($nBytes)
Return $nBytes
EndFunc
Link to comment
Share on other sites

Of course it is.

Sometimes it's not easy as A+B, however here it's not far from that.

PS: I'm asking a little effort from you, try something, post it and I will answer.

Edited by FireFox
Link to comment
Share on other sites

Why are you using WinAPI_File functions instead of a simple FileOpen/FileWrite? Also this code:

SetError(2)
SetExtended($nBytes)
Return 0

Can be replaced by one line:

Return SetError(2, $nBytes, 0)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Thank you all for the reply!

I attached the script completely, maybe it can help me ...

All I can see is just a ProgressOn, send the file ...

#include <WinAPI.au3>
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.6.0
 Author:         DELmE

 Function:
    _TCPFileRecv

Description:
    Receives a file being sent over a TCP connection

Parameters:
sock The TCP socket to be used
    rFile       The name of the file to save to
offset = 0 The offset to start writing data to the file at (to be used if the transfer is somehow interrupted) default value is 0 (no offset)
$DataSize = 1024 The number of bytes of data to be received per packet, default value is 1024

Return value:
    Success     _TCPFileRecv returns the number of bytes written to the file
    Failure     _TCPFileRecv returns 0 if the function failed and sets @error to the following:
1 = Error opening the file
2 = Failed to set offset
3 = Failed to receive data on socket (invalid socket)
4 = Unable to write data to the file
Also sets @extended to the number of bytes of data that was successfully written to the file

#ce ----------------------------------------------------------------------------
Func _TCPFileRecv($sock, $rFile, $offset = 0, $DataSize = 1024)
Local $i, $x, $lpszBuff, $nBytes, $rBuff, $hFile
$nBytes = 0
$lpszBuff = DllStructCreate("byte["&$DataSize&"]")
$hFile = _WinAPI_CreateFile($rFile,3,4,4)
If $hFile = 0 Then
_WinAPI_CloseHandle($hFile)
SetError(1)
SetExtended($nBytes)
Return 0
EndIf
While 1
If _WinAPI_SetFilePointer($hFile, $offset, 0) = -1 Then
_WinAPI_CloseHandle($hFile)
SetError(2)
SetExtended($nBytes)
Return 0
EndIf
$rBuff = ""
$rBuff = TCPRecv($sock, $DataSize)
If $rBuff <> "" Then
If @error = -1 Then
_WinAPI_CloseHandle($hFile)
SetError(3)
SetExtended($nBytes)
Return 0
EndIf
If $rBuff = @CRLF&@CRLF Then
ExitLoop
Else
DllStructSetData($lpszBuff, 1, $rBuff)
$x = StringLen(BinaryToString($rBuff))
If _WinAPI_WriteFile($hFile, DllStructGetPtr($lpszBuff), $x, $i) = False Then
_WinAPI_CloseHandle($hFile)
SetError(4)
SetExtended($nBytes)
Return 0
Else
$nBytes += $i
$offset += $i
EndIf
EndIf
EndIf
WEnd
_WinAPI_CloseHandle($hFile)
SetExtended($nBytes)
Return $nBytes
EndFunc ;==> _TCPFileRecv

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.6.0
 Author:         DELmE

 Function:
    _TCPFileSend

Description:
    Sends a file over a TCP connection

Parameters:
sock The tcp socket to be used
    sFile       The file to be sent
offset = 0 The offset to start reading data from the file at (to be used if the transfer is somehow interrupted) default value is 0 (no offset)
$DataSize = 1024 The number of bytes of data to be sent per packet, default value is 1024

Return value:
    Success     _TCPFileSend returns the number of bytes sent
    Failure     _TCPFileSend returns 0 if an error occurred and sets @error to the following:
1 = The file does not exist
2 = Failed to set offset
3 = Failed to read data from file
4 = Failed to send data through socket
Also sets @extended to the number of bytes of data that was successfully sent

#ce ----------------------------------------------------------------------------
Func _TCPFileSend($sock, $sFile, $offset = 0, $DataSize = 1024)
Local $i, $lpszBuff, $nBytes, $sBuff, $hFile, $nFileSize
$nFileSize = FileGetSize($sFile)
$nFileSize -= $offset
If $nFileSize < $DataSize Then $DataSize = $nFileSize
$nBytes = 0
$lpszBuff = DllStructCreate("byte["&$DataSize&"]")
$hFile = _WinAPI_CreateFile($sFile,2,2,2)
If $hFile = 0 Then
_WinAPI_CloseHandle($hFile)
SetError(1)
SetExtended($nBytes)
Return 0
EndIf
While $nFileSize > 0
If _WinAPI_SetFilePointer($hFile, $offset, 0) = -1 Then
_WinAPI_CloseHandle($hFile)
SetError(2)
SetExtended($nBytes)
Return 0
EndIf
DllStructSetData($lpszBuff,1,"")
If _WinAPI_ReadFile($hFile, DllStructGetPtr($lpszBuff), $DataSize, $i) = False Then
_WinAPI_CloseHandle($hFile)
SetError(3)
Return 0
Else
$sBuff = DllStructGetData($lpszBuff, 1)
$i = TCPSend($sock, $sBuff)
If $i = 0 Then
_WinAPI_CloseHandle($hFile)
SetError(4)
SetExtended($nBytes)
Return 0
Else
$nBytes += $i
$offset += $i
$nFileSize -= $i
EndIf
EndIf
WEnd
_WinAPI_CloseHandle($hFile)
TCPSend($sock, @CRLF&@CRLF)
SetExtended($nBytes)
Return $nBytesEndFunc ;==> _TCPFileSend
Link to comment
Share on other sites

Do you get the file size sent to you when you request the file? If you don't know the total file size before it's been transferred, there's no way to track it's progress, if you do get the size it is easier.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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