Is it possible to store data that arrives in an array? I know how to send and receive a single string but I would like to send multiple strings and put those strings in an array? Is it possible or does one have to send the strings in one very long string and then split it at the dataArrival event? This is what I have so far:
vb Code:
'for sending the data
'the program has 3 textboxes in array
'loop through each textbox & assign it to string variable array
'send data for each index
Dim x As Integer, a(2) As String
For x = 0 To 2
a(x) = Text1(x).Text
Winsock.SendData a(x)
Next x
'/////////////////////////////////
'for data arrival
'store data in a array and put it in each textbox
Dim x As Integer, a(2) As String
For x = 0 To 2
Winsock.GetData a(x)
Text1(x).Text = a(x)
Next x
Obviously, it sends the data from the 3 textboxes but at the receiving end, it displays them in text1 only because the for/next in dataArrival only fires one time. Basically, I'm making a 2 player card game and after any user goes, I need to update the textboxes for the other player. I need to do it in one command because the user isn't going to be very happy if they have to press SEND over 50 times! I'm just using 3 textboxes at the moment so I can see if it's doable.
[vb5 & starting to move to vb2008] I appreciate the help I get from everyone. Thank you.
After trying something out, I got it to be able to receive data and put it in the correct textbox by adding a static counter. However, it seems that the SEND is only sending once. Shouldn't it be sending 3 times because of the loop, therefore, the RECEIVE should be getting data at 3 different times? Here's the code for the DataArrival:
vb Code:
Static x As Integer
Dim a(1 to 3) As String
x = x + 1
Winsock.GetData a(x)
Text1(x).Text = a(x)
If x = 3 Then x = 0
Technically, it should get the 1st piece of data received and put it in text1(1). When it gets the next piece of data, it should put it in text1(2). However, the SEND seems to be sending it once and all data from text1(1-3) from sending program is being put in text1(1) of receiving program. When I click SEND again, the same thing will happen but the receiving program will put it in text(2). Why is the send not sending the data at 3 different times if the for/next is telling it to sendData 3 times?
Code for SEND:
VB Code:
Dim x As Integer, a(1 To 3) As String
For x = 1 To 3
a(x) = Text1(x).Text
Winsock.SendData a(x)
Next x
[vb5 & starting to move to vb2008] I appreciate the help I get from everyone. Thank you.
As for the "Why" I have no answer, but I've seen the same in my apeplication and ended with a parsing routine that separated the messages that have been sent together. In order to do that your messages would need a specific ending character!
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button Wait, I'm too old to hurry!
As for the "Why" I have no answer, but I've seen the same in my apeplication and ended with a parsing routine that separated the messages that have been sent together. In order to do that your messages would need a specific ending character!
FWIW, the messages are not sent together, they get 'joined' in the buffer on the receiving end.
Can I ask the maximum length of your strings? It seems it would make more sense to send/receive an array of strings.
Opus, I did end up resolving this post. The first way that I got it to work was what you suggested. I figured it was being sent too fast and making the messages all joined together. Or the receiving end couldn't process it fast enough so it put everything together. I figure maybe it had to do with the "rate" at which it was being sent at. Thanks for your input though. At the end of this post I'll state the other way I solved it.
Ccoder, what is FWIW? Perhaps FYI? I couldn't think of the correct term to use but yes it is the buffer being troublesome. I would still think that the receiving end should keep things in the buffer as it sorts them out instead of just saying, take it all. The max length of the strings are no more than 30 characters. However, I have about 25 strings to send out at one time. I had decided the best way to do so was to use an array to store them at before sending, as you can see from my code. Then place them in an array at the receiving end to store them. But the problem was that everything was being received as one long string. The first thing I tried was using SPLIT and that seemed to work but I didn't like the fact that I had to split my data when it technically wasn't a big string and instead it was 25 different strings.
After finding out that splitting would do the job, I decided to see if there could be a different way to send the data. I figured since it's being sent too fast or received too slow, why not use a timer when sending the data? I was so happy when it worked. I set the timer to send it at 1.5 seconds and the receiving program was able to store it in the array that I had for it.
The timer does the job perfectly because this is a text-based & turn-based game I'm working on. So a person updates (sends) the data to the other player and the receiving end gets the new text (strings) updated 1.5 seconds at a time so they can see what has changed.
[vb5 & starting to move to vb2008] I appreciate the help I get from everyone. Thank you.
The buffer is working just the way it supposed to work. It has no idea what bytes are part of what data item. It's up to the application to sort things out. Think of the buffer as a line of people waiting to be seated in a restaurant and the host/hostess (aka your app) calls out "party of 5 for Jones", "party of 3 for Smith", etc.
If you are happy with the way the code is now working then that's great. If you would like to send the entire array with one SendData then just let me know and I'll help you with that.
Ah, so that's what FWIW means. Hey thanks for that analogy of "party of 5 for Jones". It sort of cleared things up although I still think the host should only let one person in at a time since they arrived one by one.
I am happy with the code I have now but I wouldn't mind learning another way. However, I think I know how you would go about it. Is it something like this:
vb Code:
'sending code
For x = 1 To 3
a(x) = text1(x).Text
totalString = totalString & a(x) & ","
'the , is the separator to use split on at receving end
Next x
Winsock.SendData totalString
'receiving code
Winsock.GetData totalString
separateStrings = Split(totalString, ",")
For x = 0 To UBound(separateStrings)
Text1(x + 1).Text = separateStrings(x)
'+1 because I don't have 0 in the array
Next x
[vb5 & starting to move to vb2008] I appreciate the help I get from everyone. Thank you.
Ah, so that's what FWIW means. Hey thanks for that analogy of "party of 5 for Jones". It sort of cleared things up although I still think the host should only let one person in at a time since they arrived one by one.
Well, a restaurant can't control when/how many customers arrive. They just need a system for controlling how they seat (process) them.
I am happy with the code I have now but I wouldn't mind learning another way. However, I think I know how you would go about it. Is it something like this:
vb Code:
'sending code
For x = 1 To 3
a(x) = text1(x).Text
totalString = totalString & a(x) & ","
'the , is the separator to use split on at receving end
Next x
Winsock.SendData totalString
'receiving code
Winsock.GetData totalString
separateStrings = Split(totalString, ",")
For x = 0 To UBound(separateStrings)
Text1(x + 1).Text = separateStrings(x)
'+1 because I don't have 0 in the array
Next x
Actually, I was talking about sending a real array of strings with one SendData.
I have put together a small demo app for you. The form has two Winsock controls, a client and a server. Sends are only in one direction, but are sufficient to demonstrate the concept. The array is passed in a UDT. If you ever want to develop an app that needs to pass various data types, this is the way to go. I have apps that send UDTs that consist of a mix of integers, longs, strings and arrays of other UDTs. These UDTs can be up to 27KB in size and I'm not about to mess around with converting everything to/from delimited strings. I only have to convert the UDT to/from a byte array to send/receive it.
Note that strings in the UDT are fixed length. They must physically reside within the UDT. If you declare them as variable length then the UDT will contain the BSTRs (addresses) of the strings and the UDT's length would be 12 bytes rather than 90. And who knows what might reside at those addresses on the receiving end.
Well that didn't go so well. First, it wasn't in VB5 so I got errors loading the project. So I had to open up the form only & then add winsock since it gave me pictureboxes for it instead. Then when I ran it, it crashed VB5, lol. I pressed the send button once and nothing happened. I pressed it again and that's when it crashed. I know if one tries to connect while already connected it gives an error but it never crashed vb5 completely, the program would just error out and highlight the error.
I did manage to look at some of your code before running it. It seemed complicated. Thanks for trying to help though.
[vb5 & starting to move to vb2008] I appreciate the help I get from everyone. Thank you.
I noticed that you are running VB5 a couple of days ago but forgot about that little detail when I created the demo app. I would convert it to VB5, but my only system with VB5 installed at home is in storage while we do some major remodeling. Maybe someone reading this has VB5 and can lend a hand.
Hey ccoder, it's all right. I just wanted to see another way of doing things. It's not critical for me to know it since I have it working a different way.
If possible, could you take a look at my new post titled "connecting over internet - client/server im"
[vb5 & starting to move to vb2008] I appreciate the help I get from everyone. Thank you.