Android Question Pinging a IP and it returns wrong reply?

aaronk

Well-Known Member
Licensed User
I am trying to ping a IP address, and I am using the following code:
B4X:
Dim p As Phone
Dim sb As StringBuilder
    sb.Initialize
    p.Shell("ping -c 1 192.168.0.10",Null,sb,Null)
Log(sb)

It returns the following:

PING 192.168.0.10 (192.168.0.10) 56(84) bytes of data. --- 192.168.0.10 ping statistics --- 1 packets transmitted, 0 received, 100% packet loss, time 0ms

Doesn't the reply mean that it couldn't ping the IP?
It seems to transmit 1 packet since I think that my ping command is telling it to only send 1 packet, but there was 100% packet loss.

The IP address it's trying to Ping is my computers IP.

I then did a search and come across NJDude's sample:
http://www.b4x.com/android/forum/attachments/ping_sample-zip.17235/

But that is doing the same thing where it replies with 100% packet loss.
When using the ping sample from NJDude, I used:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Msgbox(Ping("192.168.0.10", "Status", 5, 10, True), "PING Results")
End Sub
Sub Ping(URL As String, ResultsType As String, Attempts As Int, Timeout As Int, Message As Boolean) As String
    Dim sb As StringBuilder
    Dim p As Phone
     
    sb.Initialize
 
    If Message Then
       ProgressDialogShow("Pinging " & URL)
        
        DoEvents
           DoEvents
             
    End If
    If ResultsType = "Report" Then    Option = "-v "
    If ResultsType = "Summary" OR ResultsType = "Status" Then Option = "-q "
    p.Shell("ping -c" & Attempts & " -W" & Timeout & Option & URL, Null, sb, Null)
         
    ProgressDialogHide
    If sb.Length = 0 Then Return "Host unreachable"
         
    If ResultsType = "Status" Then
       Return "Host online"                 
    Else     
       Return sb.ToString                 
    End If
End Sub
That code above says it was able to ping the IP address.
If I then change the IP to something else that is not a IP address on my network like 192.168.4.10 it still says the host is online.

But when I change it to:
(with the correct IP)

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Msgbox(Ping("192.168.0.10", "Report", 5, 10, True), "PING Results") ' or when I use Summery instead of Report
End Sub
It always returns the same result:

PING 192.168.0.10 (192.168.0.10) 56(84) bytes of data. --- 192.168.0.10 ping statistics --- 1 packets transmitted, 0 received, 100% packet loss, time 0ms

Is there something I am doing wrong ?
 
Last edited:

aaronk

Well-Known Member
Licensed User
Still shows it as Online.
Using my computer I pinged a IP address and it didn't reply since the IP doesn't exist on my network, when I use in my app and it tried to ping the same IP that I used on my PC it says it's online.

Tried this in Rapid and Legacy debug modes and both get the same result. (it say it's online)
 

Peter Simpson

Expert
Licensed User
Hiya @aaronk,
Hmm that's strange. All I did was copy your code and changed that one line. Admittedly it always said online in release modes, but worked perfect in debug modes. If I used an IP address that I knew exists on my network I get 'Host online', otherwise I get 'Host unreachable'. It also works for url's too. google.co.uk = 'Host online', google123abc.co.uk = 'Host unreachable'..
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
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 SB As StringBuilder   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    SB.Initialize
    Msgbox(Ping("192.168.0.100", "Status", 5, 10, True), "PING Results") 'Summary or Status/Report   
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Public Sub Ping(URL As String, ResultsType As String, Attempts As Int, Timeout As Int, Message As Boolean) As String
    Dim P As Phone
    Dim Option As String

    If Message = True Then ProgressDialogShow("Pinging " & URL)
   
    If ResultsType = "Report" Then Option = "-v "
    If ResultsType = "Summary" OR ResultsType = "Status" Then Option = "-q "
    P.Shell("ping -c " & Attempts & " -W " & Timeout & Option & URL, Null, SB, Null)

    ProgressDialogHide
    If SB.Length = 0 OR SB.ToString.Contains("Unreachable") Then Return "Host unreachable"
    Log(SB.ToString)

    If ResultsType <> "Status" Then
        Return SB.ToString
    Else
        Return "Host online"
    End If
End Sub
Works perfect in debug modes
 
Last edited:

NJDude

Expert
Licensed User
If you want to ping URLs (like www.google.com) you have to add the INTERNET permission to the manifest, the code works in DEBUG mode because debug adds this permission automatically.
 

aaronk

Well-Known Member
Licensed User
It seems to now 1/2 work.

I think the problem I had was I didn't have a space in the Ping command.

I was using:
p.Shell("ping -c" & Attempts & " -W" & Timeout & Option & URL, Null, sb, Null)

and now I replaced it with the following:
p.Shell("ping -c " & Attempts & " -W " & Timeout & Option & URL, Null, sb, Null)

Now it seems to be showing 'Host online' & 'host unreachable' correctly.

The only issue now I have with it is when you use something like this:
Log(Ping("192.168.0.1", "Report", 4, 10, True))

It always returns:
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data. --- 192.168.0.1 ping statistics --- 4 packets transmitted, 0 received, 100% packet loss, time 1005ms

192.168.0.1 is my router.
If I use:
Log(Ping("192.168.0.1", "Report", 4, 10, True))
It will reply saying 'Host online' however when I set it to be 'report' or even 'Summary' it always shows that is gets 100% packet loss even knowing when set to 'Status' it says it's online.

I then tried pinging google.com and I get this reply:
PING google.com (74.125.237.161) 56(84) bytes of data. 64 bytes from syd01s18-in-f1.1e100.net (74.125.237.161): icmp_seq=1 ttl=52 time=25.5 ms 64 bytes from syd01s18-in-f1.1e100.net (74.125.237.161): icmp_seq=2 ttl=52 time=36.7 ms 64 bytes from syd01s18-in-f1.1e100.net (74.125.237.161): icmp_seq=3 ttl=52 time=35.4 ms 64 bytes from syd01s18-in-f1.1e100.net (74.125.237.161): icmp_seq=4 ttl=52 time=36.2 ms 64 bytes from syd01s18-in-f1.1e100.net (74.125.237.161): icmp_seq=5 ttl=52 time=171 ms --- google.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4006ms rtt min/avg/max/mdev = 25.526/61.033/171.198/55.237 ms

Should this ping query work (Report/Summary) with a IP address rather than a hostname ?
 
Last edited:

Peter Simpson

Expert
Licensed User
Hello @aaronk,
It worked for me without the spaces. I only added the spacing for continuity purposes for the way that I code.

Sorry the comment text should actually read as follows, xxx 'Status or Summary/Report. Apart from that, everything appears to be working just fine :)
Should this ping query work (Report/Summary) with a IP address rather than a hostname ?
It works with both as long as you have the following in your Manifest file.
B4X:
'Add internet permission
AddPermission(android.permission.INTERNET)
 

aaronk

Well-Known Member
Licensed User
@Peter Simpson I added the Permission and get the same result where it only works when using:
Log(Ping("192.168.0.1", "Status", 4, 10, True))
The above Replies 'Host online'

If I change it to something like the following (which is on a different Subnet):
Log(Ping("192.168.5.1", "Status", 4, 10, True))
It replies 'Host unreachable' which is correct. So I know the Status part is working fine.

When using:
Log(Ping("192.168.0.1", "Report", 4, 10, True))
or
Log(Ping("192.168.0.1", "Summary", 4, 10, True))

It replies:
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data. --- 192.168.0.1 ping statistics --- 4 packets transmitted, 0 received, 100% packet loss, time 1005ms

However I know the router is online, but still says that 100% packet loss.

If I then use Googles IP address (rather than the domain name) like:
Log(Ping("74.125.237.162", "Report", 4, 10, True))

It works fine:
PING 74.125.237.162 (74.125.237.162) 56(84) bytes of data. 64 bytes from 74.125.237.162: icmp_seq=1 ttl=52 time=29.7 ms --- 74.125.237.162 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 29.758/29.758/29.758/0.000 ms

Seems that when I try and ping something on my network it's failing for some reason even through the devices I am trying to ping are correct.
I even tried to make it ping my computers IP address and it only works with the Status command and not Report/Summary.

I might have to make do with the Status (Host Online/Host unreachable).
 

sorex

Expert
Licensed User
you might have ICMP replies turned of on the router. so it blocks sending back to your phone.
 

sorex

Expert
Licensed User
sorry, I thought I saw router but it's your computer...

same thing actually.

turn off the firewall or enable IMCP replies in the rules (it was default off in XP, not sure about the later OSs)
 

sirjo66

Well-Known Member
Licensed User
It's a space problem.
Change in:
B4X:
p.Shell("ping -c " & Attempts & " -W " & Timeout & " " & Option & URL, Null, sb, Null)
For to debug it, the best code is:
B4X:
Dim txt As String
txt = "ping -c " & Attempts & " -W " & Timeout & " " & Option & URL
Log(txt)
p.Shell(txt, Null, sb, Null)
Sergio
 

aaronk

Well-Known Member
Licensed User
Can you upload your code (as a B4A project) and I will run it on my device and will let you know if it works.
I will turn my computer into a hotspot and connect my phone to it to bypass any other network device and will let you know if it then works or not

I tried on 2 routers so far (the one at home: Netgear CG3 100D-2 Cable Gateway, and the router in the office: Billion) and both don't ping correctly.
 
Top