B4J Question Client Socket - Connect & Reconnect

aminoacid

Active Member
Licensed User
Longtime User
I'm trying to write a Sub similar to the MQTT "ConnectAndReconnect" sub posted elsewhere in the forum but for a simple TCP Client Socket connection. I would like the Sub to Connect to a server and then automatically reconnect if the connection breaks or disconnects. This is what I came up with. It works (kind off) but the problem is that the "client.connected" property seems to be stuck at "true" and never seems to return a "false" if the socket is disconnected. Is there any way to work around this? All I need to do is detect when the socket connection has been disconnected but I cannot seem to find a way to detect this, other than using the "client.connected" property. Any ideas would be greatly appreciated. I am stumped!! Thanks!

B4X:
Private Sub ConnectToServer(Host As String, hport As Int)
    Do While working
        Log("Trying to connect to: " & Host)
        CloseExistingConnection
        Dim client As Socket
        client.Initialize("Client")
        client.Connect(Host, hport, 10000)
        'client.Connect("192.1.1.1", hport, 10000)
        Wait For Client_Connected (Successful As Boolean)
        If Successful Then
            AST.Initialize(client.InputStream, client.OutputStream, "AST")
            Log("IP - Successful Connect")
            Do While working And client.Connected
                Sleep(5000)
            Loop
        Else
            Log("IP - Failed to connect: " & LastException)
        End If
        Sleep(5000)
    Loop   
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You haven't described your use case, however in many cases the best solution is to make a connection when needed and close it as soon as you are done with the current task. This is easier and usually more reliable than maintaining an always connection solution.

The Connected property returns the value from the underlying API. Disconnections are not always detected by the OS.

Which server are you connecting to? Why use low level sockets?
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
Thanks for the quick response.
The "server" is a "device server" - a redirected COM port. Data is sent out over the COM port every second or so, therefore it is important to keep the connection open at all times in this case.

I have been testing a "work-around" that appears to be working, although I don't know if this is reliable or the best way to address this issue - I discovered that the Asynch Streams "Error" event fires whenever there is a disconnect, so I have been using this to detect the condition as follows:

B4X:
Sub AST_Error
    connected=False
    Log("AST Error")  'This event is fired on socket disconnect
End Sub


Sub CloseExistingConnection
    If AST.IsInitialized Then AST.Close
    If client.IsInitialized Then client.Close
    connected=False
    Log("IP Connection Closed")
End Sub


Private Sub ConnectToServer(Host As String, hport As Int)
    Do While working
        Log("Trying to connect to: " & Host)
        CloseExistingConnection
        Dim client As Socket
        client.Initialize("Client")
        client.Connect(Host, hport, 10000)
        Wait For Client_Connected (Successful As Boolean)
        If Successful Then
            connected=True
            AST.Initialize(client.InputStream, client.OutputStream, "AST")
            Log("IP - Successful Connect")
            Do While working And connected
                Sleep(5000)
            Loop
        Else
            Log("IP - Failed to connect: " & LastException)
        End If
        Sleep(5000)
    Loop
End Sub
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
Yes, I added that in. Everything seems to be working fine. I have tested for 24 hours with intentional "disconnects" and it reconnects fine. Thanks for your help.

B4X:
Sub AST_Terminated
    connected=False
    Log("AST Terminated") 
End Sub
 
Upvote 0
Top