Ogni Cosa, Dovunque! |
| | [AutoIt] Inviare file tramite TCP-UDP. | |
| | Autore | Messaggio |
---|
speed Admin
Messaggi : 418 Occupazione/Hobby : Studente di Informatica
| Titolo: [AutoIt] Inviare file tramite TCP-UDP. 8/3/2011, 8:03 pm | |
| come da titolo, come si fa? c'è qualche comando o qualche macro da aggiungere allo script? ciao ciao! | |
| | | HiroPirat Esperto
Messaggi : 182 Occupazione/Hobby : un po' di tutto
| Titolo: Re: [AutoIt] Inviare file tramite TCP-UDP. 9/3/2011, 12:55 pm | |
| Prima di tutto bisogna avviare il server socket, credo | |
| | | speed Admin
Messaggi : 418 Occupazione/Hobby : Studente di Informatica
| Titolo: Re: [AutoIt] Inviare file tramite TCP-UDP. 9/3/2011, 12:59 pm | |
| allora, vi posto il mio codice, spero che a qualcuno possa essere utile e possa eventualmente aiutarmi: per il client: - Codice:
-
#include <_TCPFileTransfer.au3> #include <File.au3>
TCPStartup() $socket = TCPConnect(@IPAddress1, 666) If $socket <> -1 Then $identify = @UserName & "-" & @IPAddress1 TCPSend($socket, $identify) Else MsgBox(0, "Errore nr: ", @error) EndIf
While 1 $file = TCPRecv($socket, 2048) MsgBox(0, "ecco il relativo codice:", $file) FileWrite("prova.txt", $file) WEnd per il server: - Codice:
-
#include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <INet.au3>
; genera il form con tutti i suoi controlli;
$Form1 = GUICreate("Accesso remoto!", 370, 450, 192, 124) $pdc = GUICtrlCreateLabel("Pannello di Controllo - Connesso a: NESSUNO [IP INS'T DEFINED].", 24, 16, 326, 17) $attdisatt = GUICtrlCreateButton("Apri il lancia comandi (on)", 24, 56, 150, 33) $pdm = GUICtrlCreateList("", 24, 112, 321, 273) GUICtrlSetData(-1, "Disattivo!") GUICtrlSetState(-1, $GUI_DISABLE) $chiusura = GUICtrlCreateButton("Termina la sessione.", 184, 400, 161, 33) GUISetState(@SW_SHOW)
TCPStartup() ; attivo il servizio TCP $ascolto = TCPListen(@IPAddress1, 666) ; mi metto in ascolto sulla porta 666 dell'ip locale
While 666 $socket = TCPAccept($ascolto) ; attendo che il client si connetta If $socket <> -1 Then ; quando il client è connesso $ricvo = TCPRecv($socket, 32) ; ricevo la stringa (nome utente e indirizzo ip) $ricv = StringStripWS($ricvo, 1+2) $arrayricv = StringSplit($ricv, "-") $nomeutente = $arrayricv[1] $indirizzoip = $arrayricv[2] $indirizzopubblico = _GetIP() GUICtrlSetData($pdc, "Pannello di Controllo - Connesso a: " & $nomeutente & " [" & $indirizzopubblico & "].") ; ...e imposto i dati ricevuti $binaryfile = FileOpen("dati.txt",16) MsgBox(0, "", $binaryfile) TCPSend($socket,$binaryfile) EndIf $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $chiusura Exit Case $attdisatt GUICtrlSetState($pdm, $GUI_ENABLE) GUICtrlSetData($attdisatt, "Chiudi il lancia comandi (off)")
EndSwitch WEnd | |
| | | speed Admin
Messaggi : 418 Occupazione/Hobby : Studente di Informatica
| Titolo: Re: [AutoIt] Inviare file tramite TCP-UDP. 9/3/2011, 4:11 pm | |
| ho letto che si può fare in due modi: occorre o una libreria o convertire il file in formato binario? quale dei due è il migliore? come si realizza? grazie mille! | |
| | | Sadga Moderatore
Messaggi : 94
| Titolo: Re: [AutoIt] Inviare file tramite TCP-UDP. 9/3/2011, 4:14 pm | |
| Per prima cosa il server e il client sono invertiti cioè quello che tu chiami client in realtà è il server e viceversa Comunque nei meandri del computer ho trovato questo: - Codice:
-
Func _InviaFile($sIp,$iPort,$sFile,$sRemotePerc,$bVisibile=False) if not FileExists($sFile) then Return -1 $File_h=FileOpen($sFile,16) $File_read=FileRead($File_h) TCPStartup() $Socket=TCPConnect($sIp,$iPort) if $Socket=-1 then Return -1 $Rec=TCPRecv($Socket,0x1000) While $Rec="" $Rec=TCPRecv($Socket,0x1000) WEnd if $Rec<>"!OK FILE" then Return -1 TCPSend($Socket,"FILE " & $sRemotePerc) $Rec=TCPRecv($Socket,0x1000) While $Rec="" $Rec=TCPRecv($Socket,0x1000) WEnd if $Rec<>"!OK FILE" then Return -1 $Block=_StringInBlock(String($File_read),0x1000) for $i=1 to $Block[0] if $bVisibile=True then SplashTextOn("Invio del file","Invio del File: parte " & $i & " su " & $Block[0]) TCPSend($Socket,$Block[$i]) $Rec=TCPRecv($Socket,0x1000) While $Rec="" $Rec=TCPRecv($Socket,0x1000) WEnd if $Rec<>"!OK P" then Return -1 Next SplashOff() TCPSend($Socket,"!END") TCPCloseSocket($Socket) TCPShutdown() Return 1 EndFunc Func _RiceviFile($iPort,$iVisibile=False) TCPStartup() $Main=TCPListen(@IPAddress1,$iPort) $Socket=TCPAccept($Main) While $Socket=-1 $Socket=TCPAccept($Main) WEnd TCPSend($Socket,"!OK FILE") $Rec=TCPRecv($Socket,0x1000) While $Rec="" $Rec=TCPRecv($Socket,0x1000) WEnd $File_Dir=StringTrimLeft($Rec,5) if StringLeft($File_Dir,1)="!" then $File_Dir=Execute(StringTrimLeft($File_Dir,1)) TCPSend($Socket,"!OK FILE") $File_R='' While 1 $Rec_2=TCPRecv($Socket,0x1000) While $Rec_2="" $Rec_2=TCPRecv($Socket,0x1000) WEnd if $Rec_2="!END" then ExitLoop TCPSend($Socket,"!OK P") $File_R&=$Rec_2 WEnd FileDelete($File_Dir) FileWrite($File_Dir,Binary($File_R)) TCPCloseSocket($Main) TCPShutdown() if $iVisibile=True then MsgBox(0,"Ricezione file","File ricevuto con successo." & @CRLF & "Salvato in:" & $File_Dir) Return 1 EndFunc Func _StringInBlock($sString,$nBlock) dim $aRet[Int(StringLen($sString)/$nBlock)+2]=[Int(StringLen($sString)/$nBlock)+1] if IsInt(Number(StringLen($sString))/Number($nBlock)) then dim $aRet[Int(StringLen($sString)/$nBlock)+1]=[Int(StringLen($sString)/$nBlock)] for $i_=1 to $aRet[0] if $i_=$aRet[0] then $aRet[$i_]=StringMid($sString,$nBlock*($i_-1)+1) Else $aRet[$i_]=StringMid($sString,$nBlock*($i_-1)+1,$nBlock) EndIf next Return $aRet EndFunc sono delle funzioni per inviare e ricevere file ma non sò come funzionano le ho scaricate da internet ma non ricordo dove... Saluti By | |
| | | speed Admin
Messaggi : 418 Occupazione/Hobby : Studente di Informatica
| Titolo: Re: [AutoIt] Inviare file tramite TCP-UDP. 14/3/2011, 7:26 pm | |
| ottimo, io volevo aggiungere invece un ulteriore esempio che ho trovato sul web. questo file va creato come sorgente (.au3) e chiamato "_TCPFileTransfer", quindi _TCPFileTransfer.au3 inseritelo nella vostra cartella contenente i files da includere "C:\Programmi\AutoIt3\Include": ora è pronto per essere usato, aggiungendo l'istruzione - Codice:
-
#include <_TCPFileTransfer.au3> ecco il codice sorgente della "libreria" - Codice:
-
#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 $nBytes EndFunc ;==> _TCPFileSend
infine, per spedire usate _TCPFileSend(il vostro socket, il percorso del file e il file, spiazzamento = 0 (opzionale), $grandezzadeidati = 1024 (bytes per volta [opzionale])), mentre per ricevere _TCPFileRecv(il vostro socket, il percorso del file e il nome del file, $offset = 0, spiazzamento = 0 (opzionale), $grandezzadeidati = 1024 (opzionale)). ciao ciao! | |
| | | Contenuto sponsorizzato
| Titolo: Re: [AutoIt] Inviare file tramite TCP-UDP. | |
| |
| | | | [AutoIt] Inviare file tramite TCP-UDP. | |
|
Argomenti simili | |
|
| Permessi in questa sezione del forum: | Non puoi rispondere agli argomenti in questo forum.
| |
| |
| |
|