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
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!
If you can not seem to express myself with words "not taught", it's because I use the google translator.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.
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
If CheckConnection=True Then
end if
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 ?Here's a snippet I use in my apps which seems to work fine:
So all you need to do after that is check like this: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
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 ?
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.As I wrote, it works in "my apps".
Here's a snippet I use in my apps which seems to work fine:
So all you need to do after that is check like this: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
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.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?