Hi,
How can i send the content of list1 with winsock to
list2 ?
thx ! :D
Printable View
Hi,
How can i send the content of list1 with winsock to
list2 ?
thx ! :D
are these seperate apps or are the listboxes on the same form?Quote:
Originally Posted by Tha Joey Madnez
Pino
A client and server :)
You could add the list items to one string, send and then split and add to the other listbox. But, I guess you would have to use weird characters to avoid messing up the splitting of the recieved string.
Got any code ? :)
Kinda like this. Haven't tested it but it's the general idea :
VB Code:
'Send code Dim i As Integer Dim strSend As String Dim sDelimiter As String sDelimiter = "+++" For i = 0 To List1.ListCount - 1 strSend = strSend & List1.List(i) & sDelimiter Next strSend = Left(strSend, Len(strSend) - Len(sDelimiter)) 'After here, you send
And
VB Code:
'Recieve code Dim i As Integer Dim strData As String Dim sDelimiter As String Dim Ar() As String sDelimiter = "+++" Ar = Split(strData, sDelimiter) For i = 0 To UBound(Ar) List2.AddItem Ar(i) Next
The only problem is that if an item in the listbox has +++ in it, the splitting won't work right :(
This is the idea but it doesn't really work as the data is send to fast just appears as one string, ways around it though.
server,
VB Code:
Option Explicit Private Sub Form_Load() With Winsock1 .LocalPort = 9999 .Listen End With Form1.Show End Sub Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long) Winsock1.Close Winsock1.Accept requestID End Sub Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim dat As String Winsock1.GetData dat List1.AddItem dat End Sub
client,
VB Code:
Option Explicit Private i As Long Private Sub Command1_Click() For i = 0 To List1.ListCount - 1 Winsock1.SendData List1.List(i) DoEvents DoEvents Next i End Sub Private Sub Form_Load() Winsock1.Connect "localhost", 9999 For i = 0 To 10 List1.AddItem "test" & i Next i End Sub
ThX ! :wave:
Here's a way of creating a delay, bit long winded though,
server,
VB Code:
Option Explicit Private Sub Form_Load() With Winsock1 .LocalPort = 9999 .Listen End With Form1.Show End Sub Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long) Winsock1.Close Winsock1.Accept requestID End Sub Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim dat As String Winsock1.GetData dat List1.AddItem dat Winsock1.SendData "NEXT" End Sub
client,
VB Code:
Option Explicit Private i As Long Private j As Long Private Sub Command1_Click() If j > List1.ListCount Then Winsock1.Close End If Winsock1.SendData List1.List(j) j = j + 1 End Sub Private Sub Form_Load() Winsock1.Connect "localhost", 9999 For i = 0 To 10 List1.AddItem "test" & i Next i End Sub Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim dat As String Winsock1.GetData dat If dat = "NEXT" Then Command1_Click End If End Sub
You'll find though that when your sending data over the internet there will be a delay anyway due to the distance, well depening on the conection speeds, so my first method would probably work fine in that case. Second method is fullproof though.
why not use packet delimeters? theres a link in my sig :)
:)
Pino
If your app is going to do more then send packets containing the list content i suggest when you send your list content you add a header "LIST" or something like that in the beginning of the packet so you can check and process each packet the way it needs. Use chr(13) as a delimeter.
This is what my example did, I just couldn't remember what the method is called :DQuote:
Originally Posted by Pino
patcher, you could also create a delay based on the SendComplete event of the winsock that is sending :)