B4J Question ping an ip

bogdanc

Active Member
Licensed User
Longtime User
I working on my app.
I need to check that network location (networks devices) is reachable.
I saw similar thread here and I know that I can run the shell command.
I would like to have it in the background and have respond that is reachable or not.
 

eurojam

Well-Known Member
Licensed User
Longtime User
see this example, you can play around with it....
B4X:
Sub AppStart (Args() As String)
   
   Dim shl As Shell
   shl.Initialize("shl", "ping", Array As String("localhost"))
   shl.WorkingDirectory = "C:"
   shl.Run(60000) 'set a timeout of 60 seconds
   StartMessageLoop
End Sub

Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
   If Success And ExitCode = 0 Then
     Log("Success")
     Log(StdOut)
   Else
     Log("Error: " & StdErr & ExitCode)
   End If
   ExitApplication
End Sub
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Small enhancement covering Ping Count = 1 with Windows and Linux/Mac

B4X:
Sub Ping(ipaddress As String)
    Dim shl As Shell
    Dim lstParams As List: lstParams.Initialize
    Try
        Dim os As String = GetSystemProperty("os.name", "").ToLowerCase
        If os.Contains("win") Then
            'Windows
            lstParams.Add("-n")
        Else
            'Linux and Mac
            lstParams.Add("-c")
        End If
        lstParams.Add("1")
        lstParams.Add(ipaddress)
        shl.Initialize("shl", "ping", lstParams)
        shl.WorkingDirectory = File.DirApp
        shl.Run(-1)
    Catch
        Log(LastException.Message)
    End Try
End Sub

B4X:
Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
   If Success And ExitCode = 0 Then
     Log("Success: Host is reachable.")
  Else
       Log("Error: Host is NOT reachable.")
  End If
  ExitApplication
End Sub
 
Upvote 0
Top