Results 1 to 2 of 2

Thread: How do I go about this?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    112

    How do I go about this?

    I have a project that works fine as it is, but I would like to "multi-socket" it. It uses winsock, however it goes through a module, I have never seen it this way before. I have the code I will post below which is the original version and then I also have code from where I made the module into a user control that I can show upon request. Here is some code:

    This is the code in the start button:
    Code:
    Dim strsplit() As String
    Dim usernames(999) As String
    Dim passwords(999) As String
    
    Command7.Enabled = True
    blnGo = True
    
    For i = 0 To List1.ListCount - 1
    If blnGo = True Then
    
    List1.ListIndex = i
    
    If InStr(1, List1.Text, ":") = 0 Then
    strsplit(0) = ""
    strsplit(1) = ""
    Label4.Caption = "Bad Login!"
    Else
    strsplit() = Split(List1.List(i), ":")
    usernames(i) = strsplit(0)
    passwords(i) = strsplit(1)
    End If
    
    GetSource "POST", "index.html", "suburl.url.com", "", "", "email=" & strsplit(0) & "&password=" & strsplit(1) & "&Remember=Remember&Loginbutton=Login" ' Login
    
    GetCookies (strSource)
    
    If InStr(1, strSource, "302 Object Moved") Then
    GetSource "GET", "index.html", "Home.url.com", "", cookies, ""
    RichTextBox3.Text = strSource
    If InStr(1, strSource, "My") Then
    List2.AddItem strsplit(0) & ":" & strsplit(1)
    End If
    Else
    Label4.Caption = "Login is bad"
    End If
    RichTextBox1.Text = strSource
    If List1.ListCount - 1 = List1.ListIndex Then
    Label4.Caption = "Done!"
    End If
    End If
    Next i
    This is the code that is in the module:
    Code:
    Sub GetSource(Method As String, Page As String, Host As String, Referer As String, Cookie As String, Data As String)
        strSource = ""
    
        strMethod = Method
        strPage = Page
        strHost = Host
        strReferer = Referer
        strCookie = Cookie
        strData = Data
    
        Form1.sckSource.Close
        Form1.sckSource.Connect Host, 80
        PageLoad
    End Sub
    
    Sub SendData()
    Dim strRequest As String
    
        strRequest = _
        strMethod & " /" & strPage & " HTTP/1.1" & vbCrLf & _
        "Host: " & strHost & vbCrLf & _
        "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4" & vbCrLf & _
        "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" & vbCrLf & _
        "Accept-Language: en,en-us;q=0.5" & vbCrLf & _
        "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" & vbCrLf & _
        "Connection: close" & vbCrLf
    
        If strReferer <> "" Then strRequest = strRequest & "Referer: " & strReferer & vbCrLf
        If strCookie <> "" Then strRequest = strRequest & "Cookie: " & strCookie & vbCrLf
        If strData <> "" Then
            strRequest = strRequest & "Content-Type: application/x-www-form-urlencoded" & vbCrLf
            strRequest = strRequest & "Content-Length: " & Len(strData) & vbCrLf & vbCrLf
            strRequest = strRequest & strData & vbCrLf
        End If
    
        strRequest = strRequest & Chr(10)
        Form1.sckSource.SendData (strRequest)
    End Sub
    
    Sub GetData()
        Dim strIncomming As String
        
        If Form1.sckSource.State = 7 Then
            Form1.sckSource.GetData strIncomming
            strSource = strSource & strIncomming
        End If
    End Sub
    
    Sub PageLoad()
        Now = GetTickCount()
        Do Until Form1.sckSource.State = 8
            If GetTickCount() >= Now + 3000 Then
                GetSource strMethod, strPage, strHost, strReferer, strCookie, strData
                Exit Sub
            End If
    
            If (GetTickCount() - Now) < 0 Then
                Now = GetTickCount()
            End If
    
            DoEvents
            Sleep 1
        Loop
    
    
    
        Form1.sckSource.Close
    End Sub
    Please help me in anyway you can. My question is what is the best method to multi socket this, as in array of winsock.

    PS-I know the URL's are messed up in the code, they're edited out. This is just an outline of the project. Thanks

  2. #2
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: How do I go about this?

    Create a winsock object on your main form, name it as WinSock, and set its Index property to 0. Create a commandbutton also, name it Command1.. it doesnt count, it just for demonstrating the class object termination.

    now, simply include the following.
    Code:
    Option Explicit
    Private cHTTP() As clsHTTP
    Private lInstanceIndex As Long
    
    Private Sub Command1_Click()
      Call TerminateHTTPInstance(lInstanceIndex)
    End Sub
    
    Private Sub Form_Load()
      ReDim cHTTP(0)
      lInstanceIndex = CreateNewHTTPInstance
      
    End Sub
    
    Private Sub TerminateHTTPInstance(lIndex As Long)
      With cHTTP(lIndex)
        'execute the closing sequnce here, for example...
      End With
      
      Set cHTTP(lIndex) = Nothing
    End Sub
    
    Private Function CreateNewHTTPInstance() As Long
    Dim lIndex As Long
      lIndex = UBound(cHTTP) + 1
      
      CreateNewHTTPInstance = lIndex
      
      ReDim Preserve cHTTP(lIndex)
      Set cHTTP(lIndex) = New clsHTTP
      
      With cHTTP(lIndex)
        .SetParent Me, lIndex
        'Now you can configure and execute things from this point ....
      End With
      
    End Function
    The class object (clsHTTP) should look alike this.

    Code:
    Option Explicit
    Private fParentForm As Form, lMyIndex As Long
    
    Public Sub SetParent(ByRef ParentFrm As Form, ByVal lClassIndex As Long)
      Randomize Timer
      Set fParentForm = ParentFrm
      lMyIndex = lClassIndex
      Load fParentForm.Winsock(lMyIndex)
    End Sub
    
    Private Sub Class_Terminate()
      Unload fParentForm.Winsock(lMyIndex)
      Debug.Print "WinSock object removed."
    End Sub
    Now, you can see, that you can easily create a new instance of clsHTTP, by using the CreateNewHTTPInstance. The return value of CreateNewHTTPInstance will be the control array index that you have to store, for later usage. The class creates a new instance of WinSock on your mainform, when calling the SetParent Method.

    You have to extend the class clsHTTP to, with your methods from your module. Dont forget to replace the callings, to point the appropirate winsock object!

    An example:
    Code:
    Sub GetData()
        Dim strIncomming As String
        
        If fParentForm.Winsock(lMyIndex).State = 7 Then
            fParentForm.Winsock(lMyIndex).GetData strIncomming
            strSource = strSource & strIncomming
        End If
    End Sub
    If you dont need this class anymore, just simply call the TerminateHTTPInstance, and provide its index.

Posting Permissions

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



Click Here to Expand Forum to Full Width