Jump to content

file transfer exemple


Recommended Posts

that use this script to work and at home, and I not have an FTP server that can help me...

problem is FileWrite($open2, $comanda[4])

how to write the file only when strictly after we received code binary totally o:)

Edited by incepator
Link to comment
Share on other sites

I would do the following:

  • Take the example scripts provided by AutoIt for TCPSend and TCPRecv and store them in your script directory
  • Open both scripts and compile them
  • Run TCPRecv.exe then run TCPSend.exe
  • Enter some data and see if the data gets sent
  • If yes, replace the GUI stuff in both scripts with FileRead and FileWrite

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I tried you said, but the problem is no longer stops sent packets.

;==============================================
;==============================================
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
TCPStartup()

Local $ConnectedSocket, $szData
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891

$ConnectedSocket = -1
$ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)

If @error Then
MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
Else
$open = FileOpen("exemple.exe", 16)
$szData = FileRead($open)
FileClose($open)
While 1
If @error Or $szData = "" Then ExitLoop
TCPSend($ConnectedSocket, $szData)
If @error Then ExitLoop
WEnd
EndIf
EndFunc

#include <GUIConstantsEx.au3>

;==============================================
;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891
Local $MainSocket, $edit, $ConnectedSocket, $szIP_Accepted
Local $msg, $recv
TCPStartup()

$MainSocket = TCPListen($szIPADDRESS, $nPORT)
If $MainSocket = -1 Then Exit

GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200, 100, 100)
$edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
GUISetState()

$ConnectedSocket = -1
Do
$ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1

$szIP_Accepted = SocketToIP($ConnectedSocket)

While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop

$recv = TCPRecv($ConnectedSocket, 2048)
If @error Then ExitLoop
$recv = BinaryToString($recv, 4)

If $recv <> "" Then

$open2 = FileOpen(@ScriptDir & "\Folder\exemple.exe", 1)
$write = FileWrite($open2, $recv)
FileClose($open2)
EndIf
WEnd

If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

TCPShutdown()
EndFunc


Func SocketToIP($SHOCKET)
Local $sockaddr, $aRet

$sockaddr = DllStructCreate("short;ushort;uint;char[8]")

$aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
"ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
If Not @error And $aRet[0] = 0 Then
$aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
If Not @error Then $aRet = $aRet[0]
Else
$aRet = 0
EndIf

$sockaddr = 0

Return $aRet
EndFunc
Link to comment
Share on other sites

You have to remove the While in the client script. Because you want to send the file only once, right?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Why not? TCP transmits Bits, so shouldn't be a problem.

Why can't you send an executable? Do you get an error message?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

it seems that indeed is a problem in the script, I tried to send a picture and I received only 70-80% of it, I think this is why I can not send executable.

; Sender

TCPStartup()
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891

$ConnectedSocket = -1
$ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)

_FileSend("file1.jpg", $ConnectedSocket)

Func _FileSend($sFile, $iMainSocket)
Local $sBuff, $iFileOp, $sRecv

$iFileOp = FileOpen($sFile, 16)
If @error Then Return 0
$sBuff = Binary(StringTrimLeft($sFile, StringInStr($sFile, "\", -1, -1)) & ",") & FileRead($iFileOp)
FileClose($iFileOp)

While BinaryLen($sBuff)
$iSendReturn = TCPSend($iMainSocket, $sBuff)
If @error Then Return 0

$sBuff = BinaryMid($sBuff, $iSendReturn + 1, BinaryLen($sBuff) - $iSendReturn)
WEnd

Return 1
EndFunc ;==>_FileSend

; Receive

#include <GUIConstantsEx.au3>

Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891

TCPStartup()
$MainSocket = TCPListen($szIPADDRESS, $nPORT)
If $MainSocket = -1 Then Exit
$ConnectedSocket = -1

Do
$ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1

_FileReceive("file2.jpg", $ConnectedSocket)

Func _FileReceive($sFileName, $iAccSocket)
Local $sBuff, $sRecv = "", $i = 0, $iFirstWhile = True

If @error Then Return 0

$sBuff = Binary($sBuff)

While $sRecv = ""
$sRecv = TCPRecv($iAccSocket, 2048, 1)
$sRecv = BinaryToString($sRecv)
WEnd

While $sRecv <> ""
If StringInStr($sRecv, ',') And $iFirstWhile Then
$sTmp = StringLeft($sRecv, StringInStr($sRecv, ",") - 1)
$sRecv = StringTrimLeft($sRecv, StringLen($sTmp) + 1)
If StringLen($sFileName) < 1 Then $sFileName = $sTmp
$iFirstWhile = False
EndIf
$sBuff &= $sRecv
$sRecv = BinaryToString(TCPRecv($iAccSocket, 2048, 1))
If @error Then ExitLoop
WEnd

$iFileOp = FileOpen($sFileName, 16 + 2)
If @error Then Return 0
FileWrite($iFileOp, $sBuff)
If @error Then Return 0
FileClose($iFileOp)

Return 1
EndFunc   ;==>_FileReceive
Link to comment
Share on other sites

Another question before I dive into your code:

Do you transfer the files in a company network or over the internet?

Company: Could you use a file share?

Internet: Could you use dropbox or something similar?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Do you just want to use TCP or do you have to use TCP? A file share isn't an option?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

If I had to write this program I would connect to a network share that everyone can access and save the screenshots and the files there in a subdirectory named like the PC name.

I like to use features and tools that are already available and reliable to do my work.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

×
×
  • Create New...