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 - HTTPwebrequest
Forum - C# / VB.NET - HTTPwebrequest - Pagina 3

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


Messaggi: 331
Iscritto: 19/12/2005

Segnala al moderatore
Postato alle 20:05
Giovedì, 09/07/2009
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

Codice sorgente - presumibilmente VB.NET

  1. Dim quanti As Integer
  2.     Dim down As NetworkStream
  3.     Dim sock As Socket
  4.     Dim buff(8192) As Byte
  5.  
  6.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  7.         Dim thr As New Thread(AddressOf scarica)
  8.         thr.Start()
  9.     End Sub
  10.  
  11.     Public Sub scarica()
  12.         sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
  13.         sock.Connect("www.artfiles.org", 80)
  14.         down = New NetworkStream(sock)
  15.         Dim byt() As Byte = Encoding.ASCII.GetBytes("GET http://www.artfiles.org/knoppix/KNOPPIX_V5.1.1CD-2007-01-04-EN.iso" & vbCrLf)
  16.         sock.BeginReceive(buff, 0, buff.Length, SocketFlags.None, New AsyncCallback(AddressOf anco), Nothing)
  17.         down.Write(byt, 0, byt.Length)
  18.         down.Flush()
  19.     End Sub
  20.  
  21.     Public Sub anco(ByVal ar As IAsyncResult)
  22.         Dim int As Integer = sock.EndReceive(ar)
  23.         If int < 1 Then
  24.             End
  25.         End If
  26.         quanti += int
  27.         sock.BeginReceive(buff, 0, buff.Length, SocketFlags.None, New AsyncCallback(AddressOf anco), Nothing)
  28.     End Sub
  29.  
  30.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  31.         Try
  32.             Label2.Text = Int((quanti / 1024) / 5)
  33.             quanti = 0
  34.         Catch ex As Exception
  35.  
  36.         End Try
  37.     End Sub


Ultima modifica effettuata da GoLDBeRG il 09/07/2009 alle 20:06
PM Quote
Avatar
GoLDBeRG (Ex-Member)
Expert


Messaggi: 331
Iscritto: 19/12/2005

Segnala al moderatore
Postato alle 20:20
Giovedì, 09/07/2009
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?

PM Quote
Avatar
riseofapocalypse (Ex-Member)
Pro


Messaggi: 150
Iscritto: 08/07/2009

Segnala al moderatore
Postato alle 20:59
Giovedì, 09/07/2009
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:

PM Quote
Avatar
GoLDBeRG (Ex-Member)
Expert


Messaggi: 331
Iscritto: 19/12/2005

Segnala al moderatore
Postato alle 21:38
Giovedì, 09/07/2009
il problema sono proprio le parti uguali.... se nn è divisibile per 10?? si fa 11?? capito il problema.....

PM Quote
Avatar
riseofapocalypse (Ex-Member)
Pro


Messaggi: 150
Iscritto: 08/07/2009

Segnala al moderatore
Postato alle 21:52
Giovedì, 09/07/2009
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
Codice sorgente - presumibilmente VB.NET

  1. Class Download
  2. #Region "Events"
  3.     Public Event DownloadStarted As EventHandler
  4.     Public Event DownloadCompleted As EventHandler
  5.     Public Event DownloadProgressChanged As EventHandler
  6.     Public Event DownloadAborted As EventHandler
  7.     Public Event DownloadPaused As EventHandler
  8.     Public Event DownloadResumed As EventHandler
  9. #End Region
  10. #Region "Attributes"
  11.     Private s, d As String
  12.     Private b, sec, v As Decimal
  13.     Private nt As Decimal
  14.     Private t() As Threading.Thread
  15.     Private ss As IO.BufferedStream, ds As IO.FileStream, dst As DownStatus
  16.     Private WithEvents tim As New Timer With {.Interval = 1000}
  17. #End Region
  18. #Region "Enums"
  19.     Enum DownStatus
  20.         NoOperation = 0
  21.         Downloading
  22.         Paused
  23.         Completed
  24.     End Enum
  25. #End Region
  26. #Region "Constructors"
  27.     Public Sub New(ByVal url As String, ByVal filename As String, ByVal threadnumber As Decimal)
  28.         s = url
  29.         d = filename
  30.         b = 0
  31.         sec = 0
  32.         nt = threadnumber
  33.         Array.Resize(t, nt)
  34.         For i As Integer = 0 To nt - 1
  35.             t(i) = New Threading.Thread(AddressOf Download)
  36.         Next
  37.         ss = Nothing
  38.         ds = Nothing
  39.         dst = DownStatus.NoOperation
  40.     End Sub
  41. #End Region
  42. #Region "Properties"
  43.     Public Property Source() As String
  44.         Get
  45.             Return s
  46.         End Get
  47.         Set(ByVal value As String)
  48.             s = value
  49.         End Set
  50.     End Property
  51.     Public Property Destination() As String
  52.         Get
  53.             Return d
  54.         End Get
  55.         Set(ByVal value As String)
  56.             d = value
  57.         End Set
  58.     End Property
  59.     Public ReadOnly Property DownloadedBytes() As Decimal
  60.         Get
  61.             Return b
  62.         End Get
  63.     End Property
  64.     Public ReadOnly Property Seconds() As Decimal
  65.         Get
  66.             Return sec
  67.         End Get
  68.     End Property
  69.     Public ReadOnly Property Speed() As Decimal
  70.         Get
  71.             Return v
  72.         End Get
  73.     End Property
  74.     Public ReadOnly Property DownloadStatus() As DownStatus
  75.         Get
  76.             Return dst
  77.         End Get
  78.     End Property
  79. #End Region
  80. #Region "Public methods"
  81.     Public Sub StartDownload()
  82.         ss = New IO.BufferedStream(Net.WebRequest.Create(s).GetResponse.GetResponseStream)
  83.         ds = New IO.FileStream(d, IO.FileMode.OpenOrCreate)
  84.         For i As Integer = 0 To nt - 1
  85.             t(i).Start()
  86.         Next
  87.         tim.Start()
  88.         dst = DownStatus.Downloading
  89.         RaiseEvent DownloadStarted(Me, New EventArgs)
  90.     End Sub
  91.     Public Sub AbortDownload()
  92.         dst = DownStatus.NoOperation
  93.         For i As Integer = 0 To nt - 1
  94.             t(i).Abort()
  95.         Next
  96.         tim.Stop()
  97.         ds.Close()
  98.         ss.Close()
  99.         RaiseEvent DownloadAborted(Me, New EventArgs)
  100.     End Sub
  101.     Public Sub PauseDownload()
  102.         dst = DownStatus.Paused
  103.         For i As Integer = 0 To nt - 1
  104.             t(i).Suspend()
  105.         Next
  106.         tim.Stop()
  107.         RaiseEvent DownloadPaused(Me, New EventArgs)
  108.     End Sub
  109.     Public Sub ResumeDownload()
  110.         tim.Start()
  111.         For i As Integer = 0 To nt - 1
  112.             t(i).Resume()
  113.         Next
  114.         dst = DownStatus.Downloading
  115.         RaiseEvent DownloadResumed(Me, New EventArgs)
  116.     End Sub
  117. #End Region
  118. #Region "Private methods"
  119.     Private Sub Download()
  120.         While True
  121.             Try
  122.                 ds.WriteByte(ss.ReadByte)
  123.             Catch
  124.                 Exit While
  125.             End Try
  126.             b += 1
  127.             Application.DoEvents()
  128.             RaiseEvent DownloadProgressChanged(Me, New EventArgs)
  129.         End While
  130.         tim.Stop()
  131.         ds.Close()
  132.         ss.Close()
  133.         dst = DownStatus.Completed
  134.         nt -= 1
  135.         If nt = 0 Then RaiseEvent DownloadCompleted(Me, New EventArgs)
  136.     End Sub
  137. #End Region
  138. #Region "Private events"
  139.     Private Sub tim_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tim.Tick
  140.         sec += 1
  141.         v = b / sec
  142.     End Sub
  143. #End Region
  144. 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 il 09/07/2009 alle 21:55
PM Quote
Avatar
GoLDBeRG (Ex-Member)
Expert


Messaggi: 331
Iscritto: 19/12/2005

Segnala al moderatore
Postato alle 9:22
Venerdì, 10/07/2009
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...

PM Quote
Avatar
riseofapocalypse (Ex-Member)
Pro


Messaggi: 150
Iscritto: 08/07/2009

Segnala al moderatore
Postato alle 9:48
Venerdì, 10/07/2009
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

PM Quote
Avatar
GoLDBeRG (Ex-Member)
Expert


Messaggi: 331
Iscritto: 19/12/2005

Segnala al moderatore
Postato alle 10:07
Venerdì, 10/07/2009
eh ma è dove inizia a leggere il socket il problema....
se tu assegni a tutti 0 all'inizio facciamo un esempio...
socket
socket 2 e socket 3
tutti e tre da offset 0....

socket 1 fa il primo byte
intanto anche socket 2 e tre avevano fatto il primo byte...
cosi passano entrambi al 2... ma anche il primo è passato al secondo... e mentre il primo passa al 2 uno degli altri 2 sarà passato al terzo....non funziona cosi... ogni socket deve scaricare la sua e solo la sua partizione di file....in modo da non interferire con l'offset degli altri....
cmq la dimensione del file si puo sapere a priori guarda


Sample

Below is a sample conversation between an HTTP client and an HTTP server running on www.example.com, port 80.

Client request (followed by a blank line, so that request ends with a double newline, each in the form of a carriage return followed by a line feed):

GET /index.html HTTP/1.1
Host: www.example.com


The "Host" header distinguishes between various DNS names sharing a single IP address, allowing name-based virtual hosting. While optional in HTTP/1.0, it is mandatory in HTTP/1.1.

Server response (followed by a blank line and text of the requested page):

HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Server: Apache/1.3.3.7 (Unix)  (Red-Hat/Linux)
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Etag: "3f80f-1b6-3e1cb03b"
Accept-Ranges: bytes
Content-Length: 438
Connection: close
Content-Type: text/html; charset=UTF-8

The ETag (entity tag) header is used to determine if a cached version of the requested resource is identical to the current version of the resource on the server. Content-Type specifies the Internet media type of the data conveyed by the http message, while Content-Length indicates its length in bytes. The HTTP/1.1 webserver publishes its ability to respond to requests for certain byte ranges of the document by setting the header Accept-Ranges: bytes. This is useful if the client needs to have only certain portions[8] of a resource sent by the server, which is called byte serving. When Connection: close is sent in a header, it means that the web server will close the TCP connection immediately after the transfer of this package.


solo che non capisco perche io non ricevo quella roba dove ce il content lenght... a me quello serve.....

PM Quote
Pagine: [ 1 2 3 4 5 ] Precedente | Prossimo