Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Seek di uno stream....
Forum - C# / VB.NET - Seek di uno stream....

Pagine: [ 1 2 3 4 5 ] Precedente | Prossimo
Avatar
GoLDBeRG (Ex-Member)
Expert


Messaggi: 331
Iscritto: 19/12/2005

Segnala al moderatore
Postato alle 10:33
Domenica, 12/07/2009
Codice sorgente - presumibilmente VB.NET

  1. Public Sub vedi()
  2.         Dim wc As New Net.WebClient
  3.         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)
  4.         wc.OpenWrite("http://www.artfiles.org/").Write(b1, 0, b1.Length)
  5.         wc.OpenRead("http://www.artfiles.org/knoppix/KNOPPIX_V5.1.1CD-2007-01-04-EN.iso")
  6.         For Each asd As String In wc.ResponseHeaders
  7.             If IsNumeric(wc.ResponseHeaders.Item(asd)) Then
  8.                 filegen = wc.ResponseHeaders.Item(asd)
  9.             End If
  10.         Next
  11.         Dim conto As Int16 = 10
  12.         While ((filegen Mod conto) <> 0)
  13.             conto += 1
  14.         End While
  15.         Dim parte As Long = filegen / conto
  16.         For a As Integer = 0 To conto - 2
  17.             Dim f As New conn
  18.             f.parte = parte
  19.             f.start = parte * a
  20.             f.ferma = f.start + parte
  21.             Dim g As New Thread(AddressOf f.scarica)
  22.             g.Start()
  23.         Next
  24.     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...

Codice sorgente - presumibilmente VB.NET

  1. Imports System
  2. Imports System.IO
  3. Imports System.Net
  4. Imports System.Net.Sockets
  5. Imports System.Threading
  6. Imports System.Text
  7.  
  8. Public Class conn
  9.  
  10.     Public down As NetworkStream
  11.     Public sock As Socket
  12.     Public parte As Long
  13.     Public buff() As Byte
  14.     Public start As Long
  15.     Public ferma As Long
  16.     Public memo As Long
  17.  
  18.     Public Sub scarica()
  19.         Array.Resize(buff, parte)
  20.         sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
  21.         sock.Connect("www.artfiles.org", 80)
  22.         down = New NetworkStream(sock)
  23.         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)
  24.         'devo mettermi in ascolto e chiedergli di iniziare
  25.         'a leggere dal byte specificato in poi fino al byte dovuto
  26.         down.Write(byt, 0, byt.Length)
  27.         down.Flush()
  28.     End Sub
  29.  
  30.     Public Sub lettura()
  31.        
  32.     End Sub
  33. 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

PM Quote
Avatar
theprogrammer (Normal User)
Guru^2


Messaggi: 2509
Iscritto: 28/01/2009

Segnala al moderatore
Postato alle 11:23
Domenica, 12/07/2009
Non penso proprio che tu possa se non e' previsto da codice lato server.

PM Quote
Avatar
riseofapocalypse (Ex-Member)
Pro


Messaggi: 150
Iscritto: 08/07/2009

Segnala al moderatore
Postato alle 12:04
Domenica, 12/07/2009
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! -.-"
Codice sorgente - presumibilmente VB.NET

  1. Public Class Download
  2. #Region "EventArgs"
  3.     Public Class DownloadProgressChangedEventArgs
  4.         Inherits EventArgs
  5.         Dim np As Decimal
  6.         Public Sub New(ByVal NewProgress As Decimal)
  7.             np = NewProgress
  8.         End Sub
  9.         Public ReadOnly Property NewProgress() As Decimal
  10.             Get
  11.                 Return np
  12.             End Get
  13.         End Property
  14.     End Class
  15.     Public Class DownloadSpeedChangedEventArgs
  16.         Inherits EventArgs
  17.         Dim s As Decimal
  18.         Public Sub New(ByVal Speed As Decimal)
  19.             s = Speed
  20.         End Sub
  21.         Public ReadOnly Property Speed() As Decimal
  22.             Get
  23.                 Return s
  24.             End Get
  25.         End Property
  26.     End Class
  27.     Public Class DownloadCompletedEventArgs
  28.         Inherits EventArgs
  29.         Dim ms As Decimal
  30.         Public Sub New(ByVal MediumSpeed As Decimal)
  31.             ms = MediumSpeed
  32.         End Sub
  33.         Public ReadOnly Property MediumSpeed() As Decimal
  34.             Get
  35.                 Return ms
  36.             End Get
  37.         End Property
  38.     End Class
  39.     Public Class DownloadStartedEventArgs
  40.         Inherits EventArgs
  41.         Dim fs As Decimal
  42.         Public Sub New(ByVal FileSize As Decimal)
  43.             fs = FileSize
  44.         End Sub
  45.         Public ReadOnly Property FileSize() As Decimal
  46.             Get
  47.                 Return fs
  48.             End Get
  49.         End Property
  50.     End Class
  51. #End Region
  52. #Region "Events"
  53.     Public Event DownloadStarted As EventHandler(Of Download.DownloadStartedEventArgs)
  54.     Public Event DownloadCompleted As EventHandler(Of Download.DownloadCompletedEventArgs)
  55.     Public Event DownloadPaused As EventHandler
  56.     Public Event DownloadResumed As EventHandler
  57.     Public Event DownloadAborted As EventHandler
  58.     Public Event DownloadProgressChanged As EventHandler(Of Download.DownloadProgressChangedEventArgs)
  59.     Public Event DownloadSpeedChanged As EventHandler(Of Download.DownloadSpeedChangedEventArgs)
  60. #End Region
  61. #Region "Attributes"
  62.     Dim numerosocket As Decimal, sec As Decimal, bn As Decimal
  63.     Dim tl As New List(Of Thread), v As New List(Of Decimal)
  64.     Dim ds As FileStream, ss As Stream
  65.     Dim WithEvents Timer1 As New Windows.Forms.Timer With {.Interval = 1000}
  66.     Dim wc As New WebClient
  67. #End Region
  68. #Region "Constructors"
  69.     Public Sub New(ByVal url As String, ByVal filename As String, ByVal threads As Decimal)
  70.         ss = wc.OpenRead(url)
  71.         ds = New FileStream(filename, FileMode.Create)
  72.         numerosocket = threads
  73.         sec = 0
  74.         bn = 0
  75.         tl = New List(Of Thread)
  76.         v = New List(Of Decimal)
  77.     End Sub
  78. #End Region
  79. #Region "Private methods"
  80.     Private Function MediumSpeed() As Decimal
  81.         Dim r As Decimal = 0
  82.         For i As Integer = 0 To v.Count - 1
  83.             r += v(i)
  84.         Next
  85.         Return r / v.Count
  86.     End Function
  87.     Private Sub Download()
  88.         While True
  89.             Try
  90.                 ds.WriteByte(ss.ReadByte)
  91.             Catch ex As NotSupportedException
  92.                 GoTo fine
  93.             Catch
  94.                 Exit While
  95.             End Try
  96.             bn += 1
  97.             'RaiseEvent DownloadProgressChanged(Me, New DownloadProgressChangedEventArgs(bn))
  98.         End While
  99.         If tl.Count = 1 Then
  100.             Timer1.Stop()
  101.             tl.Clear()
  102.             ds.Close()
  103.             ss.Close()
  104.             sec = 0
  105.             RaiseEvent DownloadCompleted(Me, New DownloadCompletedEventArgs(MediumSpeed))
  106.             v.Clear()
  107.         Else
  108. fine:       tl.Remove(Thread.CurrentThread)
  109.         End If
  110.     End Sub
  111.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  112.         On Error Resume Next
  113.         sec += 1
  114.         Dim speed As Decimal = bn / sec
  115.         v.Add(speed)
  116.         'RaiseEvent DownloadSpeedChanged(Me, New DownloadSpeedChangedEventArgs(speed))
  117.     End Sub
  118. #End Region
  119. #Region "Public methods"
  120.     Public Sub StartDownload()
  121.         RaiseEvent DownloadStarted(Me, New DownloadStartedEventArgs(wc.ResponseHeaders(HttpResponseHeader.ContentLength)))
  122.         For i As Integer = 1 To numerosocket
  123.             Dim t As New Thread(AddressOf Download)
  124.             tl.Add(t)
  125.             t.Start()
  126.         Next
  127.         Timer1.Start()
  128.     End Sub
  129.     Public Sub PauseDownload()
  130.         On Error Resume Next
  131.         For i As Integer = 0 To tl.Count - 1
  132.             tl(i).Suspend()
  133.         Next
  134.         Timer1.Stop()
  135.         RaiseEvent DownloadPaused(Me, New EventArgs)
  136.     End Sub
  137.     Public Sub ResumeDownload()
  138.         On Error Resume Next
  139.         RaiseEvent DownloadResumed(Me, New EventArgs)
  140.         For i As Integer = 0 To tl.Count - 1
  141.             tl(i).Resume()
  142.         Next
  143.         Timer1.Start()
  144.     End Sub
  145.     Public Sub AbortDownload()
  146.         On Error Resume Next
  147.         For i As Integer = 0 To tl.Count - 1
  148.             tl(i).Abort()
  149.         Next
  150.         Timer1.Stop()
  151.         tl.Clear()
  152.         ds.Close()
  153.         ss.Close()
  154.         sec = 0
  155.         v.Clear()
  156.         RaiseEvent DownloadAborted(Me, New EventArgs)
  157.     End Sub
  158. #End Region
  159. End Class


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

P.S. Per il File http://colossus.altervista.org/file/Colossus.zip 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 il 12/07/2009 alle 12:07
PM Quote
Avatar
riseofapocalypse (Ex-Member)
Pro


Messaggi: 150
Iscritto: 08/07/2009

Segnala al moderatore
Postato alle 12:27
Domenica, 12/07/2009
Ho risolto! :rotfl:
Codice sorgente - presumibilmente VB.NET

  1. Public Class Download
  2. #Region "EventArgs"
  3.     Public Class DownloadProgressChangedEventArgs
  4.         Inherits EventArgs
  5.         Dim np, s As Decimal
  6.         Public Sub New(ByVal NewProgress As Decimal, ByVal Speed As Decimal)
  7.             np = NewProgress
  8.             s = Speed
  9.         End Sub
  10.         Public ReadOnly Property NewProgress() As Decimal
  11.             Get
  12.                 Return np
  13.             End Get
  14.         End Property
  15.         Public ReadOnly Property Speed() As Decimal
  16.             Get
  17.                 Return s
  18.             End Get
  19.         End Property
  20.     End Class
  21.     Public Class DownloadCompletedEventArgs
  22.         Inherits EventArgs
  23.         Dim ms As Decimal
  24.         Public Sub New(ByVal MediumSpeed As Decimal)
  25.             ms = MediumSpeed
  26.         End Sub
  27.         Public ReadOnly Property MediumSpeed() As Decimal
  28.             Get
  29.                 Return ms
  30.             End Get
  31.         End Property
  32.     End Class
  33.     Public Class DownloadStartedEventArgs
  34.         Inherits EventArgs
  35.         Dim fs As Decimal
  36.         Public Sub New(ByVal FileSize As Decimal)
  37.             fs = FileSize
  38.         End Sub
  39.         Public ReadOnly Property FileSize() As Decimal
  40.             Get
  41.                 Return fs
  42.             End Get
  43.         End Property
  44.     End Class
  45. #End Region
  46. #Region "Events"
  47.     Public Event DownloadStarted As EventHandler(Of Download.DownloadStartedEventArgs)
  48.     Public Event DownloadCompleted As EventHandler(Of Download.DownloadCompletedEventArgs)
  49.     Public Event DownloadPaused As EventHandler
  50.     Public Event DownloadResumed As EventHandler
  51.     Public Event DownloadAborted As EventHandler
  52.     Public Event DownloadProgressChanged As EventHandler(Of Download.DownloadProgressChangedEventArgs)
  53. #End Region
  54. #Region "Attributes"
  55.     Dim numerosocket As Decimal, sec As Decimal, bn As Decimal
  56.     Dim tl As New List(Of Thread), v As New List(Of Decimal)
  57.     Dim ds As FileStream, ss As Stream
  58.     Dim WithEvents Timer1 As New Timers.Timer(500)
  59.     Dim wc As New WebClient
  60. #End Region
  61. #Region "Constructors"
  62.     Public Sub New(ByVal url As String, ByVal filename As String, ByVal threads As Decimal)
  63.         ss = wc.OpenRead(url)
  64.         ds = New FileStream(filename, FileMode.Create)
  65.         numerosocket = threads
  66.         sec = 0
  67.         bn = 0
  68.         tl = New List(Of Thread)
  69.         v = New List(Of Decimal)
  70.     End Sub
  71. #End Region
  72. #Region "Private methods"
  73.     Private Function MediumSpeed() As Decimal
  74.         Dim r As Decimal = 0
  75.         For i As Integer = 0 To v.Count - 1
  76.             r += v(i)
  77.         Next
  78.         Return r / v.Count
  79.     End Function
  80.     Private Sub Timer1_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
  81.         On Error Resume Next
  82.         sec += Timer1.Interval / 1000
  83.         Dim speed As Decimal = bn / sec
  84.         v.Add(speed)
  85.         RaiseEvent DownloadProgressChanged(Me, New DownloadProgressChangedEventArgs(bn, speed))
  86.     End Sub
  87.     Private Sub Download()
  88.         While True
  89.             Try
  90.                 ds.WriteByte(ss.ReadByte)
  91.             Catch ex As NotSupportedException
  92.                 GoTo fine
  93.             Catch
  94.                 Exit While
  95.             End Try
  96.             bn += 1
  97.         End While
  98.         If tl.Count = 1 Then
  99.             Timer1.Stop()
  100.             tl.Clear()
  101.             ds.Close()
  102.             ss.Close()
  103.             sec = 0
  104.             RaiseEvent DownloadCompleted(Me, New DownloadCompletedEventArgs(MediumSpeed))
  105.             v.Clear()
  106.         Else
  107. fine:       tl.Remove(Thread.CurrentThread)
  108.         End If
  109.     End Sub
  110. #End Region
  111. #Region "Public methods"
  112.     Public Sub StartDownload()
  113.         RaiseEvent DownloadStarted(Me, New DownloadStartedEventArgs(wc.ResponseHeaders(HttpResponseHeader.ContentLength)))
  114.         For i As Integer = 1 To numerosocket
  115.             Dim t As New Thread(AddressOf Download)
  116.             tl.Add(t)
  117.             t.Start()
  118.         Next
  119.         Timer1.Start()
  120.     End Sub
  121.     Public Sub PauseDownload()
  122.         On Error Resume Next
  123.         For i As Integer = 0 To tl.Count - 1
  124.             tl(i).Suspend()
  125.         Next
  126.         Timer1.Stop()
  127.         RaiseEvent DownloadPaused(Me, New EventArgs)
  128.     End Sub
  129.     Public Sub ResumeDownload()
  130.         On Error Resume Next
  131.         RaiseEvent DownloadResumed(Me, New EventArgs)
  132.         For i As Integer = 0 To tl.Count - 1
  133.             tl(i).Resume()
  134.         Next
  135.         Timer1.Start()
  136.     End Sub
  137.     Public Sub AbortDownload()
  138.         On Error Resume Next
  139.         For i As Integer = 0 To tl.Count - 1
  140.             tl(i).Abort()
  141.         Next
  142.         Timer1.Stop()
  143.         tl.Clear()
  144.         ds.Close()
  145.         ss.Close()
  146.         sec = 0
  147.         v.Clear()
  148.         RaiseEvent DownloadAborted(Me, New EventArgs)
  149.     End Sub
  150. #End Region
  151. 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:

PM Quote
Avatar
theprogrammer (Normal User)
Guru^2


Messaggi: 2509
Iscritto: 28/01/2009

Segnala al moderatore
Postato alle 13:09
Domenica, 12/07/2009
Scusa ... mi sono perso qualcosa o la domanda era un'altra ? Cosa c'entra la velocità di download ...?

PM Quote
Avatar
riseofapocalypse (Ex-Member)
Pro


Messaggi: 150
Iscritto: 08/07/2009

Segnala al moderatore
Postato alle 13:15
Domenica, 12/07/2009
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:

PM Quote
Avatar
theprogrammer (Normal User)
Guru^2


Messaggi: 2509
Iscritto: 28/01/2009

Segnala al moderatore
Postato alle 13:18
Domenica, 12/07/2009
Testo quotato

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 ...

PM Quote
Avatar
GoLDBeRG (Ex-Member)
Expert


Messaggi: 331
Iscritto: 19/12/2005

Segnala al moderatore
Postato alle 13:21
Domenica, 12/07/2009
quindi mi stai dicendo che se io non invoco il progresschanged la stessa classe webclient va molto piu veloce?

PM Quote
Avatar
riseofapocalypse (Ex-Member)
Pro


Messaggi: 150
Iscritto: 08/07/2009

Segnala al moderatore
Postato alle 13:23
Domenica, 12/07/2009
Beh posso spiegarti tutto! :D scherzi a parte, credo che GoldBerg capisca, visto che ha deciso di aprire questo Thread solo per l'eccessiva lunghezza dell'altro :k:

Se non invochi l'evento ProgressChanged ad ogni ciclo del While, la velocità aumenta sostanzialmente! Prova tu stesso il mio codice :k:

Ultima modifica effettuata da riseofapocalypse il 12/07/2009 alle 13:24
PM Quote
Pagine: [ 1 2 3 4 5 ] Precedente | Prossimo