PDA

Click to See Complete Forum and Search --> : winsock - sendData & dataArrival - part 2


drivenbywhat
Aug 9th, 2007, 11:45 PM
I started this thread a while ago in reference to why data sent arrived bunched up. In conclusion, it was because that was the way it was meant to be sent and there were two possible solutions to this. Add a special character at the end of the data and then split the data by that special character when it arrived or use a timer.

I use a combination of these two. I wait 3 seconds before sending data again and use the split to get the data seperated. However, I use the sendData 6 times, which means it takes 18 seconds to send the data. I'm now needing to sendData 10 times. Which means it will make my program take 30 seconds to send its data. That's too long. So I'm thinking of just using the split method. However, is it possible in the following scenario:

So here is my question. I know how to send data with special characters & receive the data and split it at the special characters. I am able to do this because I know how many items are being sent. However, what about when the items you are sending are in a listboxes and the listcount is different everytime? Unless I'm over-thinking, using the special character and splitting is out of the question isn't it? I would have to send the data from each listbox as a seperate sendData using timers to make sure data isn't bunched up. I wouldn't be able to "bunch" all the data together and and try to split it at the receiving end because since the listcount is not the same as before. I can't determine which data came from which listbox.

Anyone get what I'm saying?

DigiRev
Aug 10th, 2007, 01:55 AM
No need for any timers or pausing. Just separate each SendData "packet" with a special character at the end and split it up when it arrives.

When sending list data, I usually just separate each list item by a new line (vbCrLf) since chances are, you won't have any new line characters in a list item.

So you would have 3 separaters.

1 is the one you use at the end of your SendData packet.
2nd is the one you use to separate each list item (vbCrLf)
And 3rd is what you use to separate the other info in the packet.

ie:

Dim intLoop As Integer, strPacket As String

strPacket = "LIST" & Chr$(2) & "Another part" & Chr$(2) & "Another part" & Chr$(2)

For intLoop = 0 To List1.ListCount - 1
strPacket = strPacket & List1.List(intLoop) & vbCrLf
Next intLoop

'Remove last new line if it is there.
If Right$(strPacket, 2) = vbCrLf Then strPacket = Mid$(strPacket, 1, Len(strPacket)-2)

strPacket = strPacket & Chr$(4)

Winsock.SendData strPacket

So then your data would look like this:

LIST[2]Another part[2]Another part[2]Item1
Item2
Item3
Item4

Where [2] (Chr$(2)) separates each "parameter" or other piece of information in the packet. And [4] (Chr$(4)) separates each actual packet in case they arrive together.

Splitting by [2] (which is Chr$(2)) would give you:

strInfo() = Split(Data, Chr$(2))
'strInfo(0) = LIST
'strInfo(1) = Another part
'strInfo(2) = Another part
'strInfo(3) = Item1
Item2
Item3
Item4

Then splitting strInfo(3) by vbCrLf would give you the list items.

strList() = Split(strInfo(3), vbCrLf)

Then you can loop through the array and add each item to the list.

Dim intLoop As Integer

For intLoop = 0 To UBound(strList)
List1.AddItem strList(intLoop)
Next intLoop

Erase strInfo
Erase strList

drivenbywhat
Aug 10th, 2007, 03:41 AM
Thanks for the reply DigiRev. I had a feeling you would be monitoring this forum. ;)

Now please be patient with me because I'm slow when it comes to new things. Also, I'm not 100% sure if this would do what I need. I have more than 1 listbox. Not just one. I can do many listboxes with no problem. The problem is trying to send the data from all the listboxes in 1 shot & splitting it at the receiving end in 1 shot. Well, I can send everything in one shot, just don't know how to split it on the other side to the appropriate listboxes. I don't know if your code does this and I'm just not understanding.

Here is part of the code I use to send the items in the listboxes. Don't pay too much attention to the select case. I just use it for my timer that holds a static integer variable to know what is arriving. I use the @ for the special split character:


Select Case x
Case 1 'deck cards for u1
If lstDeckU1.ListCount > 0 Then
For y = 0 To lstDeckU1.ListCount - 1
deckCardsU1 = deckCardsU1 & lstDeckU1.List(y) & "@"
Next y
Else
deckCardsU1 = "e@"
End If

Winsock.SendData deckCardsU1

Case 2 'deck cards for u2
If lstDeckU2.ListCount > 0 Then
For y = 0 To lstDeckU2.ListCount - 1
deckCardsU2 = deckCardsU2 & lstDeckU2.List(y) & "@"
Next y
Else
deckCardsU2 = "e@"
End If

Winsock.SendData deckCardsU2


Here is part of the code I use to receive & split the items in the listboxes:


Select Case x
Case 1 'u1 deck
If unkMsg <> "e@" Then
theUpdates = Split(unkMsg, "@")
lstDeckU1.Clear
For y = 1 To UBound(theUpdates)
lstDeckU1.AddItem theUpdates(y)
Next y
End If

Case 2 'u2 deck
If unkMsg <> "e@" Then
theUpdates = Split(unkMsg, "@")
lstDeckU2.Clear
For y = 1 To UBound(theUpdates)
lstDeckU2.AddItem theUpdates(y)
Next y
End If


Before I gave up and resorted to the above code, this is what I had done for the sending part:


Case 1 'deck cards
If lstDeckU1.ListCount > 0 Then
For y = 0 To lstDeckU1.ListCount - 1
deckCards = deckCards & lstDeckU1.List(y) & "@"
Next y
deckCards = deckCards & "done@"
Else
deckCards = "e@"
End

If lstDeckU2.ListCount > 0 Then
For y = 0 To lstDeckU2.ListCount - 1
deckCards = deckCards & lstDeckU2.List(y) & "@"
Next y
deckCards = deckCards & "done@"
Else
deckCards = deckCards & "e@"
End If


At the receiving part:


Case 1 'decks

theUpdates = Split(unkMsg, "@")
If theUpdates(1) <> "e" Then
lstDeckU1.Clear
For y = 1 To UBound(theUpdates)
If theUpdates(y) <> "done" then
lstDeckU1.AddItem theUpdates(y)
Else
Exit For
End If
Next y
End If

z = y + 1
If theUpdates(z) <> "e"
lstDeckU2.Clear

For x = z To UBound(theUpdates)
If theUpdates(z) <> "done" then
lstDeckU2.AddItem theUpdates(z)
End If
Next x
End If


I think I'm pretty close with the code I wrote but I couldn't finish it. The if/thens & for/nexts got confusing. This code "should" work perfectly for my needs if the listcount for each list had at least 1 item. When the first or both lists are emtpy, which have the string of "e", then I don't know where in the code to insert more code to jump "out" or what code to put period.

Keep in mind I have 6 listboxes. If I'm having trouble with just 2, imagine how much trouble I will have with 6. So I wouldn't mind if I could send the listboxes by 2.

opus
Aug 13th, 2007, 05:11 AM
Your problem is that the receiver doesn't know the number of items that belong to a listbox (or whatever). However since your sender knows that number, why don't you send the number of items before you start to send the items. That way the receiver could start the correct for/next loop using just this number!

DigiRev
Aug 13th, 2007, 12:31 PM
Damn I remember reading your reply and starting an example project but I must have got sidetracked with my projects.

I'll write an example for you today if I can, if not, then tomorrow. Trying to finish up this chat program. :sick:

GiongTo
Aug 19th, 2007, 02:01 AM
I only send data in "Swan plant web" , But I Can't To make Chat online program . T_T . Can You Send Me Simple source Code about The Winsock to Make chat online .

drivenbywhat
Aug 19th, 2007, 02:51 AM
I only send data in "Swan plant web" , But I Can't To make Chat online program . T_T . Can You Send Me Simple source Code about The Winsock to Make chat online .

Your question doesn't have anything to do with mine. You start a new thread if your reply isn't in reference to what is already started in the thread. However, I see that you're new. Here is a link:

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

DigiRev
Aug 19th, 2007, 04:26 AM
Sorry, been real busy and I forgot again. But here's an example.

GiongTo
Aug 19th, 2007, 04:54 AM
HIHI many Thanks

drivenbywhat
Aug 19th, 2007, 10:19 AM
Ahhhh! vb5 and its lack of vb6 built in functions is annoying. I don't have INSTREV function. However, I'm trying to study your code to see how you went about it. I see you used INSTR to find the "/" during data arrival through calling a function.

I know what INSTR does but what does INSTRREV do again? Does it do the same as INSTR but starts from the right side first?

Don't worry about forgetting & taking a while. I don't have an immediate need for the code. Plus the code I have is working, just not as fast as I would like.

DigiRev
Aug 19th, 2007, 02:36 PM
Ahhhh! vb5 and its lack of vb6 built in functions is annoying. I don't have INSTREV function. However, I'm trying to study your code to see how you went about it. I see you used INSTR to find the "/" during data arrival through calling a function.

I know what INSTR does but what does INSTRREV do again? Does it do the same as INSTR but starts from the right side first?

Don't worry about forgetting & taking a while. I don't have an immediate need for the code. Plus the code I have is working, just not as fast as I would like.

Yeah, InStrRev is the opposite of InStr, starts from the end of the string, to find the last occurence of something.

I don't think VB5 has Split() either.

I can modify the example to work without these functions. Are these the only 2 that are missing?

drivenbywhat
Aug 19th, 2007, 02:43 PM
INSTRREV & SPLIT are the ones that I know vb5 doesn't have but I'm sure there a few more that I haven't had to use.