B4J Question [SOLVED]Ping to check if IP is Alive

Peter Lewis

Active Member
Licensed User
Longtime User
I am looking for an easy way to check if an IP is alive that will not crash my program.

I tried this code and it gives me an error on the output stating that the IP cannot be found , but if I go to CMD , it works perfectly, Any Ideas ?



B4X:
sub Pings(apd As String)
  
  
    Dim sh2 As Shell
    sh2.Initialize("sh2","ping",Array(apd&" -w 1000 -n 1"))
    sh2.Run(1000)
  
End Sub

Sub sh2_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
  
    Log("exit code "&ExitCode)
    Log("out "&StdOut)
    Log("error "&StdErr)
    Log("Success "&Success)
  
End Sub

The correct IP is fed into the sub
192.168.1.120
exit code 1
out Ping request could not find host 192.168.1.120 -w 1000 -n 1. Please check the name and try again.
error
Success true

So it completes the process with no error and us successful but the text back from PING gives another issue


Any Ideas ?
 

Peter Lewis

Active Member
Licensed User
Longtime User
I took out the additional arguments and it does accept that and works but I will have no way of setting the timeout and the number of pings
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
B4X:
sh.Initialize("sh2","ping",Array(apd, "-n", "5"))

each parameter is its own separate string in the passed array. you were passing everything as 1 string. not only that, but any parameters that take a value (eg, -c 5) requires 2 strings (1 for the switch and 1 for the value!!!)
so, to ping apd and set the count to 5, you need array( apd, "n", "5" )
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
B4X:
sh.Initialize("sh2","ping",Array(apd, "-n", "5"))

each parameter is its own separate string in the passed array. you were passing everything as 1 string. not only that, but any parameters that take a value (eg, -c 5) requires 2 strings (1 for the switch and 1 for the value!!!)
so, to ping apd and set the count to 5, you need array( apd, "n", "5" )
thank you
 
Upvote 0
Top