Results 1 to 8 of 8

Thread: winsock listbox send

  1. #1

    Thread Starter
    Hyperactive Member Rattled_Cage's Avatar
    Join Date
    Dec 2005
    Posts
    315

    winsock listbox send

    im using this to send all the data from a list box
    to populate another list box to a remote ip

    Code:
     For x = 0 To hosts.ListCount - 1
         Server.SendData hosts.List(x), SckIndex
    '''''
    Next x
    The problem is when ariving its all being entered as one long line

    Code:
    Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
        Dim Data As String
        Call Winsock.GetData(Data, , bytesTotal)
        txtResponse.Text = Data
        connects.AddItem txtResponse.Text
        txtResponse.Text = ""
    End Sub
    how do i get it to regonise the next listbox add to the next line

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: winsock listbox send

    I have no idea whether this will work and no way to test it, but give this a shot
    Code:
    Server.SendData hosts.List(x), SckIndex & vbCrLf

  3. #3

    Thread Starter
    Hyperactive Member Rattled_Cage's Avatar
    Join Date
    Dec 2005
    Posts
    315

    Re: winsock listbox send

    naw didnt work just added some Squares when sent ty anyway

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: winsock listbox send

    Those "squares" are end-of-line marks. You have to split the string at the ends of line at the receiving end.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  5. #5

    Thread Starter
    Hyperactive Member Rattled_Cage's Avatar
    Join Date
    Dec 2005
    Posts
    315

    Re: winsock listbox send

    ok the datas now in a textbox when it arives with "####" inbetween each item
    anyone provide a split string to help out adding them to a list box


    hosts.AddItem Split(x, "####")(0) etc

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

    Re: winsock listbox send

    Quote Originally Posted by Rattled_Cage
    ok the datas now in a textbox when it arives with "####" inbetween each item
    anyone provide a split string to help out adding them to a list box


    hosts.AddItem Split(x, "####")(0) etc
    I'm assuming 'x' is the data that is received.
    Code:
        Dim x As String, strBuffer() As String
        Dim intLoop As Integer
        
        strBuffer() = Split(x, "####")
        
        With hosts
            .Clear 'Clear previous items.
            
            For intLoop = 0 To UBound(strBuffer())
                If Len(strBuffer(intLoop)) > 0 Then
                    .AddItem strBuffer(intLoop)
                End If
            Next intLoop
        End With
    You're probably better off using vbCrLf as a delimiter since it's shorter and more unlikely that it will appear in a ListBox.

  7. #7
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: winsock listbox send

    If the received data is one delimited string you should construct this string yourself, instead of looping the .senddata method. When you loop the senddata method it usually gets stuck onto the queue and ends up as one string received like yours is. But occasionally you might get two packets received and this might cause problems depending on your logic a the receiving end.

    So what I'm trying to say is keep the loop in place but instead I'd construct the string at that point e.g.

    for...
    strPacket = strPacket & hosts.List(x) & vbCrlf
    next

    ws.Senddata strPacket

    So what it does it make the string in the loop, then send it as one packet. Sorry if this wasn't too clear

    Ps: i agree with DigiRev: use vbCrLf as a delimiter
    Chris

  8. #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