vb@rchiv
VB Classic
VB.NET
ADO.NET
VBA
C#
Schützen Sie Ihre Software vor Software-Piraterie - mit sevLock 1.0 DLL!  
 vb@rchiv Quick-Search: Suche startenErweiterte Suche starten   Impressum  | Datenschutz  | vb@rchiv CD Vol.6  | Shop Copyright ©2000-2025
 
zurück

 Sie sind aktuell nicht angemeldet.Funktionen: Einloggen  |  Neu registrieren  |  Suchen

VB.NET - Ein- und Umsteiger
Re: Resume Download 
Autor: Urot
Datum: 30.03.05 12:15

hiho ich hab ma fix nen kleenes beispiel gemacht is natürlich ohne irgendwelche fehlerbehandlung aber so ca kann man es machen :
Option Strict On
Public Class HttpDownloadEX
    Public Event DownloadFinished()
    Public Event DownloadProgress(ByVal progress As String)
    Public Event DownloadError(reason  as string)
    Private firstpacket As Boolean = True
    Private kb As Integer = 1024
    Private StartTick As Long = 0
    Private DownloadedBytes As Long = 0
    Private Abort As Boolean
    Private Structure STATEOBJECT
        Dim sock As Net.Sockets.Socket
        Const bufSize% = 32768
        Dim buf() As Byte
        Dim fStream As IO.FileStream
        Dim resuming As Boolean
        Dim fSize&
        Dim remoteFileName$
        Dim port%
        Dim rHOST$
        Dim connected As Boolean
        Dim fName$
    End Structure
    Public Sub New(ByVal host As String, ByVal remoteFileName As String, ByVal _
      localFile As String, Optional ByVal port As Integer = 80)
        startdownload(host, remoteFileName, localFile, port)
    End Sub
    Private Sub startdownload(ByVal host As String, ByVal remoteFileName As _
      String, ByVal localFile As String, Optional ByVal port As Integer = 80)
        Dim fSize&
        Dim sObject As New STATEOBJECT
        sObject.fName = localFile
        If IO.File.Exists(localFile) Then sObject.resuming = True
        If IO.File.Exists(localFile) Then fSize = New IO.FileInfo( _
          localFile).Length
        ReDim sObject.buf(sObject.bufSize - 1)
        sObject.fSize = fSize
        sObject.fStream = Nothing
        sObject.port = port
        sObject.remoteFileName = remoteFileName
        sObject.rHOST = host
        sObject.sock = New Net.Sockets.Socket( _
          Net.Sockets.AddressFamily.InterNetwork, _
          Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
        Net.Dns.BeginResolve(host, AddressOf resolvecallback, sObject)
    End Sub
    Protected Sub resolvecallback(ByVal ar As IAsyncResult)
        Dim sobject As STATEOBJECT = CType(ar.AsyncState, STATEOBJECT)
        If Not ar.IsCompleted Then
            Net.Dns.BeginResolve(sobject.rHOST, AddressOf resolvecallback, _
              sobject)
            Exit Sub
        End If
        Dim rhe As Net.IPHostEntry = Net.Dns.EndResolve(ar)
        Dim ipe As New Net.IPEndPoint(rhe.AddressList(0), sobject.port)
        sobject.sock.BeginConnect(ipe, AddressOf connectcallback, sobject)
    End Sub
    Protected Sub connectcallback(ByVal ar As IAsyncResult)
        Dim sobject As STATEOBJECT = CType(ar.AsyncState, STATEOBJECT)
        Try
            sobject.sock.EndConnect(ar)
            sobject.connected = True
            ' hier sind wir nun connected und sollten erstmal unseren request 
            ' senden und natürlich die response empfangen !
            sobject.buf = System.Text.Encoding.Default.GetBytes(HttpRequest( _
              sobject))
            Dim bSent As Integer = sobject.sock.Send(sobject.buf, 0, _
            sobject.buf.Length, Net.Sockets.SocketFlags.None)
            ReDim sobject.buf(sobject.bufSize - 1)
            sobject.sock.BeginReceive(sobject.buf, 0, sobject.bufSize, _
              Net.Sockets.SocketFlags.None, AddressOf readcallback, sobject)
        Catch e As System.Net.Sockets.SocketException
            If e.ErrorCode = 10065 Then
                ' connection timed out ...
                RaiseEvent DownloadError("Connection timed out ...")
                Abort = True
            ElseIf e.ErrorCode = 10061 Then
                ' connection refused ...
                RaiseEvent DownloadError("connection refused ...")
                Abort = True
            Else
                RaiseEvent DownloadError("Connection error ...")
                Abort = True
            End If
        End Try
    End Sub
    ' empfängt daten vom webserver !
    Protected Sub readcallback(ByVal ar As IAsyncResult)
        If Not Abort Then
            Dim sobject As STATEOBJECT = CType(ar.AsyncState, STATEOBJECT)
            Dim bRead% = sobject.sock.EndReceive(ar)
            If bRead = 0 Then
                ' download finished
                RaiseEvent DownloadFinished()
                If Not Nothing Is sobject.sock Then
                    sobject.sock.Close()
                End If
                sobject = Nothing
                Exit Sub
            End If
            processData(sobject, bRead)
            ReDim sobject.buf(sobject.bufSize - 1)
            sobject.sock.BeginReceive(sobject.buf, 0, sobject.bufSize, _
              Net.Sockets.SocketFlags.None, AddressOf readcallback, sobject)
        End If
    End Sub
 
fortsetzung folgt
ahoi
alle Nachrichten anzeigenGesamtübersicht  |  Zum Thema  |  Suchen

 ThemaViews  AutorDatum
Resume Download1.666msdn24.03.05 21:38
Re: Resume Download1.008Moderatorralf_oop25.03.05 09:27
Re: Resume Download920msdn25.03.05 16:03
Re: Resume Download933Moderatorralf_oop25.03.05 16:48
Re: Resume Download888msdn25.03.05 23:09
Re: Resume Download941msdn28.03.05 23:07
Re: Resume Download866Moderatorralf_oop29.03.05 09:23
Re: Resume Download907msdn29.03.05 12:52
Re: Resume Download873d.fack29.03.05 14:21
Re: Resume Download865msdn29.03.05 19:30
Re: Resume Download885Moderatorralf_oop30.03.05 08:24
Re: Resume Download995Urot30.03.05 12:15
Re: Resume Download940Urot30.03.05 12:16
Re: Resume Download1.001Urot30.03.05 14:32
Re: Resume Download873Urot30.03.05 14:39
Re: Resume Download897msdn30.03.05 20:57
Re: Resume Download896Urot30.03.05 21:01
Re: Resume Download957msdn30.03.05 22:30
Re: Resume Download899msdn07.05.05 01:16
Re: Resume Download885msdn07.05.05 01:17
Re: Resume Download885Urot07.05.05 04:13
Re: Resume Download904msdn07.05.05 22:35
Re: Resume Download971msdn07.05.05 22:37
Re: Resume Download807Urot07.05.05 23:48
Re: Resume Download867msdn08.05.05 00:17
Re: Resume Download936msdn08.05.05 20:56
Re: Resume Download815msdn10.05.05 09:32
Re: Resume Download855spike2410.05.05 11:42
Re: Resume Download872msdn10.05.05 13:04
Re: Resume Download997msdn10.05.05 13:15

Sie sind nicht angemeldet!
Um auf diesen Beitrag zu antworten oder neue Beiträge schreiben zu können, müssen Sie sich zunächst anmelden.

Einloggen  |  Neu registrieren

Funktionen:  Zum Thema  |  GesamtübersichtSuchen 

nach obenzurück
 
   

Copyright ©2000-2025 vb@rchiv Dieter Otter
Alle Rechte vorbehalten.
Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein.

Diese Seiten wurden optimiert für eine Bildschirmauflösung von mind. 1280x1024 Pixel