iOS Question socket1_connected stuck in loop

DickD

Active Member
Licensed User
See the very simple module below. The loop never ends after 10 as shown in the log. It has to be killed with the Debugger. The StartSearch.Show sub is called properly as shown in the log but the loop keeps on going. Why?

B4X:
Sub doit
socket1.Connect(ipaddress,5500,600)
End Sub

Sub socket1_connected(success As Boolean)
Log(ipaddress & " connection = " & success & ", IPcounter = " & ipcounter)
If success = False Then
ipcounter = ipcounter + 1
ipaddress = ipbase & ipcounter
If ipcounter > 10 Then
socket1.Close
FU.ResignFocus
StartSearch.show
End If
End If
' socket1.Close
doit
End Sub

Log of the above:

Application_Start
Application_Active
IN StartSearch start
192.168.2.1 connection = false, IPcounter = 1
192.168.2.2 connection = false, IPcounter = 2
192.168.2.3 connection = false, IPcounter = 3
192.168.2.4 connection = false, IPcounter = 4
192.168.2.5 connection = false, IPcounter = 5
192.168.2.6 connection = false, IPcounter = 6
192.168.2.7 connection = false, IPcounter = 7
192.168.2.8 connection = false, IPcounter = 8
192.168.2.9 connection = false, IPcounter = 9
192.168.2.10 connection = false, IPcounter = 10
IN startsearch.show = Main.NavControl.ShowPage('SL')
192.168.2.11 connection = false, IPcounter = 11
IN startsearch.show = Main.NavControl.ShowPage('SL')
192.168.2.12 connection = false, IPcounter = 12
IN startsearch.show = Main.NavControl.ShowPage('SL')
192.168.2.13 connection = false, IPcounter = 13
IN startsearch.show = Main.NavControl.ShowPage('SL')
192.168.2.14 connection = false, IPcounter = 14
IN startsearch.show = Main.NavControl.ShowPage('SL')
192.168.2.15 connection = false, IPcounter = 15
IN startsearch.show = Main.NavControl.ShowPage('SL')
192.168.2.16 connection = false, IPcounter = 16
IN startsearch.show = Main.NavControl.ShowPage('SL')
192.168.2.17 connection = false, IPcounter = 17
IN startsearch.show = Main.NavControl.ShowPage('SL')
192.168.2.18 connection = false, IPcounter = 18
IN startsearch.show = Main.NavControl.ShowPage('SL')
 

DickD

Active Member
Licensed User
Why aren't you using Wait For to wait for the connected event? It will simplify your code.

As I previously wrote this is a very inefficient way to find the server. The UDP solution is much better.

You need to dim and initialize the socket again before you reuse it.
1. I have not seen Wait For discussed in any of the documentation or in any of the sample programs. Do you have an example?
2. I will probably use UDP eventually but I was already in the middle of this and want to see it work. I understand why you say it isn't very efficient due to the TimeOut period. I also don't want to have to use a server but don't know enough about UDP yet to rule this out. I'll get there eventually.
3. I added a new dim and initialize command at the top of Sub Doit as shown below. It did not stop the run away loop.

Erel: We need better documentation. Too much of using B4i is guess work and trial-and-error. I can put up with this since I'm independent, my time is my own. But I don't see it being used in a professional IT operation without dependable support.

B4X:
Sub doit
Dim socket1 As Socket
socket1.Initialize("Socket1")
socket1.Connect(ipaddress,5500,2000)
End Sub

Sub socket1_connected(success As Boolean)
Log(ipaddress & " connection = " & success & ", IPcounter = " & ipcounter)
If success = False Then
ipcounter = ipcounter + 1
ipaddress = ipbase & ipcounter
If ipcounter > 10 Then
socket1.Close
FU.ResignFocus
StartSearch.show
End If
End If
socket1.Close
doit
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top