Results 1 to 8 of 8

Thread: winsock listbox send

Threaded View

  1. #8
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: winsock listbox send

    If I were to do this, I would do it like this:

    (Please note that I wrote this code of the top of my head, I did not test any of it)

    To send data:
    vb Code:
    1. Private Sub cmdSendData_Click()
    2.     Dim DataArr() As String
    3.     Dim DataStr As String
    4.     Dim Delimiter As String
    5.     Dim X As Long
    6.    
    7.     ' put the data into an array
    8.     ReDim DataArr(Hosts.ListCount - 1)
    9.    
    10.     For X = 0 To UBound(DataArr)
    11.          DataArr(K) = Hosts.List(X)
    12.     Next X
    13.    
    14.     ' join all data without a delimiter
    15.     DataStr = Join(DataArr, "")
    16.    
    17.     ' find a character (delimiter) that is NOT used in the data
    18.     For X = 1 To 254
    19.         If InStr(1, DataStr, Chr(X)) = 0 Then Exit For
    20.     Next X
    21.    
    22.     Delimiter = Chr(X)
    23.     ' join the data using the delimiter, and set it also as first character
    24.     DataStr = Delimiter & Join(DataArr, Delimiter)
    25.    
    26.     ' send the length of the data, following the actual data
    27.     DataStr = Right("00000000" & Hex(Len(DataStr)), 8) & DataStr
    28.    
    29.     ' send the whole thing...
    30.     Winsock1.SendData DataStr
    31. End Sub
    To receive data:
    vb Code:
    1. Option Explicit
    2.  
    3. Private DataLength As Long, DataBuffer As String
    4.  
    5. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    6.     Dim strData As String
    7.    
    8.     Winsock1.GetData strData
    9.    
    10. MoreDataToProcess:
    11.    
    12.     ' if DataLength = 0, it means this is the start of the data
    13.     If DataLength = 0 Then
    14.         ' read the data length, so we know how much to expect
    15.         DataLength = Val("&H" & Left(strData, 8))
    16.        
    17.         ' save the data into a buffer, excluding the data length
    18.         DataBuffer = Mid(strData, 9)
    19.     Else
    20.         ' append data to the buffer
    21.         DataBuffer = DataBuffer & strData
    22.        
    23.         ' if we received the whole data or more...
    24.         If Len(DataBuffer) >= DataLength Then
    25.             ' call the function to process the data
    26.             Received_All_Data Left(DataBuffer, DataLength)
    27.            
    28.             ' if we received more data, process the rest of the data
    29.             If Len(DataBuffer) > DataLength Then
    30.                 ' read the rest of the data
    31.                 strData = Mid(DataBuffer, DataLength + 1)
    32.                 DataBuffer = ""
    33.                 DataLength = 0
    34.                
    35.                 ' jump to the beginning of the function to start the process all over
    36.                 GoTo MoreDataToProcess
    37.             End If
    38.         End If
    39.     End If
    40. End Sub
    41.  
    42. Private Sub Received_All_Data(ByVal strData As String)
    43.     Dim CharDelimiter As String
    44.     Dim DataLines() As String
    45.     Dim K As Long
    46.    
    47.     ' read the delimiter
    48.     CharDelimiter = Left(strData, 1)
    49.    
    50.     ' split the data by the delimiter (ommiting the first character the delimter)
    51.     DataLines = Split(Mid(strData, 2), CharDelimiter)
    52.    
    53.     ' put the data back into a list
    54.     List1.Clear
    55.     For K = 0 To UBound(DataLines)
    56.         List1.AddItem DataLines(K)
    57.     Next K
    58. End Sub
    Last edited by CVMichael; May 18th, 2007 at 03:20 PM.

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