Oppure

Loading
09/07/09 17:43
GoLDBeRG
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text

Public Class download

    Dim quanti As Integer
    Dim down As NetworkStream
    Dim sock As Socket

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim thr As New Thread(AddressOf scarica)
        thr.Start()
    End Sub

    Public Sub scarica()
        ' File.Delete("C:\casa.zip")
        Dim link As String = "http://www.microsoft.com/downloads/info.aspx?na=90&p=&SrcDisplayLang=it&SrcCategoryId=&SrcFamilyId=5b33b5a8-5e76-401f-be08-1e1555d4f3d4&u=http%3a%2f%2fdownload.microsoft.com%2fdownload%2f9%2f4%2fd%2f94d3f80a-2c0d-4ce1-a983-52f946b62c78%2fWindowsXP-KB936929-SP3-x86-ITA.exe"
        sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        sock.Connect("colossus.altervista.org", 80)
        down = New NetworkStream(sock)
        Dim byt() As Byte = Encoding.ASCII.GetBytes("GET http://colossus.altervista.org/file/Colossus.zip" & vbCrLf)
        Dim altro As New Thread(AddressOf anco)
        altro.Start()
        down.Write(byt, 0, byt.Length)
        down.Flush()
    End Sub

    Public Sub anco()
        While (True)
            Dim ric(1024) As Byte
            sock.Receive(ric, ric.Length, SocketFlags.None)
            Dim str As String = Encoding.ASCII.GetString(ric)
            MsgBox(str)
        End While
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            Label2.Text = Int((quanti / 1024) / 5)
            quanti = 0
        Catch ex As Exception

        End Try
    End Sub
End Class


guarda un po che sorpresa.... adesso vediamo se è veloce il nostro ben amato socket puro...
aaa
09/07/09 18:05
GoLDBeRG
qui l'ho portato addirittura in asincrono.... e indvina un po... 125 kb/s a malapena.... con orbit che va a 621.... a chi devo dare la colpa ai socket per forza... sti maledetti forse sono lenti

  Dim quanti As Integer
    Dim down As NetworkStream
    Dim sock As Socket
    Dim buff(8192) As Byte

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim thr As New Thread(AddressOf scarica)
        thr.Start()
    End Sub

    Public Sub scarica()
        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" & vbCrLf)
        sock.BeginReceive(buff, 0, buff.Length, SocketFlags.None, New AsyncCallback(AddressOf anco), Nothing)
        down.Write(byt, 0, byt.Length)
        down.Flush()
    End Sub

    Public Sub anco(ByVal ar As IAsyncResult)
        Dim int As Integer = sock.EndReceive(ar)
        If int < 1 Then
            End
        End If
        quanti += int
        sock.BeginReceive(buff, 0, buff.Length, SocketFlags.None, New AsyncCallback(AddressOf anco), Nothing)
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            Label2.Text = Int((quanti / 1024) / 5)
            quanti = 0
        Catch ex As Exception

        End Try
    End Sub
Ultima modifica effettuata da GoLDBeRG 09/07/09 18:06
aaa
09/07/09 18:20
GoLDBeRG
ok rise ci sono.... ho intorzato il codice del download singolo in una classe e ho premito 10 volte l'avvio del thread..... quindi 10 download con 10 socket diversi.... 810 O.O...
il problema non era il socket singolo... il problema era che dovevo aprirne di piu in contemporanea e sommar ei byte che ricevo.... ecco come fa quello stupido di orbit a correre cosi apre 20 socket per ogni download.... bene... adesso dovro' aprire un altro thread per scoprire come coordinare questi 10 socket.... se tipo divido il file remoto in 10 pezzi e ogni socket scarica un pezzo e poi li unisco? è un macello... che ne pensi?
aaa
09/07/09 18:59
riseofapocalypse
Scusami ma non ero in casa! Comunque ci avevo già pensato ma avevo trascurato l'idea per il problema della divisione del file in parti...ma ora, visto che siamo in ballo, balliamo! :D
Dunque, si tratta di gestire il problema della divisione in parti uguali del file, ciascuna per ogni Socket! Io un'ideuzza ce l'avrei, ora mi metto al lavoro :k:
aaa
09/07/09 19:38
GoLDBeRG
il problema sono proprio le parti uguali.... se nn è divisibile per 10?? si fa 11?? capito il problema.....
aaa
09/07/09 19:52
riseofapocalypse
Ho aggiornato la mia classe Download in modo che utilizzi diversi Thread in parallelo, inoltre il numero dei Thread è impostabile tramite il costruttore :k: :D
Class Download
#Region "Events"
    Public Event DownloadStarted As EventHandler
    Public Event DownloadCompleted As EventHandler
    Public Event DownloadProgressChanged As EventHandler
    Public Event DownloadAborted As EventHandler
    Public Event DownloadPaused As EventHandler
    Public Event DownloadResumed As EventHandler
#End Region
#Region "Attributes"
    Private s, d As String
    Private b, sec, v As Decimal
    Private nt As Decimal
    Private t() As Threading.Thread
    Private ss As IO.BufferedStream, ds As IO.FileStream, dst As DownStatus
    Private WithEvents tim As New Timer With {.Interval = 1000}
#End Region
#Region "Enums"
    Enum DownStatus
        NoOperation = 0
        Downloading
        Paused
        Completed
    End Enum
#End Region
#Region "Constructors"
    Public Sub New(ByVal url As String, ByVal filename As String, ByVal threadnumber As Decimal)
        s = url
        d = filename
        b = 0
        sec = 0
        nt = threadnumber
        Array.Resize(t, nt)
        For i As Integer = 0 To nt - 1
            t(i) = New Threading.Thread(AddressOf Download)
        Next
        ss = Nothing
        ds = Nothing
        dst = DownStatus.NoOperation
    End Sub
#End Region
#Region "Properties"
    Public Property Source() As String
        Get
            Return s
        End Get
        Set(ByVal value As String)
            s = value
        End Set
    End Property
    Public Property Destination() As String
        Get
            Return d
        End Get
        Set(ByVal value As String)
            d = value
        End Set
    End Property
    Public ReadOnly Property DownloadedBytes() As Decimal
        Get
            Return b
        End Get
    End Property
    Public ReadOnly Property Seconds() As Decimal
        Get
            Return sec
        End Get
    End Property
    Public ReadOnly Property Speed() As Decimal
        Get
            Return v
        End Get
    End Property
    Public ReadOnly Property DownloadStatus() As DownStatus
        Get
            Return dst
        End Get
    End Property
#End Region
#Region "Public methods"
    Public Sub StartDownload()
        ss = New IO.BufferedStream(Net.WebRequest.Create(s).GetResponse.GetResponseStream)
        ds = New IO.FileStream(d, IO.FileMode.OpenOrCreate)
        For i As Integer = 0 To nt - 1
            t(i).Start()
        Next
        tim.Start()
        dst = DownStatus.Downloading
        RaiseEvent DownloadStarted(Me, New EventArgs)
    End Sub
    Public Sub AbortDownload()
        dst = DownStatus.NoOperation
        For i As Integer = 0 To nt - 1
            t(i).Abort()
        Next
        tim.Stop()
        ds.Close()
        ss.Close()
        RaiseEvent DownloadAborted(Me, New EventArgs)
    End Sub
    Public Sub PauseDownload()
        dst = DownStatus.Paused
        For i As Integer = 0 To nt - 1
            t(i).Suspend()
        Next
        tim.Stop()
        RaiseEvent DownloadPaused(Me, New EventArgs)
    End Sub
    Public Sub ResumeDownload()
        tim.Start()
        For i As Integer = 0 To nt - 1
            t(i).Resume()
        Next
        dst = DownStatus.Downloading
        RaiseEvent DownloadResumed(Me, New EventArgs)
    End Sub
#End Region
#Region "Private methods"
    Private Sub Download()
        While True
            Try
                ds.WriteByte(ss.ReadByte)
            Catch
                Exit While
            End Try
            b += 1
            Application.DoEvents()
            RaiseEvent DownloadProgressChanged(Me, New EventArgs)
        End While
        tim.Stop()
        ds.Close()
        ss.Close()
        dst = DownStatus.Completed
        nt -= 1
        If nt = 0 Then RaiseEvent DownloadCompleted(Me, New EventArgs)
    End Sub
#End Region
#Region "Private events"
    Private Sub tim_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tim.Tick
        sec += 1
        v = b / sec
    End Sub
#End Region
End Class

Provala e dimmi che velocità raggiunge :-| :rotfl:

P.S. Lascia perdere se è un po incasinata, l'ho aggiornata di fretta senza badare molto all'eleganza! xD
Ultima modifica effettuata da riseofapocalypse 09/07/09 19:55
aaa
10/07/09 7:22
GoLDBeRG
sbaglio o non divide il file in parti uguali..... si limita solamente a scaricare in parallelo piu volte il file... ti faccio un esempio.....


|---------------| tutto il file

|--| socket 1
|--| socket 2
|--| socket 3
|--| socket 4
|--| socket 5
|--| socket 6

pero' i socket devono essere daccordo tra di loro di dove iniziare a leggere l'offset dello stream e dove finire... intanto il problema sorge perche non si sa a priori quanto sia grande il file... a meno che non esiste un'altra funzione http come il get che ce lo dice.... poi il numero di bytes va diviso in parti uguali e ogni socket scarica solo quella parte assegnata. poi alla fine con un ciclo si scrive su file prima il buffer del socket 1 poi quello del due e cosi via... in modo che si ricrea il file.... ora vedo se riesco a modificare la classe che mi hai dato in modo da fargli dividere in parti uguali il file...
aaa
10/07/09 7:48
riseofapocalypse
Beh con la mia classe non c'è bisogno di dividere in parti uguali il file, poichè tutti i Thread hanno in comune lo Stream di lettura e lo Stream di scrittura, per cui l'Offset avanza da solo dopo ogni lettura/scrittura :D
aaa