Android Question Can't reconnect network socket within service

mevial

Member
Licensed User
Longtime User
Hi. I've added network client connection within my service. While starting service, socket connects successfully. But if connection breaks, or server not available, I can't reconnect without restarting service. Only first "_Connected" event has "Success".
B4X:
Sub Service_Create
        Sock.Initialize("Sock")
End Sub

Sub Service_Start (StartingIntent As Intent)
        If NetMode>1 Then
            Sock.Connect(TCPServer, TCPPort,20000)
        End If
End Sub

Sub Sock_Connected(Success As Boolean)
        If Success Then
            Try
                AstNetIsOpen=True
                astNet.Initialize(Me,"ASTNet",Sock.InputStream, Sock.OutputStream)
            Catch
                ToastMessageShow("Can't open network stream",False)
                AstNetIsOpen=False
                AstNetTimeout=30
            End Try
            If AstNetIsOpen Then
                ToastMessageShow("Network connected.", False)
                astNet.Write("::0," & DevMac)
            End If
        Else
            If astNet.IsInitialized Then astNet.Close
            Sock.Close
            AstNetIsOpen=False
            AstNetTimeout=30
            ToastMessageShow("Connecting error.", False)
        End If
End Sub

Sub ElTimerCmd_Tick
        If NetMode>1 And AstNetIsOpen=False Then
            If AstNetTimeout>0 Then
                AstNetTimeout=AstNetTimeout-1
            Else
                Sock.Connect(TCPServer, TCPPort,20000)
            End If
        End If
End Sub
 

OliverA

Expert
Licensed User
Longtime User
1) What changes AstNetIsOpen to False if a connected socket fails? Right now I only see it changed to false if the connection process fails. This would explain why your tick event does not catch the issue.
2) You are connecting every time the service is started. Technically, the socket may already be connected (Service_Start may be called multiple times on an already running service without calling Service_Create again)
3) In the tick method, I would re-dim and re-initialize Sock before trying to call it's Connect method again. I would also first check if Sock is already initialized and open, and if so close the socket first.
 
Upvote 0

mevial

Member
Licensed User
Longtime User
B4X:
Sub ASTNet_Terminated
    ToastMessageShow("Network stream terminated", False)
    AstNetIsOpen=False
    AstNetTimeout=1
    If astNet.IsInitialized Then astNet.Close
    Sock.Close
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
try
B4X:
Sub ElTimerCmd_Tick
        If NetMode>1 And AstNetIsOpen=False Then
            If AstNetTimeout>0 Then
                AstNetTimeout=AstNetTimeout-1
            Else
                Dim Sock As Socket
                Sock.Initialize("Sock")
                Sock.Connect(TCPServer, TCPPort,20000)
            End If
        End If
End Sub
Note: My assumption here is that Sock is Global
 
Upvote 0

mevial

Member
Licensed User
Longtime User
It works. Need I move first initialize socket from Service_Create to Service_Start? How redim works? Local Sock replaces global Sock?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Need I move first initialize socket from Service_Create
If you need it done once for the Service's life, yes (for simplicity. Could keep it in start, but then you need to check IsInitialized and Connected)
How redim works? Local Sock replaces global Sock?
It just redim's the global Sock directly. Seen it once in one of @Erel's code samples and been making use of it since then. Some more B4X magic...
 
Upvote 0

mevial

Member
Licensed User
Longtime User
It just redim's the global Sock directly. Seen it once in one of @Erel's code samples and been making use of it since then. Some more B4X magic...
This redim works only for sockets, or also for any other variables? If I create local variable with same name as global I change the global one?

Without redim reconnections also works fine with initialize.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Without redim reconnections also works fine with initialize.
I just like clean slates. Calling Initialize again does not create a new object. It just calls the initialize method again on the same object. Dim’ing the global object creates a new object
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
This redim works only for sockets, or also for any other variables? If I create local variable with same name as global I change the global one?
All variables. Global/Process_Globals are global to the module at hand. The color coding of the variables in the IDE should give you a hint of their scope.
 
Upvote 0
Top