Direct connection, Trasferimento diretto di file

« Older   Newer »
  Share  
xdmisterx
CAT_IMG Posted on 26/4/2010, 12:47     +1   -1




Un piccolo software composta da un lato client e un lato server per il trasferimento di file tra due pc.
Testato in rete locale e internet.
Client:
SPOILER (click to view)
CODICE
#region
#AutoIt3Wrapper_Res_Description=Tool per trasferimento diretto di file
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Res_Language=1040
#AutoIt3Wrapper_Res_LegalCopyright=Powered by Misterx
#AutoIt3Wrapper_Icon=icona.ico
#endregion
#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.6.1
Author:         Misterx

Script Function:
       Client per trasferimento file.

#ce ----------------------------------------------------------------------------
#NoTrayIcon
#include <GuiConstants.au3>
#include <GuiEdit.au3>
#include <GuiIpAddress.au3>
TCPStartup()

Global $host = "xdmisterx.no-ip.biz", $porta = 8090, $title = "Direct connection - client", $socket = ""

$conn = GUICreate("Connessione",200,70)
$ip = _GUICtrlIpAddress_Create($conn,10,10,130,20)
$p = GUICtrlCreateInput(8090,150,10,40,20,0x2000)
$connect_b = GUICtrlCreateButton("Connetti",10,40,180,20)

GUISetState()

While 1
       Switch GUIGetMsg()
       Case $gui_event_close
               Exit
       Case $connect_b
               $host = _GUICtrlIpAddress_Get($ip)
               $porta = GUICtrlRead($p)
               If $host <> "" And $porta <> "" Then
                       GUICtrlSetState($connect_b,$gui_disable)
                       $socket = TCPConnect(TCPNameToIP($host),$porta)
                       If $socket = -1 Then
                               MsgBox(16,"Connessione","Impossibile connettersi all'host.",10)
                               GUICtrlSetState($connect_b,$gui_enable)
                       Else
                               ExitLoop
                       EndIf
               Else
                       MsgBox(16,"Connessione","Parametri errati",5)
               EndIf
       EndSwitch
WEnd

GUIDelete($conn)

GUICreate($title,300,200)
GUISetFont(8,"","","Terminal")
$log = GUICtrlCreateEdit("",0,0,300,150,BitOR(0x1000,0x00200000,0x0800))
$send = GUICtrlCreateButton("Invia file",5,155,143,20)
$exit = GUICtrlCreateButton("Esci",153,155,143,20)
$progress = GUICtrlCreateProgress(5,180,290,15)
GUICtrlSetLimit($progress,100,0)
_log("Connessione effettuata con successo.")

GUISetState()

While 1
       $recv = TCPRecv($socket,2048)
       If $recv <> "" Then
               If StringInStr($recv,"xEXITx",1) Then
                       _log("Connessione terminata.")
               EndIf
               
               If StringInStr($recv,"xFILENAMEx",1) Then
                       _log("Richiesta ricezione file...")
                       $file_r = StringReplace($recv,"xFILENAMEx","")
                       $save = FileSaveDialog($title,@DesktopDir,"All(*.*)",16,$file_r)
                       If $save <> "" Then
                               GUICtrlSetState($send,$gui_disable)
                               GUICtrlSetState($exit,$gui_disable)
                               _log("Trasferimento in corso...")
                               TCPSend($socket,"xOKx")
                               _TCPFileRecv($save,$socket)
                               _log("Trasferimento completato.")
                               GUICtrlSetData($progress,0)
                               GUICtrlSetState($send,$gui_enable)
                               GUICtrlSetState($exit,$gui_enable)
                       Else
                               TCPSend($socket,"xABORTx")
                               _log("Trasferimento annullato.")
                       EndIf
               EndIf
       EndIf
       
       Switch GUIGetMsg()
       Case $gui_event_close
               TCPSend($socket,"xEXITx")
               Exit
       Case $send
               $file = FileOpenDialog($title,@DesktopDir,"All(*.*)")
               If $file <> "" Then
                       $file_sp = StringSplit($file,"\",2)
                       _log("In attesa di conferma...")
                       TCPSend($socket,"xFILENAMEx"& $file_sp[UBound($file_sp)-1])
                       $conf = False
                       While 1
                               $recv = TCPRecv($socket,2048)
                               If $recv <> "" Then
                                       If StringInStr($recv,"xOKx",1) Then
                                               $conf = True
                                               ExitLoop
                                       EndIf
                                       
                                       If StringInStr($recv,"xABORTx",1) Then
                                               ExitLoop
                                       EndIf
                               EndIf
                       WEnd
                       If $conf = True Then
                               GUICtrlSetState($send,$gui_disable)
                               GUICtrlSetState($exit,$gui_disable)
                               _log("Inizio trasferimento di "& $file_sp[UBound($file_sp)-1])
                               _TCPFileSend($file,$socket)
                               GUICtrlSetData($progress,0)
                               _log("Trasferimento completato.")
                               GUICtrlSetState($send,$gui_enable)
                               GUICtrlSetState($exit,$gui_enable)
                       Else
                               _log("Trasferimento annullato.")
                       EndIf
               EndIf
       Case $exit
               TCPSend($socket,"xEXITx")
               Exit
       EndSwitch
WEnd

Func _log($data)
       _GUICtrlEdit_AppendText($log,$data & @CRLF)
EndFunc

Func _TCPFileSend($sFile,$_socket)
   Local $error=False, $num=1, $wait=5000, $ack=False
   $sFileOpen=FileOpen($sFile, 16 + 0)
   $sFileRead=FileRead($sFileOpen)
   FileClose($sFileOpen)
   $sFileSize=BinaryLen($sFileRead)
       $init=TimerInit()
       TCPSend($_socket,"xBufferOnx"& $sFileSize)
       While TimerDiff($init) < $wait
               $recv=TCPRecv($_socket,2048)
               If StringInStr($recv,"xContinuex",1) Then
                       $init=TimerInit()
                       $ack=True
                       ExitLoop
               EndIf
       WEnd
       If $ack=True Then
               If $sFileSize < 100000 Then
                       $TCPSend=TCPSend($_socket,$sFileRead)
                       $progress_bar=False
               Else
                       $buff=$sFileRead
                       Do
                               $perc=Round((BinaryLen($buff)/$sFileSize)*100,0)
                               $percfin=100-$perc
                               GUICtrlSetData($progress,$percfin)
                               $TCPSend=TCPSend($_socket,BinaryMid($buff,1,100000))
                               While 1
                                       $recv=TCPRecv($_socket,2048)
                                       If StringInStr($recv,"XStreamX",1) Then ExitLoop
                               WEnd
                               $buff=BinaryMid($buff,$TCPSend+1)
                       Until BinaryLen($buff)=0
               EndIf
               Sleep(100)
               For $i=1 To 5
                       TCPSend($_socket,"xEndTransx")
               Next
       Else
               $error=True
       EndIf
       If $error=True Then
               Return 0
       Else
               Return 1
       EndIf
EndFunc ;==> _TCPFileSend


Func _TCPFileRecv($rFile,$_socket)
       Local $error=False
       Local $buff, $no_recv_count=0, $wait=5000, $syn=False
       $init=TimerInit()
       While TimerDiff($init) < $wait
               $recv=TCPRecv($_socket,2024)
               If StringInStr($recv,"xBufferOnx",1) Then
                       $init=TimerInit()
                       $syn=True
                       TCPSend($_socket,"xContinuex")
                       ExitLoop
               EndIf
       WEnd
       If $syn=True Then
               $size=StringReplace($recv,"xBufferOnx","")
               $binary_recv=0
               While 1
                       $TCPRecv=TCPRecv($_socket,100000)
                       If StringLen($TCPRecv) Then
                               If StringInStr($TCPRecv,"xEndTransx",1) Then
                                       ExitLoop
                               EndIf
                               If $TCPRecv <> "" Then
                                       If $TCPRecv=="xEndTransx" Then ExitLoop
                                       $binary_recv+=BinaryLen($TCPRecv)
                                       $perc=Round(($binary_recv/$size)*100,0)
                                       GUICtrlSetData($progress,$perc)
                                       $str=BinaryToString($TCPRecv)
                                       $buff &=$str
                                       $no_recv_count=0
                                       TCPSend($_socket,"XStreamX")
                               EndIf
                       EndIf
               WEnd
               If $buff="" Then
                       $error=True
               Else
                       $buff=StringToBinary($buff)
                       $rFileOpen=FileOpen($rFile,16+2)
                       If $rFileOpen=-1 Then
                               $error=True
                       Else
                               $rFileWrite=FileWrite($rFileOpen,$buff)
                               If $rFileWrite=0 Then
                                       $error=True
                               EndIf
                               FileClose($rFileOpen)
                       EndIf
               EndIf
       Else
               $error=True
       EndIf
       If $error=True Then
               Return 0
       Else
               Return 1
       EndIf
EndFunc ;==> _TCPFileRecv


Server:
SPOILER (click to view)
CODICE
#region
#AutoIt3Wrapper_Res_Description=Tool per trasferimento diretto di file
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Res_Language=1040
#AutoIt3Wrapper_Res_LegalCopyright=Powered by Misterx
#AutoIt3Wrapper_Icon=icona.ico
#endregion
#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.6.1
Author:         Misterx

Script Function:
       Client per trasferimento file.

#ce ----------------------------------------------------------------------------
#NoTrayIcon
#include <GuiConstants.au3>
#include <GuiEdit.au3>
#include <GuiIpAddress.au3>
TCPStartup()

Global $host = "xdmisterx.no-ip.biz", $porta = 8090, $title = "Direct connection - client", $socket = ""

$conn = GUICreate("Connessione",200,70)
$ip = _GUICtrlIpAddress_Create($conn,10,10,130,20)
$p = GUICtrlCreateInput(8090,150,10,40,20,0x2000)
$connect_b = GUICtrlCreateButton("Connetti",10,40,180,20)

GUISetState()

While 1
       Switch GUIGetMsg()
       Case $gui_event_close
               Exit
       Case $connect_b
               $host = _GUICtrlIpAddress_Get($ip)
               $porta = GUICtrlRead($p)
               If $host <> "" And $porta <> "" Then
                       GUICtrlSetState($connect_b,$gui_disable)
                       $socket = TCPConnect(TCPNameToIP($host),$porta)
                       If $socket = -1 Then
                               MsgBox(16,"Connessione","Impossibile connettersi all'host.",10)
                               GUICtrlSetState($connect_b,$gui_enable)
                       Else
                               ExitLoop
                       EndIf
               Else
                       MsgBox(16,"Connessione","Parametri errati",5)
               EndIf
       EndSwitch
WEnd

GUIDelete($conn)

GUICreate($title,300,200)
GUISetFont(8,"","","Terminal")
$log = GUICtrlCreateEdit("",0,0,300,150,BitOR(0x1000,0x00200000,0x0800))
$send = GUICtrlCreateButton("Invia file",5,155,143,20)
$exit = GUICtrlCreateButton("Esci",153,155,143,20)
$progress = GUICtrlCreateProgress(5,180,290,15)
GUICtrlSetLimit($progress,100,0)
_log("Connessione effettuata con successo.")

GUISetState()

While 1
       $recv = TCPRecv($socket,2048)
       If $recv <> "" Then
               If StringInStr($recv,"xEXITx",1) Then
                       _log("Connessione terminata.")
               EndIf
               
               If StringInStr($recv,"xFILENAMEx",1) Then
                       _log("Richiesta ricezione file...")
                       $file_r = StringReplace($recv,"xFILENAMEx","")
                       $save = FileSaveDialog($title,@DesktopDir,"All(*.*)",16,$file_r)
                       If $save <> "" Then
                               GUICtrlSetState($send,$gui_disable)
                               GUICtrlSetState($exit,$gui_disable)
                               _log("Trasferimento in corso...")
                               TCPSend($socket,"xOKx")
                               _TCPFileRecv($save,$socket)
                               _log("Trasferimento completato.")
                               GUICtrlSetData($progress,0)
                               GUICtrlSetState($send,$gui_enable)
                               GUICtrlSetState($exit,$gui_enable)
                       Else
                               TCPSend($socket,"xABORTx")
                               _log("Trasferimento annullato.")
                       EndIf
               EndIf
       EndIf
       
       Switch GUIGetMsg()
       Case $gui_event_close
               TCPSend($socket,"xEXITx")
               Exit
       Case $send
               $file = FileOpenDialog($title,@DesktopDir,"All(*.*)")
               If $file <> "" Then
                       $file_sp = StringSplit($file,"\",2)
                       _log("In attesa di conferma...")
                       TCPSend($socket,"xFILENAMEx"& $file_sp[UBound($file_sp)-1])
                       $conf = False
                       While 1
                               $recv = TCPRecv($socket,2048)
                               If $recv <> "" Then
                                       If StringInStr($recv,"xOKx",1) Then
                                               $conf = True
                                               ExitLoop
                                       EndIf
                                       
                                       If StringInStr($recv,"xABORTx",1) Then
                                               ExitLoop
                                       EndIf
                               EndIf
                       WEnd
                       If $conf = True Then
                               GUICtrlSetState($send,$gui_disable)
                               GUICtrlSetState($exit,$gui_disable)
                               _log("Inizio trasferimento di "& $file_sp[UBound($file_sp)-1])
                               _TCPFileSend($file,$socket)
                               GUICtrlSetData($progress,0)
                               _log("Trasferimento completato.")
                               GUICtrlSetState($send,$gui_enable)
                               GUICtrlSetState($exit,$gui_enable)
                       Else
                               _log("Trasferimento annullato.")
                       EndIf
               EndIf
       Case $exit
               TCPSend($socket,"xEXITx")
               Exit
       EndSwitch
WEnd

Func _log($data)
       _GUICtrlEdit_AppendText($log,$data & @CRLF)
EndFunc

Func _TCPFileSend($sFile,$_socket)
   Local $error=False, $num=1, $wait=5000, $ack=False
   $sFileOpen=FileOpen($sFile, 16 + 0)
   $sFileRead=FileRead($sFileOpen)
   FileClose($sFileOpen)
   $sFileSize=BinaryLen($sFileRead)
       $init=TimerInit()
       TCPSend($_socket,"xBufferOnx"& $sFileSize)
       While TimerDiff($init) < $wait
               $recv=TCPRecv($_socket,2048)
               If StringInStr($recv,"xContinuex",1) Then
                       $init=TimerInit()
                       $ack=True
                       ExitLoop
               EndIf
       WEnd
       If $ack=True Then
               If $sFileSize < 100000 Then
                       $TCPSend=TCPSend($_socket,$sFileRead)
                       $progress_bar=False
               Else
                       $buff=$sFileRead
                       Do
                               $perc=Round((BinaryLen($buff)/$sFileSize)*100,0)
                               $percfin=100-$perc
                               GUICtrlSetData($progress,$percfin)
                               $TCPSend=TCPSend($_socket,BinaryMid($buff,1,100000))
                               While 1
                                       $recv=TCPRecv($_socket,2048)
                                       If StringInStr($recv,"XStreamX",1) Then ExitLoop
                               WEnd
                               $buff=BinaryMid($buff,$TCPSend+1)
                       Until BinaryLen($buff)=0
               EndIf
               Sleep(100)
               For $i=1 To 5
                       TCPSend($_socket,"xEndTransx")
               Next
       Else
               $error=True
       EndIf
       If $error=True Then
               Return 0
       Else
               Return 1
       EndIf
EndFunc ;==> _TCPFileSend


Func _TCPFileRecv($rFile,$_socket)
       Local $error=False
       Local $buff, $no_recv_count=0, $wait=5000, $syn=False
       $init=TimerInit()
       While TimerDiff($init) < $wait
               $recv=TCPRecv($_socket,2024)
               If StringInStr($recv,"xBufferOnx",1) Then
                       $init=TimerInit()
                       $syn=True
                       TCPSend($_socket,"xContinuex")
                       ExitLoop
               EndIf
       WEnd
       If $syn=True Then
               $size=StringReplace($recv,"xBufferOnx","")
               $binary_recv=0
               While 1
                       $TCPRecv=TCPRecv($_socket,100000)
                       If StringLen($TCPRecv) Then
                               If StringInStr($TCPRecv,"xEndTransx",1) Then
                                       ExitLoop
                               EndIf
                               If $TCPRecv <> "" Then
                                       If $TCPRecv=="xEndTransx" Then ExitLoop
                                       $binary_recv+=BinaryLen($TCPRecv)
                                       $perc=Round(($binary_recv/$size)*100,0)
                                       GUICtrlSetData($progress,$perc)
                                       $str=BinaryToString($TCPRecv)
                                       $buff &=$str
                                       $no_recv_count=0
                                       TCPSend($_socket,"XStreamX")
                               EndIf
                       EndIf
               WEnd
               If $buff="" Then
                       $error=True
               Else
                       $buff=StringToBinary($buff)
                       $rFileOpen=FileOpen($rFile,16+2)
                       If $rFileOpen=-1 Then
                               $error=True
                       Else
                               $rFileWrite=FileWrite($rFileOpen,$buff)
                               If $rFileWrite=0 Then
                                       $error=True
                               EndIf
                               FileClose($rFileOpen)
                       EndIf
               EndIf
       Else
               $error=True
       EndIf
       If $error=True Then
               Return 0
       Else
               Return 1
       EndIf
EndFunc ;==> _TCPFileRecv


Downlaod source e eseguibili: www.mediafire.com/?nxmztgmtnnl
Per qualsiasi problema chiedete pure :)
Ciao :)
 
Top
Kurt_Black_hat 2.0
CAT_IMG Posted on 26/4/2010, 12:48     +1   -1




Ottimo sistema :P
 
Top
Lahace
CAT_IMG Posted on 26/4/2010, 12:48     +1   -1




testato anche con router e boiate simili? xD
se è cosi potrei aggiungerlo al mio terminale con il tuo permesso ;)
 
Top
Kurt_Black_hat 2.0
CAT_IMG Posted on 26/4/2010, 12:51     +1   -1




CITAZIONE (Lahace @ 26/4/2010, 13:48)
se è cosi potrei aggiungerlo al mio terminale con il tuo permesso ;)

FAIL -.-
Non sei mica l'unico che ha contribuito al progetto eh.
 
Top
xdmisterx
CAT_IMG Posted on 26/4/2010, 12:51     +1   -1




Sisi con il router funge. L'unica cosa è che bisogna fowardare come al solito le porte.
 
Top
Lahace
CAT_IMG Posted on 26/4/2010, 14:28     +1   -1




CITAZIONE (Kurt_Black_hat 2.0 @ 26/4/2010, 13:51)
CITAZIONE (Lahace @ 26/4/2010, 13:48)
se è cosi potrei aggiungerlo al mio terminale con il tuo permesso ;)

FAIL -.-
Non sei mica l'unico che ha contribuito al progetto eh.

giusto scusa kurt ç.ç

al nostro terminale ;)
 
Top
Kurt_Black_hat 2.0
CAT_IMG Posted on 26/4/2010, 14:29     +1   -1




CITAZIONE (Lahace @ 26/4/2010, 15:28)
CITAZIONE (Kurt_Black_hat 2.0 @ 26/4/2010, 13:51)
FAIL -.-
Non sei mica l'unico che ha contribuito al progetto eh.

giusto scusa kurt ç.ç

al nostro terminale ;)

asd xD
 
Top
MHack
CAT_IMG Posted on 26/4/2010, 14:38     +1   -1




Wow, molto utile, soprattutto a scuola ;)
 
Top
Gianlu-thebest
CAT_IMG Posted on 27/4/2010, 07:03     +1   -1




g00d work :)
 
Top
8 replies since 26/4/2010, 12:47   108 views
  Share