Results 1 to 7 of 7

Thread: [RESOLVED] Winsock Multithread : Connect, DataArrival

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    353

    Resolved [RESOLVED] Winsock Multithread : Connect, DataArrival

    Hi guys, i've been searching the forum for some info but i did not find what i needed...

    What i need to do is :

    - send 4 different strings to 4 different ip's (hosts)
    - read the data recieved from all the 4 hosts i've sent the data before

    So far i have done this :

    CONNECT BUTTON :
    Hosts(0) = "192.168.1.1"
    Hosts(0) = "192.168.1.2"
    Hosts(0) = "192.168.1.3

    For i = 0 To 2
    Winsock1(i).Close
    Winsock1(i).Connect Hosts(i), 80

    DataToSend(i)= Hosts(i)

    next i


    WINSOCK CONNECT :

    For i = 0 To 2
    If Winsock1(i).State = 7 Then Winsock1(i).SendData LOGIN_DATA_SEND(i)

    Next i

    this code kinda works, winsock connects but sending the first packet in a bad way making multiple lines in DataTosend...but that s not the main problem

    Now the problem i have is how to get the DATA ARRIVAL in the approriate arrays.

    Like this :


    for i=0 to 2

    DataRetrieved(i) =

    next i


    Cheers
    Thanks for helping me out.

  2. #2
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Winsock Multithread : Connect, DataArrival

    You don't have to loop. When you add a Winsock array to your form, then you can simply use the Winsock events and the Index of each connection.

    Something like this.

    vb Code:
    1. Option Explicit
    2.  
    3. Private DataToSend(2) As String
    4. Private DataRetrieved(2) As String
    5. Private Hosts(2) As String
    6.  
    7. Private Sub Command1_Click()
    8.     Dim i As Integer
    9.    
    10.     Hosts(0) = "192.168.1.1"
    11.     Hosts(1) = "192.168.1.2"
    12.     Hosts(2) = "192.168.1.3"
    13.  
    14.     For i = 0 To 2
    15.         Winsock1(i).Close
    16.         Winsock1(i).Connect Hosts(i), 80
    17.  
    18.         DataToSend(i) = Hosts(i)
    19.     Next i
    20. End Sub
    21.  
    22. Private Sub Form_Load()
    23.    
    24.     'Load two more sockets
    25.     Load Winsock1(1)
    26.     Load Winsock1(2)
    27.  
    28. End Sub
    29.  
    30. Private Sub Winsock1_Connect(Index As Integer)
    31.     Winsock1(Index).SendData DataToSend(Index)
    32. End Sub
    33.  
    34. Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    35.     Dim strReturn As String
    36.    
    37.     Winsock1(Index).GetData strReturn, vbString 'receive the data
    38.    
    39.     DataRetrieved(Index) = strReturn
    40.    
    41.     'DataRetrieved(Index) now contains the received data
    42.     'Note: if the data is more than a few kB, then you'll need to buffer it
    43. End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    353

    Re: Winsock Multithread : Connect, DataArrival

    Thanks man for replying...i was thinking how to do this...

    let me try this ... then ill get back to tell how i did


    Cheers
    Thanks for helping me out.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    353

    Re: Winsock Multithread : Connect, DataArrival

    everything works fine so far...

    the only problem i get is on DATA ARRIVAL...i get subscript out of range for this

    Code:
     DataRetrieved(Index) = strReturn
    Thanks for helping me out.

  5. #5
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Winsock Multithread : Connect, DataArrival

    How is DataRetrieved declared?

    With "Private DataRetrieved(2) As String" you can only use 3 connections. If you want to use more connections, then increase the number in the declaration.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    353

    Re: Winsock Multithread : Connect, DataArrival

    yeah...i got to tthat later one hehehe

    btw it seems that sometimes i dont get the entire string back from data arrival even though i put

    text1.text = text1.text & strReturn
    Thanks for helping me out.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    353

    Re: Winsock Multithread : Connect, DataArrival

    it all worked well as u said man

    thanks and .. rating
    Thanks for helping me out.

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