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