Android Question Problem with network

stefanoxjx

Active Member
Licensed User
Hi guys, someone can tell me why this code don't send "Test line1" and "Test line2" but only "Test line3" and "Test line4"?

B4X:
Sub Process_Globals
   
Dim Client As Socket

End Sub

Sub Globals
   
Client.Initialize("Client")
Dim Astreams As AsyncStreams

End Sub

Sub Activity_Create(FirstTime As Boolean)

  Activity.LoadLayout("1")

End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub AStreams_Error
  ToastMessageShow("Errore", True)
End Sub

Sub Client_Connected (Successful As Boolean)
If Successful = False Then
  Msgbox(LastException.Message, "Error connecting")
  Return
  End If
  ToastMessageShow("Connect",False)
   
  Astreams.Initialize(Client.InputStream, Client.OutputStream, "AStreams")
End Sub

Sub SendCommand(text As String)
   Dim buffer() As Byte
  buffer = text.GetBytes("utf-8")
  Astreams.Write(buffer)
     
End Sub

Sub AStreams_NewData (Buffer() As Byte)
  Dim msg As String
  msg = BytesToString(Buffer, 0, Buffer.Length,"utf-8")
  ToastMessageShow(msg, False)
End Sub

Sub Button1_Click
Client.Connect("192.168.0.252",80,2000)
   Delay(3000)

   SendCommand("Test line1")
  SendCommand("Test line2")
   
   
End Sub
Sub Button2_Click
   
  SendCommand("Test line3")
  SendCommand("Test line4")

End Sub


Sub Delay(nMilliSecond As Long)
   Dim nBeginTime As Long
   Dim nEndTime As Long
   
   nEndTime = DateTime.Now + nMilliSecond
   nBeginTime = DateTime.Now
   Do While nBeginTime < nEndTime
   nBeginTime = DateTime.Now
   Log(nBeginTime)
   If nEndTime < nBeginTime Then Return
   DoEvents
   Loop
   
End Sub
I would like to connect, send packet (Test line1 & Test line 2) and disconnect with one touch, but seems that this solutions don't work :(
Please, help me.
Unfortunately, I'm newbe of B4A :(
Thanks.

Stefano
 

KMatle

Expert
Licensed User
After "Client.Connect" you have to wait until "Sub Client_Connected (Successful AsBoolean)" is called. AFTER Successful = true you can send commands.

Most of those calls (like connect) call a routine in Android's OS. This routine calls a defined sub when it is ready (here: Sub Client_Connected). You have to "wait" for it.

Connecting, waiting x secs and then sending commands without knowing if it is really connected cause errors. It's like you pass a crossing assuming that the lights are green without waiting for "green"

So move the connect statement to Activity_Create, disable the buttons (no send until connected) and debug it to see that the Sub Client_Connected is called. Inside Sub Client_Connected enable the send buttons again (now it is connected and you can send commands).
 

stefanoxjx

Active Member
Licensed User
Hi KMatle and thanks for your explanation.
I would like to have a sub routine that connect, send data and disconnect.
About this, I would have two questions:
- I've tried to write a sub routine like this:
B4X:
Sub Button1_Click
  Client.connect("192.168.0.252",80,2000)
  'I'm waiting for connection ready
  Do While (Client.connected=false)
  Loop
  SendCommand("bla bla")
  'Close connection
  Client.close
End Sub
Why Client.connected return true but SendCommand don't send any byte?
- Client.close de initialize the socket but I would like to close only connection without socket de initialization.
Exist a command to do it?

Thanks.

Stefano
 

KMatle

Expert
Licensed User
Take a look at Erels example: http://www.b4x.com/android/forum/threads/asyncstreams-tutorial.7669/#content

What you have to understand is that Android (like any other OS like Windows) is working with events.

As you can imagine there are a lot of programs running parallel on you phone. If one app/programm would block the whole cpu while in a loop (as you have it in your code) it would use 100% of the CPU and would stop all other running apps/programms on the phone. Android checks this and giving a prompt "App does not response" (wait/close it).

In the code you define Sub's and Android knows if event x is raised, it must call this sub.

In your app you execute "Client.Connect("192.168.0.252",80,2000)". Notice that after that your app has to "stop" at that point. No own waits - no loops -nothing like that - just do nothing! Just wait for the sub to be called.

Android gets "Client.Connect("192.168.0.252",80,2000)" and does all things needed to connect (your app is -let's say- "sleeping"). If Android is ready, it will call the event "Client_Connected" automatically and your app "awakes" again. You check if the connection is successful with "If Successful = true". When successful open a msgbox to tell the user that it's connected.

You have all the subroutines you need. Remove the connect from the button's click event to Activity_Create , remove all delays/waits and try.

After that we can talk about disconnect and further functions.
 
Top