|
-
Jul 25th, 2008, 01:52 PM
#1
Thread Starter
Frenzied Member
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:
Option Explicit
Private col(1 To 5) As Collection
Private strData(1 To 4) As String
Private BufferData(1 To 4) As String
Private Sub Form_Load()
Dim i As Integer, x As Integer
For i = 1 To 5
lvwItems.ListItems.Add , , "Item in Listview: " & i 'several items in the listview
'add strings to the Collections (just an example)
Set col(i) = New Collection
For x = 1 To 50
col(i).Add "string to send to the server"
Next x
Next i
End Sub
Private Sub Command1_Click()
Dim i As Integer
For i = 1 To 4 '4 connections to the server
Sockets(i).Close
Sockets(i).Connect "someserver.com", 80
Next i
End Sub
Private Sub Sockets_Connect(Index As Integer)
Call DownloadNextPart(Index) 'start downloading when connected to the server
End Sub
'I'm not sure how to make this work properly
'????
Private Sub DownloadNextPart(Index As Integer)
Dim i As Long
With lvwItems
For i = 1 To .ListItems.Count 'loop through listview items
If Sockets(Index).State = sckConnected Then
'send next string in collection
'if there are no more strings to be sent from a collection then
'start sending strings from the next collection ????
Sockets(Index).SendData col(.ListItems(i).Tag).Item(???????) & vbCrLf
Exit Sub
End If
Next i
End With
End Sub
Private Sub Sockets_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Sockets(Index).GetData strData(Index), vbString 'get data
BufferData(Index) = BufferData(Index) & strData(Index) 'buffer data
If InStr(1, BufferData(Index), "...") Then ' all data has been received
'do something with it
BufferData(Index) = vbNullString 'clear buffer
End If
'if this is the returned data from the last string
'in the collection, then remove Listview item ?????
Call DownloadNextPart(Index) 'download next part
End Sub
Last edited by Chris001; Jul 25th, 2008 at 01:57 PM.
-
Jul 26th, 2008, 10:23 AM
#2
Thread Starter
Frenzied Member
Re: Downloading with Winsock array
Does anybody know how to do this?
-
Jul 26th, 2008, 04:01 PM
#3
Hyperactive Member
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
-
Jul 27th, 2008, 09:05 AM
#4
Thread Starter
Frenzied Member
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:
Private col(1 To 1000) As Collection
'col(1) belongs to Listview item 1
'col(2) belongs to Listview item 2
'because the Listview items are removed and the index of the items change,
'the correct index is stored in Listview item Tag.
Private Sub Sockets_DataArrival(ByVal bytesTotal As Long)
Sockets.GetData strData, vbString 'get data
BufferData = BufferData & strData 'buffer data
If InStr(1, BufferData, "...") Then ' all data has been received
col(lvwItems.ListItems(1).Tag).Remove 1 'remove first string from collection
'do something with BufferData here
'if Collection is empty, then all strings are done
'remove Listview item
If col(lvwItems.ListItems(1).Tag).Count = 0 Then
lvwItems.ListItems.Remove 1
End If
BufferData = vbNullString 'clear buffer
Sockets.SendData col(lvwItems.ListItems(1).Tag).Item(1) & vbCrLf 'send next string
End If
End Sub
-
Jul 28th, 2008, 11:55 AM
#5
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|