Network example using async streaming

prokli

Active Member
Licensed User
Longtime User
Hello everybody!

I am rather puzzled with this simple network example.
This code is an extraction of a large project which is sending to and receiving data from a temperature sensor via WLAN. I would like to use asynchronous streaming instead of polling for data inside a timer sub.

The problem:
Connecting to my sensor works fine for the very first time. If I press "Close" button and then "Connect" button again, I get always a wrong connection (Successful always = False). What's wrong???

B4X:
'Activity module
Sub Process_Globals
    Dim ClientSocket As Socket        
    Dim AStream As AsyncStreams        '
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Display")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Close_Click
    If ClientSocket.Connected Then
       Astream.Close
       ClientSocket.Close
    End If
End Sub

Sub Connect_Click
    If Not(ClientSocket.IsInitialized) Then
       ClientSocket.Initialize("ClientSocket")
    End If
    ClientSocket.Connect("192.168.154.85", 2101, 2000)
End Sub

Sub ClientSocket_Connected (Successful As Boolean)
    If Successful Then
       AStream.Initialize(ClientSocket.InputStream,    
                                ClientSocket.OutputStream, "AStreams")

    ToastMessageShow("Successfully connected", True)
    Else
    ToastMessageShow("No connection!", True)
   End If
    
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    ToastMessageShow(msg, False)
    Log(msg)
End Sub
 
Last edited by a moderator:
Top