Results 1 to 5 of 5

Thread: ReceiveData

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    65

    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:
    1. 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.

  2. #2
    Lively Member putta's Avatar
    Join Date
    Oct 2004
    Location
    Original Citizen Of This Planet
    Posts
    86

    Re: ReceiveData

    In the winsock recive data section

    VB Code:
    1. dim TempStr as string
    2. dim TempArr() as string
    3. dim Username as string
    4. dim ChatBox as string
    5.  
    6. Winsock.GetData Var1
    7.  
    8. temparr() = split(TempStr,":") ' where ":" is the delimiter
    9.  
    10. Username = TempArr(0)
    11. ChatBox = TempArr(1)


    Hope this helps!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    65

    Re: ReceiveData

    Quote Originally Posted by putta
    In the winsock recive data section

    VB Code:
    1. dim TempStr as string
    2. dim TempArr() as string
    3. dim Username as string
    4. dim ChatBox as string
    5.  
    6. Winsock.GetData Var1
    7.  
    8. temparr() = split(TempStr,":") ' where ":" is the delimiter
    9.  
    10. Username = TempArr(0)
    11. 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.

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: ReceiveData

    Quote 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:
    1. 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:
    1. Winsock1.GetData Var1
    2.  
    3. Dim strSplit() As String, strName As String, strMsg As String
    4.  
    5. 'Split the data seperated by ":"
    6. strSplit() = Split(Var1, ":")
    7.  
    8. 'Name
    9. strName = strSplit(0)
    10. 'Message
    11. strMsg = strSplit(1)
    12.  
    13. 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:
    1. 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:
    1. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    2.     '"MSG|" & txtNameBox.Text & "|" & txtChatBox.Text & Chr$(0)
    3.     Dim strData As String, lonPos As Long
    4.     Dim strCommand As String, strBuff() As String
    5.     Dim lonLoop As Long, strSegments() As String
    6.    
    7.     'Get entire data.
    8.     Winsock1.GetData strData
    9.    
    10.     'Search for the end of packet delimiter (Chr$(0))
    11.     lonPos = InStr(1, strData, Chr$(0))
    12.    
    13.     If lonPos > 0 Then
    14.         'End of packet found. Loop through all packets.
    15.         strBuff() = Split(strData, Chr$(0))
    16.        
    17.         For lonLoop = 0 To UBound(strBuff())
    18.            
    19.             If Len(strBuff(lonLoop)) > 0 Then
    20.                 'Split the data into segments.
    21.                 'MSG|NameBox|ChatBox becomes
    22.                
    23.                 '(0) - MSG
    24.                 '(1) - NameBox
    25.                 '(2) - ChatBox
    26.                 strSegments() = Split(strBuff(lonLoop), "|")
    27.                
    28.                 'Check the command (packet header).
    29.                 Select Case strSegments(0)
    30.                
    31.                     Case "MSG"
    32.                         'This data is a chat message.
    33.                         textchat.Text = strSegments(1) & ": " & strSegments(2)
    34.                    
    35.                     Case "..."
    36.                         'Another command here.
    37.                        
    38.                 End Select
    39.            
    40.             End If
    41.        
    42.         Next lonLoop
    43.    
    44.     Else
    45.        
    46.         If InStr(1, strData, "|") > 0 Then
    47.             strSegments = Split(strData, "|")
    48.            
    49.             'Same select case statement as in the loop.
    50.             'Check the command (packet header).
    51.             Select Case strSegments(0)
    52.            
    53.                 Case "MSG"
    54.                     'This data is a chat message.
    55.                     textchat.Text = strSegments(1) & ": " & strSegments(2)
    56.                
    57.                 Case "..."
    58.                     'Another command here.
    59.                        
    60.             End Select
    61.        
    62.     End If
    63.    
    64. 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.

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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
  •  



Click Here to Expand Forum to Full Width