Results 1 to 5 of 5

Thread: Downloading with Winsock array

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Downloading with Winsock array

    Hi, I've used Winsock arrays many times before, but I'm not really sure how to do this. It's a bit complicated, but I'll try to explain it the best I can.

    I have several items in a Listview. To each Listview item belongs a Collection with strings (between 1 and 300). I need to send each string in the first Collection to the server and the server automatically sends data back. This needs to be done with multiple connections.

    After all the strings from the first collection have been sent to the server and all the data the server has sent back is received, then the item in the Listview has to be removed.

    Then all the strings from the second collection have to be send to the server. After all the strings from the second collection have been sent to the server and all the data the server has sent back is received, then that item in the Listview has to be removed... etc, etc, until the end of the Listview.

    Below is some pseudo code. I'm specifically having problems with the "DownloadNextPart" sub.


    vb Code:
    1. Option Explicit
    2.  
    3. Private col(1 To 5) As Collection
    4. Private strData(1 To 4) As String
    5. Private BufferData(1 To 4) As String
    6.  
    7. Private Sub Form_Load()
    8.     Dim i As Integer, x As Integer
    9.    
    10.     For i = 1 To 5
    11.         lvwItems.ListItems.Add , , "Item in Listview: " & i 'several items in the listview
    12.    
    13.         'add strings to the Collections (just an example)
    14.         Set col(i) = New Collection
    15.         For x = 1 To 50
    16.             col(i).Add "string to send to the server"
    17.         Next x
    18.        
    19.     Next i
    20.    
    21. End Sub
    22.  
    23. Private Sub Command1_Click()
    24.     Dim i As Integer
    25.    
    26.     For i = 1 To 4 '4 connections to the server
    27.         Sockets(i).Close
    28.         Sockets(i).Connect "someserver.com", 80
    29.     Next i
    30.  
    31. End Sub
    32.  
    33. Private Sub Sockets_Connect(Index As Integer)
    34.  
    35.     Call DownloadNextPart(Index) 'start downloading when connected to the server
    36.  
    37. End Sub
    38.  
    39. 'I'm not sure how to make this work properly
    40. '????
    41. Private Sub DownloadNextPart(Index As Integer)
    42. Dim i As Long
    43.  
    44.     With lvwItems
    45.         For i = 1 To .ListItems.Count 'loop through listview items
    46.  
    47.             If Sockets(Index).State = sckConnected Then
    48.                
    49.                 'send next string in collection
    50.                 'if there are no more strings to be sent from a collection then
    51.                 'start sending strings from the next collection ????
    52.                 Sockets(Index).SendData col(.ListItems(i).Tag).Item(???????) & vbCrLf
    53.                
    54.                 Exit Sub
    55.             End If
    56.    
    57.         Next i
    58.     End With
    59.    
    60. End Sub
    61.  
    62. Private Sub Sockets_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    63.  
    64.     Sockets(Index).GetData strData(Index), vbString 'get data
    65.  
    66.     BufferData(Index) = BufferData(Index) & strData(Index) 'buffer data
    67.    
    68.    
    69.     If InStr(1, BufferData(Index), "...") Then ' all data has been received
    70.         'do something with it
    71.  
    72.         BufferData(Index) = vbNullString 'clear buffer
    73.     End If
    74.    
    75.     'if this is the returned data from the last string
    76.     'in the collection, then remove Listview item ?????
    77.  
    78.     Call DownloadNextPart(Index) 'download next part
    79. End Sub
    Last edited by Chris001; Jul 25th, 2008 at 01:57 PM.

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Downloading with Winsock array

    Does anybody know how to do this?

  3. #3
    Hyperactive Member Teseng's Avatar
    Join Date
    Sep 2007
    Location
    Tupelo, MS
    Posts
    281

    Re: Downloading with Winsock array

    Take a look at this, it helped me understand a great deal about winsock downloads, DigiRev is a genius at it.

    http://www.vbforums.com/showthread.php?t=514329

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Downloading with Winsock array

    Thanks Teseng, but it's not really Winsock itself I'm having problems with, but the logics of sending all strings with multiple connections and removing Listview items at the correct moment. I looked at the example, but unfortunately it doesn't help me with my problem.

    Below is the code with only one connection and it works fine, but I need to be able to do it with multiple connections. I know how make a Winsock array, but not how to keep track of everything.

    vb Code:
    1. Private col(1 To 1000) As Collection
    2.  
    3. 'col(1) belongs to Listview item 1
    4. 'col(2) belongs to Listview item 2
    5.  
    6. 'because the Listview items are removed and the index of the items change,
    7. 'the correct index is stored in Listview item Tag.
    8.  
    9. Private Sub Sockets_DataArrival(ByVal bytesTotal As Long)
    10.  
    11.     Sockets.GetData strData, vbString 'get data
    12.  
    13.     BufferData = BufferData & strData 'buffer data
    14.    
    15.    
    16.     If InStr(1, BufferData, "...") Then ' all data has been received
    17.        
    18.         col(lvwItems.ListItems(1).Tag).Remove 1 'remove first string from collection
    19.        
    20.        
    21.         'do something with BufferData here
    22.  
    23.        
    24.         'if Collection is empty, then all strings are done
    25.         'remove Listview item
    26.         If col(lvwItems.ListItems(1).Tag).Count = 0 Then
    27.             lvwItems.ListItems.Remove 1
    28.         End If
    29.        
    30.         BufferData = vbNullString 'clear buffer
    31.        
    32.         Sockets.SendData col(lvwItems.ListItems(1).Tag).Item(1) & vbCrLf 'send next string
    33.     End If
    34.  
    35. End Sub

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Downloading with Winsock array

    I'm sure somebody must have an idea how to do something like this.

    <sarcasm>
    Perhaps someone can tell me how to show a Messagebox with "Hello World!" or how to change the caption of a Form?
    </sarcasm>


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