Hi,
I'm programming a open TCP connection to my server @ home.
The server sends every 30 secs a Poll message to test the connection.
if the b4a app doesn't receive the command, it reconnects to the server.
Everything is working right, but often when the connection is lost, it doesn't reconnect until i turn the device on and put the focus to my app..
I Can't see what's going wrong, I hope someone can help me!
(I've readed about Startserviceat; maybe thats the solution, but if it is; how do I implend that to my code?, I can't use startserviceat from the service itselfs)?
The main code starts the service (See Below)
I'm programming a open TCP connection to my server @ home.
The server sends every 30 secs a Poll message to test the connection.
if the b4a app doesn't receive the command, it reconnects to the server.
Everything is working right, but often when the connection is lost, it doesn't reconnect until i turn the device on and put the focus to my app..
I Can't see what's going wrong, I hope someone can help me!
(I've readed about Startserviceat; maybe thats the solution, but if it is; how do I implend that to my code?, I can't use startserviceat from the service itselfs)?
The main code starts the service (See Below)
B4X:
'Activity module
Sub Process_Globals
Dim WriteText As TextWriter
Dim txtReceive As TextReader
End Sub
Sub Activity_Create(FirstTime As Boolean) As String
Activity.LoadLayout("Form1")
StartService(TcpService)
End Sub
'TCP Service Module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim TCP_LastReceiveData As Long
Dim myTimer As Timer
Dim Socket1 As Socket
End Sub
Sub Socket1_Connected (Successful As Boolean)
If Main.txtReceive.IsInitialized = False Then
Main.txtReceive.Initialize(Socket1.InputStream)
End If
End Sub
Sub Service_Create
End Sub
Sub myTimer_Tick
Dim MyStr As String
Dim NowTimer As Long = DateTime.Now
Log("Mytimer Event")
Try
If Main.txtReceive.Ready = True Then 'Data is waiting!
Log("DataReceived")
MyStr = Main.txtReceive.Readline
TCP_LastReceiveData = NowTimer + 30000
Select Case MyStr.SubString2(0,4)
Case "TEST"
Case "NOOP"
End Select
End If
Catch
End Try
If NowTimer > TCP_LastReceiveData Then 'Timeout opgetreden
Log("Restart TCP")
RestartTcpClient
End If
End Sub
Sub RestartTcpClient
Dim NowTimer As Long = DateTime.Now
TCP_LastReceiveData= NowTimer + 30000
If Socket1.IsInitialized Then Socket1.Close
Socket1.Initialize("Socket1")
Socket1.Connect("my serverIP",899,20000)
End Sub
Sub Service_Start (StartingIntent As Intent)
RestartTcpClient
myTimer.Initialize("myTimer",6000)
myTimer.Enabled = True
End Sub
Last edited: