How to ping a site

lock255

Well-Known Member
Licensed User
Longtime User
Hi I am creating a program that does ping a website, so what I need is:
1) Know whether the site is online
2) the IP address of the server
If someone helps me I'd be really grateful. I just started programming with B4A, so I ask you spieghermi things in a simple and step by step.
 

GaNdAlF89

Active Member
Licensed User
Longtime User
Thanks!!
Is timeout measured in seconds? Unlike windows shell, where is measured in milliseconds?
 
Last edited:
Upvote 0

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
please how is it possible, to do UNLIMITED pings?
Thanks
 
Upvote 0

mrred128

Active Member
Licensed User
Longtime User
I don't know if this helps, but I wound up using this snippet to tell if a local machine was actually local.

B4X:
Sub TestServer(server as string) As Boolean
    Dim p As Phone
    Dim sb As StringBuilder
    sb.Initialize
    p.Shell("ping -c 1 " & server,Null,sb,Null)
    If sb.Length = 0 Then
        Return False
    Else
        Return True
    End If
End Sub

Short and sweet. If checking the internet, I would change the parameter 1 to a higher value.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
Unfortunately I can not publish the full code, but you can safely write it if you follow the previous comments. If you have any difficulty sonoa your disposal!

Lock,

I think you need to work on your attitude my friend.

In all your threads it's

"give me this", "give me that", "write me a working example"

always in a demanding way instead of a polite "can I have" question.

So I was stunned to see when someone asked for your 5 lines of code that you don't even want to give it to him
while the only thing you changed is probably the url or ip address.

How low can you go.

besides that a ping was/is no garantee that a website it up and running and icmp request like ping gets blocked on a lot of firewalls/servers.
 
Upvote 0

lock255

Well-Known Member
Licensed User
Longtime User
Lock,

I think you need to work on your attitude my friend.

In all your threads it's

"give me this", "give me that", "write me a working example"

always in a demanding way instead of a polite "can I have" question.

So I was stunned to see when someone asked for your 5 lines of code that you don't even want to give it to him
while the only thing you changed is probably the url or ip address.

How low can you go.

besides that a ping was/is no garantee that a website it up and running and icmp request like ping gets blocked on a lot of firewalls/servers.
If you can not seem to express myself with words "not taught", it's because I use the google translator.

I do not expect anything from anyone, any suggestion for my problems is greatly appreciated.

I can not understand what you write to make a discussion about a year ago ... tell me in private or in a more recent thread
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
because your new threats always start like that aswell, maybe I should pull Google's ear since you use google translate ;)
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
Here's a snippet I use in my apps which seems to work fine:
B4X:
Sub TestServer(server As String) As Boolean
    Dim p As Phone
    Dim sb As StringBuilder
    sb.Initialize
    p.Shell("ping -c 1 " & server,Null,sb,Null)
    Log(sb)
    If sb.Length = 0 Then
        Return False
    Else
        If (s.Mid(sb.ToString,(s.InStr(sb.ToString,"received,")+11),(s.InStr(sb.ToString,"%"))-(s.InStr(sb.ToString,"received,")+10)))="0" Then
            Return True
        Else
            Return False
        End If
    End If
End Sub

Sub CheckConnection
Dim SP As ServerSocket
    Dim p As Phone
      
    Dim connected As Boolean
    connected = False
If (p.GetSettings ("wifi_on") == 1) Then
    If (SP.GetMyWifiIP <>"127.0.0.1") Then
        If TestServer("xxx.xxx.xxx.xxx")=False Then
            connected =  False
            ToastMessageShow("Server not reachable", False)
        Else
            connected =  True
        End If
    Else
    ToastMessageShow("No valid IP address", False)
    connected = False
    End If
Else
ToastMessageShow("WiFi off", False)
connected = False
End If
    Log(SP.GetMyWifiIP)
    
  Log (connected)
    Return connected
  
End Sub
So all you need to do after that is check like this:
B4X:
If CheckConnection=True Then

end if

The whole code is not mine. I just assembled it from parts from other members. Kudos to them.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Here's a snippet I use in my apps which seems to work fine:
B4X:
Sub TestServer(server As String) As Boolean
    Dim p As Phone
    Dim sb As StringBuilder
    sb.Initialize
    p.Shell("ping -c 1 " & server,Null,sb,Null)
    Log(sb)
    If sb.Length = 0 Then
        Return False
    Else
        If (s.Mid(sb.ToString,(s.InStr(sb.ToString,"received,")+11),(s.InStr(sb.ToString,"%"))-(s.InStr(sb.ToString,"received,")+10)))="0" Then
            Return True
        Else
            Return False
        End If
    End If
End Sub

Sub CheckConnection
Dim SP As ServerSocket
    Dim p As Phone
     
    Dim connected As Boolean
    connected = False
If (p.GetSettings ("wifi_on") == 1) Then
    If (SP.GetMyWifiIP <>"127.0.0.1") Then
        If TestServer("xxx.xxx.xxx.xxx")=False Then
            connected =  False
            ToastMessageShow("Server not reachable", False)
        Else
            connected =  True
        End If
    Else
    ToastMessageShow("No valid IP address", False)
    connected = False
    End If
Else
ToastMessageShow("WiFi off", False)
connected = False
End If
    Log(SP.GetMyWifiIP)
   
  Log (connected)
    Return connected
 
End Sub
So all you need to do after that is check like this:
B4X:
If CheckConnection=True Then

end if

The whole code is not mine. I just assembled it from parts from other members. Kudos to them.
Your code will never be reliable. The server may stop answering to your PING for any reason (it is off for maintenance, it is victim of a DOS attack, its network cards are not functioning properly, its firewall rules have been changed to block the ICM protocol, its electrical sources are down, etc.) and lead you to think that you're not connected to Internet, which is wrong. That's less the case, of course, with server farms behind the same IP, which are supposed to be always up, but you generate an useless traffick to these servers and it may happen that you're unable to reach them anyway due to a DNS failure for example (one of my servers is still unreachable for this reason). I admit that pinging google.com will help you to decide with a high probability whether you're connected or not but Google may become bored at some time to be the target of millions of ICMP requests and start blocking them... Moreover, between the time where you check the server and the time where you really want to use the network, many things can occur and the "connected/unconnected" status may become wrong. So, to my eyes, checking a server to know whether you're connected or not is at worst unreliable, at best useless. If you want to use a remote resource/service, connect to it. What's the usefulness of pinging a server (especially a different server than the one you want to connect) some minutes ago ?
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
Your code will never be reliable. The server may stop answering to your PING for any reason (it is off for maintenance, it is victim of a DOS attack, its network cards are not functioning properly, its firewall rules have been changed to block the ICM protocol, its electrical sources are down, etc.) and lead you to think that you're not connected to Internet, which is wrong. That's less the case, of course, with server farms behind the same IP, which are supposed to be always up, but you generate an useless traffick to these servers and it may happen that you're unable to reach them anyway due to a DNS failure for example (one of my servers is still unreachable for this reason). I admit that pinging google.com will help you to decide with a high probability whether you're connected or not but Google may become bored at some time to be the target of millions of ICMP requests and start blocking them... Moreover, between the time where you check the server and the time where you really want to use the network, many things can occur and the "connected/unconnected" status may become wrong. So, to my eyes, checking a server to know whether you're connected or not is at worst unreliable, at best useless. If you want to use a remote resource/service, connect to it. What's the usefulness of pinging a server (especially a different server than the one you want to connect) some minutes ago ?

Thank you for your comment.
As I wrote, it works in "my apps".
If anyone thinks it's not unreliable or useless, which indeed it isn't in some cases which are out of this thread's field of interest, they can benefit from it.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
As I wrote, it works in "my apps".
Don't get me wrong. I have no doubt about the fact it works fine with some servers and no objection about the fact that you share your code. But I have a personal obsession that pushes me to try to understand why so many people feel the need to "ping" a server before connecting to this server (or worse: before connecting to a different server). Your favourite web browser does not "ping" anything. It tries to establish the connection when you submit an URL and has no idea of the connected status before. So I would be glad if someone could explain the reason for this PING before connection.
 
Upvote 0

Celso

Member
Licensed User
Longtime User
Here's a snippet I use in my apps which seems to work fine:
B4X:
Sub TestServer(server As String) As Boolean
    Dim p As Phone
    Dim sb As StringBuilder
    sb.Initialize
    p.Shell("ping -c 1 " & server,Null,sb,Null)
    Log(sb)
    If sb.Length = 0 Then
        Return False
    Else
        If (s.Mid(sb.ToString,(s.InStr(sb.ToString,"received,")+11),(s.InStr(sb.ToString,"%"))-(s.InStr(sb.ToString,"received,")+10)))="0" Then
            Return True
        Else
            Return False
        End If
    End If
End Sub

Sub CheckConnection
Dim SP As ServerSocket
    Dim p As Phone
     
    Dim connected As Boolean
    connected = False
If (p.GetSettings ("wifi_on") == 1) Then
    If (SP.GetMyWifiIP <>"127.0.0.1") Then
        If TestServer("xxx.xxx.xxx.xxx")=False Then
            connected =  False
            ToastMessageShow("Server not reachable", False)
        Else
            connected =  True
        End If
    Else
    ToastMessageShow("No valid IP address", False)
    connected = False
    End If
Else
ToastMessageShow("WiFi off", False)
connected = False
End If
    Log(SP.GetMyWifiIP)
   
  Log (connected)
    Return connected
 
End Sub
So all you need to do after that is check like this:
B4X:
If CheckConnection=True Then

end if

The whole code is not mine. I just assembled it from parts from other members. Kudos to them.


Hi, can you show us where the 's' is declared?

Thanks a lot
 
Upvote 0
Top