View Full Version : FIle Transfer VB
ciao raga ho trovato su internet un sorgente per fare un file Transfer in Vb(io nn conosco il vb ) cmq l'ho migliorato nell'aspetto e ho aggiunto altre cose..ma mi servirebbe sapere cm posso inserire un contatore della velocità di trasferimento e il tempo restante...grazie
Un esempio su come fare per vedere quanti bytes sono stati inviati te lo può dare il sorgente presente a questo link (evento sendProgress, in particolare).
http://www.ostrosoft.com/vb/projects/transfer.asp
Per quanto riguarda il tempo potresti usare l'istruzione Time per ottenere l'ora di sistema...puoi salvarti in una variabile l'ora in cui è iniziato il download del file ed in base al n° di bytes inviati e l'ora attuale puoi calcolarti la velocità media e una stima del tempo che impiegherà a scaricare il resto...
Aloha!
si avevo gia visto quel sorgente...
cmq potresti darmi una mano in +? nn conosco il vb...magari se hai tempo ad aiutarmi ..(se mi aggiungi qualkosa al sorgente)così poi leggendolo capisco..
Per aiutarti non c'è problema, ma se fosse possibile preferirei vedere il sorgente che usi tu e fare le modifiche su quello ;)
Aloha!
certo..te lo scrivo qui se è possibile..allora
SERVER
----------------------
Option Explicit
Dim lPos As Long
Dim bOK As Boolean
Dim fname As String
Private Sub cmdRun_Click()
If cmdRun.Caption = "Run" Then
cmdRun.Caption = "Stop"
wsTCP(0).LocalPort = 1111
wsTCP(0).Listen
Else
wsTCP(0).Close
cmdRun.Caption = "Run"
End If
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive & "\"
End Sub
Private Sub wsTCP_Close(Index As Integer)
Close #1
Unload wsTCP(1)
bOK = False
End Sub
Private Sub wsTCP_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Load wsTCP(1)
If wsTCP(0).RemoteHostIP = txt_secure Then
wsTCP(1).Accept requestID
lblip = wsTCP(0).RemoteHostIP
Else
wsTCP(1).Close
lbl_lamer = wsTCP(0).RemoteHostIP
End If
End Sub
Private Sub wsTCP_DataArrival(Index As Integer, ByVal bytesTotal As Long)
If Not bOK Then
wsTCP(1).GetData fname
If InStr(fname, vbCrLf) <> 0 Then fname = Left(fname, InStr(fname, vbCrLf) - 1)
bOK = True
If Dir(Dir1.Path & "\" & fname) <> "" Then Kill Dir1.Path & "\" & fname
Open Dir1.Path & "\" & fname For Binary As 1
lPos = 1
wsTCP(1).SendData "OK" & vbCrLf
Else
Dim buffer() As Byte
wsTCP(1).GetData buffer
Put #1, lPos, buffer
lPos = lPos + UBound(buffer) + 1
End If
End Sub
---------------------------------------------------------------
Client
------------------------------
Option Explicit
Dim buffer() As Byte
Dim lBytes As Long
Dim temp As String
Private Sub cmdBrowse_Click()
dlg.ShowOpen
txtFile = dlg.FileName
End Sub
Private Sub cmdSend_Click()
cmdSend.Enabled = False
lBytes = 0
ReDim buffer(FileLen(dlg.FileName) - 1)
Open dlg.FileName For Binary As 1
Get #1, 1, buffer
Close #1
Load wsTCP(1)
wsTCP(1).RemoteHost = txtIP
wsTCP(1).RemotePort = 1111
wsTCP(1).Connect
lblStatus = "Connecting..."
End Sub
Private Sub wsTCP_Close(Index As Integer)
lblStatus = "Connection closed"
Unload wsTCP(1)
End Sub
Private Sub wsTCP_Connect(Index As Integer)
lblStatus = "Connected"
wsTCP(1).SendData dlg.FileTitle & vbCrLf
End Sub
Private Sub wsTCP_DataArrival(Index As Integer, ByVal bytesTotal As Long)
wsTCP(1).GetData temp
If InStr(temp, vbCrLf) <> 0 Then temp = Left(temp, InStr(temp, vbCrLf) - 1)
If temp = "OK" Then
wsTCP(1).SendData buffer
Else
lblStatus = "Something wrong"
Unload wsTCP(1)
cmdSend.Enabled = True
End If
End Sub
Private Sub wsTCP_SendComplete(Index As Integer)
If temp = "OK" Then
lblStatus = "Send complete"
temp = ""
Unload wsTCP(1)
cmdSend.Enabled = True
End If
End Sub
Private Sub wsTCP_SendProgress(Index As Integer, ByVal bytesSent As Long, ByVal bytesRemaining As Long)
If temp = "OK" Then
lBytes = lBytes + bytesSent
lblStatus = lBytes & " out of " & UBound(buffer) & " bytes sent"
End If
End Sub
-------------------------------------------
grazie!!
Il codice mi sembra uguale a quello del link che ti ho postato, o sbaglio? :confused:
Allora, le modifiche sono solo sulla parte del Client...il server non l'ho neanche guardato.
Timer è la funzione che restituisce il n° di secondi trascorsi dalla mezzanotte.
In tempoiniziale salvo quanti secondi sono trascorsi dalla mezzanotte. In questo modo posso sapere quando l'applicazione ha iniziato ad inviare dati.
L'evento sendProgress è generato durante l'invio dei dati: in questo evento salvo sulla variabile tempoAttuale il valore di Timer...faccio un controllo se questo nuovo valore è inferiore al tempoiniziale (in tal caso significa che ho passato la 1/2notte e quindi aggiungo 86400, cioè il n° di secondi che ci sono in un giorno).
Facendo la differenza tempoattuale - tempoiniziale ottengo quanti secondi sono passati dall'inzio dell'invio dei dati. lBytes mi tiene memoria di quanti dati ho inviato e quindi conoscendo i secondi trascorsi è facile calcolare la velocità media d'invio. Poi sapendo tale velocità e avendo in bytesRemaining il n° di bytes che devono essere ancora inviati è altrettanto facile calcolare una stima del tempo d'attesa rimanente.
Qui è stato facile perchè si invia tutto il file in una volta sola, quindi nell'evento sendProgresse so quanti bytes mi mancano...se il file fosse letto man mano da disco le cose cambierebbero. Non so se sto programma funziona per file molto grandi...
'da aggiungere alle variabili globali
dim tempoiniziale
Private Sub wsTCP_DataArrival(Index As Integer, ByVal bytesTotal As Long)
wsTCP(1).GetData temp
If InStr(temp, vbCrLf) <> 0 Then temp = Left(temp, InStr(temp, vbCrLf) - 1)
If temp = "OK" Then
' salvo il tempo corrente
tempoiniziale = Timer
wsTCP(1).SendData buffer
Else
lblStatus = "Something wrong"
Unload wsTCP(1)
cmdSend.Enabled = True
End If
End Sub
Private Sub wsTCP_SendProgress(Index As Integer, ByVal bytesSent As Long, ByVal bytesRemaining As Long)
dim velocitàmedia, temporestante, tempoattuale
If temp = "OK" Then
lBytes = lBytes + bytesSent
tempoattuale= Timer
'piccolo controllo se supero la mezzanotte
if tempoattuale< tempoiniziale then tempoattuale=tempoattuale+86400
' calcolo velocità media in kb/s
velocitàmedia = (lBytes/1024)/(Timer-tempoiniziale)
' calcolo tempo rimanente in secondi
temporestante = (bytesRemaning/1024)/velocitàmedia
lblStatus = lBytes & " out of " & UBound(buffer) & " bytes sent " & _
" at " & velocitàmedia & "Kb/s : elapsed time " & temporestante & " seconds"
End If
End Sub
In alternativa, se ti serve un programma di file transfer posso inviartene uno fatto da me. A guardarlo fa schifo (si inseriscono i comandi come in una console :D) ma funziona discretamente...è possibile effettuare fino a 10 donwload contemporaneamente e l'unico limite alla grandezza del file è dato dalla capienza dell'hard disk...io l'ho fatto per scambiare file di piccole-medie dimensioni(da pochi Kb fino a centinaia di Mb) coi miei amici e devo dire che il suo sporco lavoro lo fa :D
Aloha!
grazie 1000 ora cerco di studiarmelo x bene!
azz...
nel codice sostituisci questo
velocitàmedia = (lBytes/1024)/(Timer-tempoiniziale)
con questo
velocitàmedia = (lBytes/1024)/(tempoattuale-tempoiniziale)
:D
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.