|
-
Oct 22nd, 2006, 11:45 PM
#1
Thread Starter
Lively Member
ReceiveData
Ok to learn im working on a chat program. It sends to all clients on and I have it sending multiple things but I want to organize it better so I can expand.
I have it sending info like this right now...
VB Code:
Winsock.SendData txtNameBox.Text & ": " & txtchatBox.Text
My question is when its received how do you set them all to their own variable rather than all being in the same variable. This would be so I could display them all different places or whatever.
Such as Winsock.GetData Var1
Winsock.GetData Var2
Winsock.GetData Var3
Im sure thats wrong but any helpw ould be appreciated. THanks.
-
Oct 22nd, 2006, 11:59 PM
#2
Lively Member
Re: ReceiveData
In the winsock recive data section
VB Code:
dim TempStr as string
dim TempArr() as string
dim Username as string
dim ChatBox as string
Winsock.GetData Var1
temparr() = split(TempStr,":") ' where ":" is the delimiter
Username = TempArr(0)
ChatBox = TempArr(1)
Hope this helps!
-
Oct 23rd, 2006, 12:04 AM
#3
Thread Starter
Lively Member
Re: ReceiveData
 Originally Posted by putta
In the winsock recive data section
VB Code:
dim TempStr as string
dim TempArr() as string
dim Username as string
dim ChatBox as string
Winsock.GetData Var1
temparr() = split(TempStr,":") ' where ":" is the delimiter
Username = TempArr(0)
ChatBox = TempArr(1)
Hope this helps! 
So there you have ":" splitting the data, correct?
Also I would like to set it up to sendpackets but no experience with that. So instead of sending all data at once it sends when you call the packet.
-
Oct 23rd, 2006, 12:08 AM
#4
Re: ReceiveData
 Originally Posted by HaLLa
Ok to learn im working on a chat program. It sends to all clients on and I have it sending multiple things but I want to organize it better so I can expand.
I have it sending info like this right now...
VB Code:
Winsock.SendData txtNameBox.Text & ": " & txtchatBox.Text
My question is when its received how do you set them all to their own variable rather than all being in the same variable. This would be so I could display them all different places or whatever.
Such as Winsock.GetData Var1
Winsock.GetData Var2
Winsock.GetData Var3
Im sure thats wrong but any helpw ould be appreciated. THanks.
To answer your question, you could just put:
VB Code:
Winsock1.GetData Var1
Dim strSplit() As String, strName As String, strMsg As String
'Split the data seperated by ":"
strSplit() = Split(Var1, ":")
'Name
strName = strSplit(0)
'Message
strMsg = strSplit(1)
TxtChat.Text = strName & ": " & strMsg
But since you mentioned wanting to expand, I think you should send a header along with that packet to let the clients know that the data is a chat message.
You should also add a delimiter to the end of the packet. For example:
VB Code:
Winsock.SendData "MSG|" & txtNameBox.Text & "|" & txtChatBox.Text & Chr$(0)
(just a basic example)
Now, when the data is received, you can have it setup like this:
VB Code:
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
'"MSG|" & txtNameBox.Text & "|" & txtChatBox.Text & Chr$(0)
Dim strData As String, lonPos As Long
Dim strCommand As String, strBuff() As String
Dim lonLoop As Long, strSegments() As String
'Get entire data.
Winsock1.GetData strData
'Search for the end of packet delimiter (Chr$(0))
lonPos = InStr(1, strData, Chr$(0))
If lonPos > 0 Then
'End of packet found. Loop through all packets.
strBuff() = Split(strData, Chr$(0))
For lonLoop = 0 To UBound(strBuff())
If Len(strBuff(lonLoop)) > 0 Then
'Split the data into segments.
'MSG|NameBox|ChatBox becomes
'(0) - MSG
'(1) - NameBox
'(2) - ChatBox
strSegments() = Split(strBuff(lonLoop), "|")
'Check the command (packet header).
Select Case strSegments(0)
Case "MSG"
'This data is a chat message.
textchat.Text = strSegments(1) & ": " & strSegments(2)
Case "..."
'Another command here.
End Select
End If
Next lonLoop
Else
If InStr(1, strData, "|") > 0 Then
strSegments = Split(strData, "|")
'Same select case statement as in the loop.
'Check the command (packet header).
Select Case strSegments(0)
Case "MSG"
'This data is a chat message.
textchat.Text = strSegments(1) & ": " & strSegments(2)
Case "..."
'Another command here.
End Select
End If
End Sub
I didn't test that code, so I don't know if it works or not, but a setup like that will make it a lot easier when you add more to your chat program than just standard text messages.
-
Oct 23rd, 2006, 04:09 AM
#5
Re: ReceiveData
It's best to let the DataArrival event finish as quickly as possible.. so rather than doing everything in there, just do the segragation into packets and add the received data into a collection of packets then enable a timer. The timer will then continue the handling of the collection, eg: transfer to interface, or senddata to other clients to broadcast msg, etc.
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
|