Oppure

Loading
12/07/09 8:33
GoLDBeRG
Public Sub vedi()
        Dim wc As New Net.WebClient
        Dim b1() As Byte = wc.Encoding.GetBytes("Get http://www.artfiles.org/knoppix/KNOPPIX_V5.1.1CD-2007-01-04-EN.iso HTTP/1.1" & vbNewLine & vbNewLine)
        wc.OpenWrite("http://www.artfiles.org/").Write(b1, 0, b1.Length)
        wc.OpenRead("http://www.artfiles.org/knoppix/KNOPPIX_V5.1.1CD-2007-01-04-EN.iso")
        For Each asd As String In wc.ResponseHeaders
            If IsNumeric(wc.ResponseHeaders.Item(asd)) Then
                filegen = wc.ResponseHeaders.Item(asd)
            End If
        Next
        Dim conto As Int16 = 10
        While ((filegen Mod conto) <> 0)
            conto += 1
        End While
        Dim parte As Long = filegen / conto
        For a As Integer = 0 To conto - 2
            Dim f As New conn
            f.parte = parte
            f.start = parte * a
            f.ferma = f.start + parte
            Dim g As New Thread(AddressOf f.scarica)
            g.Start()
        Next
    End Sub


con questa procedura vedo la grandezza del file remoto... la divido per il giusto numero di byte... pezzi uguali... e chiamo una classe che dovrebbe scaricare solo una porzione di file...

Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text

Public Class conn

    Public down As NetworkStream
    Public sock As Socket
    Public parte As Long
    Public buff() As Byte
    Public start As Long
    Public ferma As Long
    Public memo As Long

    Public Sub scarica()
        Array.Resize(buff, parte)
        sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        sock.Connect("www.artfiles.org", 80)
        down = New NetworkStream(sock)
        Dim byt() As Byte = Encoding.ASCII.GetBytes("GET http://www.artfiles.org/knoppix/KNOPPIX_V5.1.1CD-2007-01-04-EN.iso HTTP/1.1" & vbCrLf & vbCrLf)
        'devo mettermi in ascolto e chiedergli di iniziare 
        'a leggere dal byte specificato in poi fino al byte dovuto
        down.Write(byt, 0, byt.Length)
        down.Flush()
    End Sub

    Public Sub lettura()
       
    End Sub
End Class


siccome il metodo Seek genera sempre un errore di NotSupportException cosa posso usare? come faccio a fargli scaricare solo dal byte specificato invece che dall'inizio?? grazie a tutti
aaa
12/07/09 9:23
theprogrammer
Non penso proprio che tu possa se non e' previsto da codice lato server.
aaa
12/07/09 10:04
riseofapocalypse
Per aggirare questo problema ho provato a ricostruire la mia classe Download con i Thread che scaricano in parallelo dallo stesso Stream, però ho aggiunto un particolare: siccome lo Stream non permette letture in parallelo, ho gestito il problema facendo eliminare i Thread che leggono negli stessi istanti! :D
Indovina un po': la velocità è alta soltanto se non invoco gli eventi DownloadSpeedChanged e DownloadProgressChanged! -.-"
Public Class Download
#Region "EventArgs"
    Public Class DownloadProgressChangedEventArgs
        Inherits EventArgs
        Dim np As Decimal
        Public Sub New(ByVal NewProgress As Decimal)
            np = NewProgress
        End Sub
        Public ReadOnly Property NewProgress() As Decimal
            Get
                Return np
            End Get
        End Property
    End Class
    Public Class DownloadSpeedChangedEventArgs
        Inherits EventArgs
        Dim s As Decimal
        Public Sub New(ByVal Speed As Decimal)
            s = Speed
        End Sub
        Public ReadOnly Property Speed() As Decimal
            Get
                Return s
            End Get
        End Property
    End Class
    Public Class DownloadCompletedEventArgs
        Inherits EventArgs
        Dim ms As Decimal
        Public Sub New(ByVal MediumSpeed As Decimal)
            ms = MediumSpeed
        End Sub
        Public ReadOnly Property MediumSpeed() As Decimal
            Get
                Return ms
            End Get
        End Property
    End Class
    Public Class DownloadStartedEventArgs
        Inherits EventArgs
        Dim fs As Decimal
        Public Sub New(ByVal FileSize As Decimal)
            fs = FileSize
        End Sub
        Public ReadOnly Property FileSize() As Decimal
            Get
                Return fs
            End Get
        End Property
    End Class
#End Region
#Region "Events"
    Public Event DownloadStarted As EventHandler(Of Download.DownloadStartedEventArgs)
    Public Event DownloadCompleted As EventHandler(Of Download.DownloadCompletedEventArgs)
    Public Event DownloadPaused As EventHandler
    Public Event DownloadResumed As EventHandler
    Public Event DownloadAborted As EventHandler
    Public Event DownloadProgressChanged As EventHandler(Of Download.DownloadProgressChangedEventArgs)
    Public Event DownloadSpeedChanged As EventHandler(Of Download.DownloadSpeedChangedEventArgs)
#End Region
#Region "Attributes"
    Dim numerosocket As Decimal, sec As Decimal, bn As Decimal
    Dim tl As New List(Of Thread), v As New List(Of Decimal)
    Dim ds As FileStream, ss As Stream
    Dim WithEvents Timer1 As New Windows.Forms.Timer With {.Interval = 1000}
    Dim wc As New WebClient
#End Region
#Region "Constructors"
    Public Sub New(ByVal url As String, ByVal filename As String, ByVal threads As Decimal)
        ss = wc.OpenRead(url)
        ds = New FileStream(filename, FileMode.Create)
        numerosocket = threads
        sec = 0
        bn = 0
        tl = New List(Of Thread)
        v = New List(Of Decimal)
    End Sub
#End Region
#Region "Private methods"
    Private Function MediumSpeed() As Decimal
        Dim r As Decimal = 0
        For i As Integer = 0 To v.Count - 1
            r += v(i)
        Next
        Return r / v.Count
    End Function
    Private Sub Download()
        While True
            Try
                ds.WriteByte(ss.ReadByte)
            Catch ex As NotSupportedException
                GoTo fine
            Catch
                Exit While
            End Try
            bn += 1
            'RaiseEvent DownloadProgressChanged(Me, New DownloadProgressChangedEventArgs(bn))
        End While
        If tl.Count = 1 Then
            Timer1.Stop()
            tl.Clear()
            ds.Close()
            ss.Close()
            sec = 0
            RaiseEvent DownloadCompleted(Me, New DownloadCompletedEventArgs(MediumSpeed))
            v.Clear()
        Else
fine:       tl.Remove(Thread.CurrentThread)
        End If
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        On Error Resume Next
        sec += 1
        Dim speed As Decimal = bn / sec
        v.Add(speed)
        'RaiseEvent DownloadSpeedChanged(Me, New DownloadSpeedChangedEventArgs(speed))
    End Sub
#End Region
#Region "Public methods"
    Public Sub StartDownload()
        RaiseEvent DownloadStarted(Me, New DownloadStartedEventArgs(wc.ResponseHeaders(HttpResponseHeader.ContentLength)))
        For i As Integer = 1 To numerosocket
            Dim t As New Thread(AddressOf Download)
            tl.Add(t)
            t.Start()
        Next
        Timer1.Start()
    End Sub
    Public Sub PauseDownload()
        On Error Resume Next
        For i As Integer = 0 To tl.Count - 1
            tl(i).Suspend()
        Next
        Timer1.Stop()
        RaiseEvent DownloadPaused(Me, New EventArgs)
    End Sub
    Public Sub ResumeDownload()
        On Error Resume Next
        RaiseEvent DownloadResumed(Me, New EventArgs)
        For i As Integer = 0 To tl.Count - 1
            tl(i).Resume()
        Next
        Timer1.Start()
    End Sub
    Public Sub AbortDownload()
        On Error Resume Next
        For i As Integer = 0 To tl.Count - 1
            tl(i).Abort()
        Next
        Timer1.Stop()
        tl.Clear()
        ds.Close()
        ss.Close()
        sec = 0
        v.Clear()
        RaiseEvent DownloadAborted(Me, New EventArgs)
    End Sub
#End Region
End Class

Prova tu stesso! Nel DownloadCompletedEventArgs dell'evento DownloadCompleted viene restituita la velocità media del Download!

P.S. Per il File colossus.altervista.org/file/… mi va sui 200 KB/sec, però se invoco quei due eventi mi va a malapena sui 50 KB/sec! Con 30 Thread intendo :k:
Ultima modifica effettuata da riseofapocalypse 12/07/09 10:07
aaa
12/07/09 10:27
riseofapocalypse
Ho risolto! :rotfl:
Public Class Download
#Region "EventArgs"
    Public Class DownloadProgressChangedEventArgs
        Inherits EventArgs
        Dim np, s As Decimal
        Public Sub New(ByVal NewProgress As Decimal, ByVal Speed As Decimal)
            np = NewProgress
            s = Speed
        End Sub
        Public ReadOnly Property NewProgress() As Decimal
            Get
                Return np
            End Get
        End Property
        Public ReadOnly Property Speed() As Decimal
            Get
                Return s
            End Get
        End Property
    End Class
    Public Class DownloadCompletedEventArgs
        Inherits EventArgs
        Dim ms As Decimal
        Public Sub New(ByVal MediumSpeed As Decimal)
            ms = MediumSpeed
        End Sub
        Public ReadOnly Property MediumSpeed() As Decimal
            Get
                Return ms
            End Get
        End Property
    End Class
    Public Class DownloadStartedEventArgs
        Inherits EventArgs
        Dim fs As Decimal
        Public Sub New(ByVal FileSize As Decimal)
            fs = FileSize
        End Sub
        Public ReadOnly Property FileSize() As Decimal
            Get
                Return fs
            End Get
        End Property
    End Class
#End Region
#Region "Events"
    Public Event DownloadStarted As EventHandler(Of Download.DownloadStartedEventArgs)
    Public Event DownloadCompleted As EventHandler(Of Download.DownloadCompletedEventArgs)
    Public Event DownloadPaused As EventHandler
    Public Event DownloadResumed As EventHandler
    Public Event DownloadAborted As EventHandler
    Public Event DownloadProgressChanged As EventHandler(Of Download.DownloadProgressChangedEventArgs)
#End Region
#Region "Attributes"
    Dim numerosocket As Decimal, sec As Decimal, bn As Decimal
    Dim tl As New List(Of Thread), v As New List(Of Decimal)
    Dim ds As FileStream, ss As Stream
    Dim WithEvents Timer1 As New Timers.Timer(500)
    Dim wc As New WebClient
#End Region
#Region "Constructors"
    Public Sub New(ByVal url As String, ByVal filename As String, ByVal threads As Decimal)
        ss = wc.OpenRead(url)
        ds = New FileStream(filename, FileMode.Create)
        numerosocket = threads
        sec = 0
        bn = 0
        tl = New List(Of Thread)
        v = New List(Of Decimal)
    End Sub
#End Region
#Region "Private methods"
    Private Function MediumSpeed() As Decimal
        Dim r As Decimal = 0
        For i As Integer = 0 To v.Count - 1
            r += v(i)
        Next
        Return r / v.Count
    End Function
    Private Sub Timer1_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
        On Error Resume Next
        sec += Timer1.Interval / 1000
        Dim speed As Decimal = bn / sec
        v.Add(speed)
        RaiseEvent DownloadProgressChanged(Me, New DownloadProgressChangedEventArgs(bn, speed))
    End Sub
    Private Sub Download()
        While True
            Try
                ds.WriteByte(ss.ReadByte)
            Catch ex As NotSupportedException
                GoTo fine
            Catch
                Exit While
            End Try
            bn += 1
        End While
        If tl.Count = 1 Then
            Timer1.Stop()
            tl.Clear()
            ds.Close()
            ss.Close()
            sec = 0
            RaiseEvent DownloadCompleted(Me, New DownloadCompletedEventArgs(MediumSpeed))
            v.Clear()
        Else
fine:       tl.Remove(Thread.CurrentThread)
        End If
    End Sub
#End Region
#Region "Public methods"
    Public Sub StartDownload()
        RaiseEvent DownloadStarted(Me, New DownloadStartedEventArgs(wc.ResponseHeaders(HttpResponseHeader.ContentLength)))
        For i As Integer = 1 To numerosocket
            Dim t As New Thread(AddressOf Download)
            tl.Add(t)
            t.Start()
        Next
        Timer1.Start()
    End Sub
    Public Sub PauseDownload()
        On Error Resume Next
        For i As Integer = 0 To tl.Count - 1
            tl(i).Suspend()
        Next
        Timer1.Stop()
        RaiseEvent DownloadPaused(Me, New EventArgs)
    End Sub
    Public Sub ResumeDownload()
        On Error Resume Next
        RaiseEvent DownloadResumed(Me, New EventArgs)
        For i As Integer = 0 To tl.Count - 1
            tl(i).Resume()
        Next
        Timer1.Start()
    End Sub
    Public Sub AbortDownload()
        On Error Resume Next
        For i As Integer = 0 To tl.Count - 1
            tl(i).Abort()
        Next
        Timer1.Stop()
        tl.Clear()
        ds.Close()
        ss.Close()
        sec = 0
        v.Clear()
        RaiseEvent DownloadAborted(Me, New EventArgs)
    End Sub
#End Region
End Class

Addirittura è arrivato ad una media di 400 KB/sec! In pratica anzichè usare il System.Windows.Forms.Timer, ho usato il System.Timers.Timer! Inoltre ho eliminato l'evento DownloadSpeedChanged e ho convogliato la sua proprietà Speed in DownloadProgressChanged, in modo che lo chiamo solo quando cambia la velocità! :k:
aaa
12/07/09 11:09
theprogrammer
Scusa ... mi sono perso qualcosa o la domanda era un'altra ? Cosa c'entra la velocità di download ...?
aaa
12/07/09 11:15
riseofapocalypse
Questo Thread è derivato da un altro Thread, il cui problema inizialmente era la velocità di Download! Insomma è un intrico di problemi :D siccome non credo ci sia una soluzione per questo Seek, ho proposto la mia classe di Download modificata :k:
aaa
12/07/09 11:18
theprogrammer
Postato originariamente da riseofapocalypse:

Questo Thread è derivato da un altro Thread, il cui problema inizialmente era la velocità di Download! Insomma è un intrico di problemi :D siccome non credo ci sia una soluzione per questo Seek, ho proposto la mia classe di Download modificata :k:


Ok ... ma cosi' non si capisce niente ...
aaa
12/07/09 11:21
GoLDBeRG
quindi mi stai dicendo che se io non invoco il progresschanged la stessa classe webclient va molto piu veloce?
aaa