dcsimg
Results 1 to 4 of 4

Thread: Inet hangs - multiple form instances - trying Winsock

  1. #1

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Inet hangs - multiple form instances - trying Winsock

    Hi

    Here is my app status:

    Purpose - download multiple list files from internet
    Approach - created a simple "download form". After a while, I just needed more forms because I had more than one list of files to download. Just solved that by adding a MDIform to my project, add a button to create another "download form" instance and voilà. Apparently, my problem was solved. But no

    When I press my "download" button in my form-instance3, the other form instances (2 and 1) hangs on downloading the contents (I get a timeout sometimes) until the form-instance3 terminates all the downloads and so on, for all the other forms. So, even though I know my app is not multi-threaded, the multiple instances of the same form are in conflict (Inet component, presumably) and I can't download multiple files at the same time.

    What can I do to solve this? how can I download multiple files at the same time?

    Many thanks
    Last edited by RS_Arm; Oct 4th, 2009 at 12:09 PM. Reason: title change

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Inet hangs - multiple form instances

    I would use the Winsock control since it's asynchronous.

    An easier alternative would be this (it's also asynchronous):
    http://www.vbforums.com/showthread.php?t=532597

  3. #3

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: Inet hangs - multiple form instances

    Hi,

    I'm trying to use Winsock.
    In my case, I have a MSHFlexGrid with 2 collumns: one with URL and the other with the "path+filename" (where the file is going to be saved).
    I'm iterating through all rows calling the next function:

    Code:
    Public Function DownloadSock(ArqURL As String, ArqDestino As String) As Boolean
    'ArqURL is the file URL
    'ArqDestino is where the downloaded file is going to be stored, in my hard disc
    
    Dim arquivo() As Byte
    Dim ficheiroID As Integer
    
    ficheiroID = FreeFile
    On Error GoTo Trata_erro
    Open ArqDestino For Binary Access Write As #ficheiroID
    
    
    Me.Winsock1.Connect ArqURL, 80 
    Me.Winsock1.GetData arquivo()
    Put #ficheiroID, , arquivo()
    
    Close #ficheiroID
    
    DownloadSock = True
    
    
    Exit Function
    
    Trata_erro:
      
        MDIForm1.Text1 = MDIForm1.Text1 & "Error! " & Err.Number & Err.Description & " - " & Err.Source & " - URL: " & ArqURL & " - Destino: " & ArqDestino & vbNewLine
        DownloadSock = False
    
    End Function

    I'm getting this error
    40006
    Wrong protocol or connection state for the requested transaction or request
    What am I doing wrong?

    Thanks

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Inet hangs - multiple form instances

    Quote Originally Posted by RS_Arm View Post
    What am I doing wrong?
    A lot.

    You're getting that error because you're trying to receive data (.GetData) before the Winsock control is connected.

    The .GetData call will go in the _DataArrival() event.

    I posted that link because 1) It's a lot easier than using the Winsock control and requires less code and 2) It doesn't add a dependency on the Winsock control.

    But if you insist, to initiate a download, you'd do something like this:

    Code:
    Option Explicit
    
    Private strSavePath As String
    Private strURI As String
    Private strHost As String
    Private lonPort As Long
    
    Private Function URL_Parse(ByVal URL As String, _
        ByRef Hostname As String, _
        ByRef Port As Long, _
        ByRef URI As String) As Boolean
        
        Dim lonPos As Long
        
        If LCase$(Left$(URL, 7)) = "http://" Then
            URL = Mid$(URL, 8)
        End If
        
        lonPos = InStr(1, URL, "/")
        
        If lonPos > 0 Then
            URI = Mid$(URL, lonPos)
            URL = Left$(URL, lonPos - 1)
        Else
            URI = "/"
        End If
        
        lonPos = InStr(1, URL, ":")
        
        If lonPos > 0 Then
            Hostname = Left$(URL, lonPos - 1)
            Port = CLng(Mid$(URL, lonPos + 1))
        Else
            Hostname = URL
            Port = 80
        End If
        
        URL_Parse = (Len(Hostname) > 0) And (Port > 0)
        
    End Function
    
    Private Sub HTTP_Get(ByRef URL As String, ByRef SavePath As String)
        
        If URL_Parse(URL, strHost, lonPort, strURI) Then
            
            With Winsock1
                .Close
                .RemoteHost = strHost
                .RemotePort = lonPort
                .Connect
            End With
            
            strSavePath = SavePath
            Debug.Print "Connecting to " & strHost & ":" & CStr(lonPort)
        End If
        
    End Sub
    
    Private Sub Command1_Click()
        
        HTTP_Get "www.vbforums.com/favicon.ico", App.Path & "\favicon.ico"
        
    End Sub
    
    'Connected, now we can send GET request
    Private Sub Winsock1_Connect()
        
        Dim strGET As String
        
        strGET = "GET " & IIf(Len(strURI) > 0, strURI, "/") & " HTTP/1.1" & vbCrLf
        strGET = strGET & "Host: " & strHost & vbCrLf
        strGET = strGET & "User-Agent: Visual Basic 6" & vbCrLf
        strGET = strGET & "Accept: *.*" & vbCrLf
        strGET = strGET & "Connection: Close" & vbCrLf & vbCrLf
        
        Winsock1.SendData strGET
        Debug.Print "Sending:"
        Debug.Print strGET
        Debug.Print
        
    End Sub
    
    'Receiving data
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
        
        Dim strData As String
        
        Winsock1.GetData strData, vbString, bytesTotal
        Debug.Print strData
        
    End Sub
    
    Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
        
        Winsock1.Close
        Debug.Print "Connection error: " & Description & IIf(Right$(Description, 1) = ".", "", ".")
        
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width