Nabend, oder eher Morgen?
Hm naja ich habe mal nach langem wieder mal was produktives mit Autoit gescriptet
Zwei Funktionen mit denen man Dateien per TCP-Protokol durch die Gegend schicken kann und das mit beachtlichem Speed
habe es bei mir im loaken Netzwerk mit bis zu 1200 kb/s geschaft .
So zu den Funktionen:
Spoiler anzeigen
-> Binäre Übertragungsart, somit kann man alle Dateien ohne Verlust verschicken.
-> Progressbar mit Fortschritt, Dateigröße und Übertragungsrate. <- Optional
-> Timeoutfunktion, die das Script unterbricht, wenn keine Verbindung möglich ist, <- Optional + Timeout bestimmbar
-> Benachrichtung, wenn die Datei erfolgreich übertragen wurde <- Optional
-> Einstellung der Ruhepause zwischen TCPSend/Recv <- So kann man das Verhältnis zwischen CPU - Auslastung <=> Übertragunsrate optimieren(Optional)
-> Einstellung der Größe in KB die jeweils gesendet wird <- So kann man das Verhältnis zwischen CPU - Auslastung <=> Übertragunsrate optimieren (optionaö)
So das war es erstmal
Beispiel:
Spoiler anzeigen
Senden:
#include <_TCPFileTransfer.au3>
TCPStartup()
_TCPSendFile(@WindowsDir&"\explorer.exe",@IPAddress1,34567,10)
TCPShutdown()
Empfangen:
[autoit]#include <_TCPFileTransfer.au3>
TCPStartup()
_TCPREcvFile("",@IPAddress1,34567,10)
TCPShutdown()
QuellCode:
Spoiler anzeigen
#include-once
;=============================================== _TCPSendFile ==============================================
;
; Author: DJ Baselord
; Version: 1.0
; Requirements: Autoit v. 3.2.4.0
; Parameters: $sFile: File
; $sIP: IP to conenct to
; $iPort: TCP - Port to connect to
; $iTimeout: Time to wait for a connection
; $bPopup: 1 to popup Progress, 0 to hide
; $bShowMessageOnSuccess: 1 to show Msg on success, 0 to hide
; $iSleep: Time to sleep beetween each TCPSend, very important for the
; relation beetween CPU - ussage and transmition rate
; $iPartsize: Size in KB needed by TCP - Send also important for the
; relation beetween CPU - ussage and transmition rate
;
; Returnvalue: 1 on succes
; 0 on failure and @error is set as below:
; 1: file doesn't exist
; 2: error occured while file reading
; 3: if timer is expired
;
;===========================================================================================================
Func _TCPSendFile($sFile,$sIP,$iPort,$iTimeout=0,$bPopup=1,$bShowMessageOnSuccess=1,$iSleep=10,$iPartsize=128)
If $bPopup Then ProgressOn("_TCPSendFile","Verbindungsaufbau","Dateieinlesen",-1,-1,16)
If Not FileExists($sFile) Then Return SetError(1,0,0)
$hFile = FileOpen($sFile,16)
$hFileRead = FileRead($hFile)
FileClose($hFile)
$iFilesize = BinaryLen($hFileRead)
If Not BinaryLen($hFileRead) Then Return SetError(2,0,0)
If $bPopup And Not $iTimeout Then ProgressSet(0,"Warte auf Verbindung")
If $bPopup And $iTimeout Then ProgressSet(0,"Warte noch "&$iTimeout&" Sekunden")
If $iTimeout Then $hTimer = TimerInit()
Do
$iConnectedSocket = TCPConnect($sIP,$iPort)
Sleep(100)
If $bPopup And $iTimeout Then
$iCurrenttime = Int(TimerDiff($hTimer)/1000)
ProgressSet((TimerDiff($hTimer)/1000)/$iTimeout*100,"Warte noch "&$iTimeout-$iCurrenttime&" Sekunden")
If $iCurrenttime >= $iTimeout Then
ProgressSet(100,"Zeit abgelaufen")
Sleep(3000)
Return SetError(3,0,ProgressOff())
EndIf
EndIf
Until $iConnectedSocket > -1
$sFile = StringTrimLeft($sFile,StringInStr($sFile,"\",-1,-1))
Do
$TCPSend = TCPSend($iConnectedSocket,$sFile&Chr(3)&$iFilesize)
Sleep(100)
Until $TCPSend > 0
If $bPopup Then
$hTimer = TimerInit()
$iFilesizeMB = Round($iFilesize/1024^2,2)
ProgressSet(0,"Sende 0.0 von "&$iFilesizeMB&" MB @ 0.0 kb/s","Datei senden...")
EndIf
Local $iNewTime, $iOldTime
Do
$TCPSend = TCPSend($iConnectedSocket,BinaryMid($hFileRead,1,$iPartsize*1024))
$hFileRead = BinaryMid($hFileRead,1+$TCPSend)
If $bPopup Then
$iNewTime = TimerDiff($hTimer)
$iTime = $iNewTime - $iOldTime
$iRate = ($TCPSend/$iTime)/1.024
$iSent = ($iFilesize-BinaryLen($hFileRead))/1024^2
ProgressSet($iSent/$iFilesizeMB*100,"Sende "&Round($iSent,2)&" von "&$iFilesizeMB&" @ "&Round($iRate,2)&" kb/s")
$iOldTime = $iNewTime
EndIf
Sleep($iSleep)
Until BinaryLen($hFileRead) <= 0
TCPCloseSocket($iConnectedSocket)
If $bShowMessageOnSuccess Then MsgBox(64+262144,"_TCPSendFile","Datei erfolgreich übertragen")
ProgressOff()
Return 1
EndFunc
;=============================================== _TCPRecvFile ==============================================
;
; Author: DJ Baselord
; Version: 1.0
; Requirements: Autoit v. 3.2.4.0
; Parameters: $sFile: String to save file to if "", orignial filename will be used
; $sIP: IP to wait for connection
; $iPort: TCP - Port to open up listening port
; $iTimeout: Time to wait for a connection
; $bPopup: 1 to popup Progress, 0 to hide
; $bShowMessageOnSuccess: 1 to show Msg on success, 0 to hide
; $iSleep: Time to sleep beetween each TCPRecv, very important for the
; relation beetween CPU - ussage and transmition rate
; $iPartsize: Size in KB needed by TCPRecv also important for the
; relation beetween CPU - ussage and transmition rate
;
; Returnvalue: 1 on succes
; 0 on failure and @error is set as below:
; 1: if timer is expired
;
;===========================================================================================================
Func _TCPRecvFile($sFile,$sIP,$iPort,$iTimeout=0,$bPopup=1,$bShowMessageOnSuccess=1,$iSleep=10,$iPartsize=128)
If $bPopup Then ProgressOn("_TCPRecvFile","Verbindungsaufbau","Warten",-1,-1,16)
$ListeningSocket = TCPListen($sIP,$iPort)
If $bPopup And Not $iTimeout Then ProgressSet(0,"Warte auf Verbindung")
If $bPopup And $iTimeout Then ProgressSet(0,"Warte noch "&$iTimeout&" Sekunden")
If $iTimeout Then $hTimer = TimerInit()
Do
$iConnectedSocket = TCPAccept($ListeningSocket)
Sleep(100)
If $bPopup And $iTimeout Then
$iCurrenttime = Int(TimerDiff($hTimer)/1000)
ProgressSet((TimerDiff($hTimer)/1000)/$iTimeout*100,"Warte noch "&$iTimeout-$iCurrenttime&" Sekunden")
If $iCurrenttime >= $iTimeout Then
ProgressSet(100,"Zeit abgelaufen")
Sleep(3000)
Return SetError(1,0,ProgressOff())
EndIf
EndIf
Until $iConnectedSocket > -1
Do
$Recv = TCPRecv($iConnectedSocket,2048)
Sleep(100)
Until $Recv <> ""
$Recv = StringSplit($Recv,Chr(3))
$iFilesize = $Recv[2]
$SFilename = $Recv[1]
$sBuffer = Binary("")
If $bPopup Then
$hTimer = TimerInit()
$iFilesizeMB = Round($iFilesize/1024^2,2)
ProgressSet(0,"Sende 0.0 von "&$iFilesizeMB&" MB @ 0.0 kb/s","Datei senden...")
EndIf
Local $iNewTime, $iOldTime
Do
$Recv = Binary(TCPRecv($iConnectedSocket,$iPartsize*1024))
$sBuffer &= $Recv
Sleep($iSleep)
If $bPopup Then
$iNewTime = TimerDiff($hTimer)
$iTime = $iNewTime - $iOldTime
$iRate = (BinaryLen($Recv)/$iTime)/1.024
$iSent = (BinaryLen($sBuffer))/1024^2
ProgressSet($iSent/$iFilesizeMB*100,"Empfange "&Round($iSent,2)&" von "&$iFilesizeMB&" @ "&Round($iRate,2)&" kb/s")
$iOldTime = $iNewTime
EndIf
Until BinaryLen($sBuffer) >= $iFilesize
If $sFile = "" Then $sFile = $SFilename
$hFile = FileOpen($sFile,18)
FileWrite($hFile,$sBuffer)
FileClose($hFile)
$sBuffer = ""
TCPCloseSocket($ListeningSocket)
TCPCloseSocket($iConnectedSocket)
If $bShowMessageOnSuccess Then MsgBox(64+262144,"_TCPRecvFile","Datei erfolgreich übertragen")
ProgressOff()
Return 1
EndFunc
So und das ganze noch mal als ZIP im Anhang =)
Würde mich über Feedback und Verbesserungsvorschläge freuen
mfg DJ