|
-
May 17th, 2007, 01:19 PM
#1
Thread Starter
Hyperactive Member
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
-
May 17th, 2007, 01:27 PM
#2
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
-
May 17th, 2007, 01:45 PM
#3
Thread Starter
Hyperactive Member
Re: winsock listbox send
naw didnt work just added some Squares when sent ty anyway
-
May 17th, 2007, 02:16 PM
#4
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
-
May 17th, 2007, 03:19 PM
#5
Thread Starter
Hyperactive Member
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
-
May 17th, 2007, 04:02 PM
#6
Re: winsock listbox send
 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.
-
May 17th, 2007, 04:56 PM
#7
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
-
May 18th, 2007, 07:57 AM
#8
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:
Private Sub cmdSendData_Click()
Dim DataArr() As String
Dim DataStr As String
Dim Delimiter As String
Dim X As Long
' put the data into an array
ReDim DataArr(Hosts.ListCount - 1)
For X = 0 To UBound(DataArr)
DataArr(K) = Hosts.List(X)
Next X
' join all data without a delimiter
DataStr = Join(DataArr, "")
' find a character (delimiter) that is NOT used in the data
For X = 1 To 254
If InStr(1, DataStr, Chr(X)) = 0 Then Exit For
Next X
Delimiter = Chr(X)
' join the data using the delimiter, and set it also as first character
DataStr = Delimiter & Join(DataArr, Delimiter)
' send the length of the data, following the actual data
DataStr = Right("00000000" & Hex(Len(DataStr)), 8) & DataStr
' send the whole thing...
Winsock1.SendData DataStr
End Sub
To receive data:
vb Code:
Option Explicit
Private DataLength As Long, DataBuffer As String
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData
MoreDataToProcess:
' if DataLength = 0, it means this is the start of the data
If DataLength = 0 Then
' read the data length, so we know how much to expect
DataLength = Val("&H" & Left(strData, 8))
' save the data into a buffer, excluding the data length
DataBuffer = Mid(strData, 9)
Else
' append data to the buffer
DataBuffer = DataBuffer & strData
' if we received the whole data or more...
If Len(DataBuffer) >= DataLength Then
' call the function to process the data
Received_All_Data Left(DataBuffer, DataLength)
' if we received more data, process the rest of the data
If Len(DataBuffer) > DataLength Then
' read the rest of the data
strData = Mid(DataBuffer, DataLength + 1)
DataBuffer = ""
DataLength = 0
' jump to the beginning of the function to start the process all over
GoTo MoreDataToProcess
End If
End If
End If
End Sub
Private Sub Received_All_Data(ByVal strData As String)
Dim CharDelimiter As String
Dim DataLines() As String
Dim K As Long
' read the delimiter
CharDelimiter = Left(strData, 1)
' split the data by the delimiter (ommiting the first character the delimter)
DataLines = Split(Mid(strData, 2), CharDelimiter)
' put the data back into a list
List1.Clear
For K = 0 To UBound(DataLines)
List1.AddItem DataLines(K)
Next K
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|