Link to home
Start Free TrialLog in
Avatar of Razorknight
Razorknight

asked on

Splitting received data from winsock client into text boxes

Hello, I am trying to split received data from a winsock client into 4 text feilds. I have tried this method but I am getting the error "Runtime Error '9" "Subscript out of range". I cannot see why though, please help.

Here is my code.

'Sending Data
Dim str As String
str = Text1.Text & Chr$(0) & Text2.Text & Chr$(0) & Text4.Text & Chr$(0)
Winsock1.SendData str

'Receiving Data
Dim sArrParams() As String
Winsock1.GetData Data

sArrParams = Split(Data, Chr$(0))
Text1.Text = sArrParams(0)
Text2.Text = sArrParams(1)
Text3.Text = sArrParams(2)
Text4.Text = sArrParams(3)
Avatar of mirzas
mirzas
Flag of Bosnia and Herzegovina image

define sArrParams as

Dim sArrParams(0 to 3) as String


if that does not work make sure you got all of the things you sent.
Avatar of drnick
drnick

str = Text1.Text & Chr$(0) & Text2.Text & Chr$(0) & Text4.Text & Chr$(0)

there are only three strings
text3.text is missing
Avatar of Razorknight

ASKER

Hmm, thanks guys, I tried adding text3 and also setting sArrParams(0 to 3) but...

but I get this error when sending: "compile error" "cant assign to array"

here is new code...

Dim sArrParams(0 To 3) As String
Winsock1.GetData Data

sArrParams = Split(Data, Chr$(0))
Text1.Text = sArrParams(0)
Text2.Text = sArrParams(1)
Text3.Text = sArrParams(2)
Text4.Text = sArrParams(3)

Please assist me further.
ack, I forget to show where error occured...

Dim sArrParams(0 To 3) As String
Winsock1.GetData Data

sArrParams = Split(Data, Chr$(0)) 'Error here
Text1.Text = sArrParams(0)
Text2.Text = sArrParams(1)
Text3.Text = sArrParams(2)
Text4.Text = sArrParams(3)
How did you define Data?
ASKER CERTIFIED SOLUTION
Avatar of mirzas
mirzas
Flag of Bosnia and Herzegovina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
here this code worked for me


Private Sub Command1_Click()
    Dim tmp As String
   
    tmp = Text1.Text & Chr$(0) & Text2.Text & Chr$(0) & Text3.Text & Chr$(0) & Text4.Text & Chr$(0)
   
   
    wsk1.Close
    wsk2.Close
    wsk2.LocalPort = 1234
    wsk2.Listen
    wsk1.Connect "localhost", 1234
   
   
    Do
        DoEvents
    Loop Until wsk1.State = sckConnected
   
   
    wsk1.SendData tmp
End Sub

Private Sub Form_Unload(Cancel As Integer)
    wsk2.Close
    wsk1.Close
End Sub

Private Sub wsk2_ConnectionRequest(ByVal requestID As Long)
    wsk2.Close
   
    wsk2.Accept requestID
End Sub

Private Sub wsk2_DataArrival(ByVal bytesTotal As Long)
    Dim tmp As String
   
   
    wsk2.GetData tmp
    'If Len(tmp) <> bytesTotal Then MsgBox "whoops"
   
    Dim foo As Variant
   
   
    foo = Split(tmp, Chr$(0))
   
    MsgBox foo(0)
    MsgBox foo(1)
    MsgBox foo(2)
    MsgBox foo(3)
   
End Sub

Yep, that worked, cheers!