Android Question IP Scanning - Problems

aidymp

Well-Known Member
Licensed User
Longtime User
Hi Guys im trying to detect the presence of a device on my network that accepts FTP connections

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
Dim Timer1 As Timer
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
Dim Command As String
Dim Result As Int
Dim Ph As Phone
Dim Adr As String
Dim Start As Int
Dim stop As Int
Dim M As Int
Dim blocker As Boolean = False
Dim FTP As FTP_Auto
Dim address As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    'Timer1.Initialize("Timer1", 1000)
    'Timer1.Enabled=True
RunPing
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub RunPing
Command = File.Combine(File.DirInternalCache, "command")
Adr="192.168.0."
Start=1
stop=254
For M=Start To stop
File.WriteString(File.DirInternalCache, "command", "ping -w 1 -c 1 -n " & Adr & M  & CRLF & "exit")
Dim StdOut, StdErr As StringBuilder
StdOut.Initialize
StdErr.Initialize
Result = Ph.Shell("sh", Array As String(Command), StdOut, StdErr)
Dim R As String
R=StdOut.ToString
If R.Contains("1 received") Then
check (Adr&m)
Log(Adr & M & " On")
Else
Log(Adr & M & " Off")
End If
Next
End Sub
  Sub FTP_FileExist(Found As Boolean)
    If Found Then Log("FTP connection successful") Else Log("Nope!")
End Sub
Sub check(addr As String)
FTP.Initialize(Activity, Me, addr, "root", "", 21, False, False)
Log (addr)
'Log (FTP.IsInitialized)
FTP.FileOrFolderExist("font.cfg" , "etc", False)
End Sub

FTP_FileExist is never fired, I presume that before it gets an answer FTP.Initialise is called again?

I have tried various ways to fix this I used a timer but the timer event will not fire! I presume this is because of the shell command?

I have tried using a flag that prevents other parts been run until FileExist is called - but that never gets called!

If i use the FTP.initialise and the FTP.File exist comands out of that loop they connect and fire correctly!

its driving me nuts, been working on this 48 hours!

Any help would really be appreciated!

Thanks

Aidy
 

aidymp

Well-Known Member
Licensed User
Longtime User
Hi I have solved this problem! I changed the code as follows!

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
Dim Timer1 As Timer
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
Dim Command As String
Dim Result As Int
Dim Ph As Phone
Dim Adr As String
Dim Start As Int
Dim stop As Int
Dim M As Int=0
Dim blocker As Boolean = False
Dim FTP As FTP_Auto
Dim address As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Timer1.Initialize("Timer1", 3000)
    Timer1.Enabled=True
'RunPing
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub Timer1_Tick
RunPing

End Sub
Sub RunPing
M=M+1
Adr="192.168.0."
Command = File.Combine(File.DirInternalCache, "command")
File.WriteString(File.DirInternalCache, "command", "ping -w 1 -c 1 -n " & Adr & M  & CRLF & "exit")
Dim StdOut, StdErr As StringBuilder
StdOut.Initialize
StdErr.Initialize
Result = Ph.Shell("sh", Array As String(Command), StdOut, StdErr)
Dim R As String
R=StdOut.ToString
If R.Contains("1 received") Then
check (Adr&m)
Log(Adr & M & " On")
Else
Log(Adr & M & " Off")
RunPing
End If
End Sub
  Sub FTP_FileExist(Found As Boolean)
    If Found Then Log("FTP connection successful "&Adr&M) Else Log("Nope!")
    Log(FTP.LastProcessSuccessful)
End Sub
Sub check(addr As String)
FTP.Initialize(Activity, Me, addr, "root", Null, 21, False, False)
FTP.SetStealthOn (True)

'Log (addr)
'Log (FTP.IsInitialized)
FTP.FileOrFolderExist("font.cfg" , "etc", False)
End Sub
 
Last edited:
Upvote 0
Top