Test internet connection reliably?

Just starting out? Need help? Post your questions and find answers here.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Test internet connection reliably?

Post by SFSxOI »

srod wrote:When I ping my dns server I always get a timeout.
but www.nxsoftware.com pings OK from here though:

Code: Select all

C:\Windows\system32>ping www.nxsoftware.com

Pinging server40.ukservers.net [217.10.138.240] with 32 bytes of data:
Reply from 217.10.138.240: bytes=32 time=125ms TTL=49
Reply from 217.10.138.240: bytes=32 time=125ms TTL=49
Reply from 217.10.138.240: bytes=32 time=124ms TTL=49
Reply from 217.10.138.240: bytes=32 time=124ms TTL=49

Ping statistics for 217.10.138.240:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 124ms, Maximum = 125ms, Average = 124ms
I get a timeout when I ping my DNS server also, some are just set up not to respond.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Re: Test internet connection reliably?

Post by Baldrick »

srod wrote:When I ping my dns server I always get a timeout.
That answers that then. :)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Test internet connection reliably?

Post by srod »

Nice idea though. :)
I may look like a mule, but I'm not a complete ass.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Test internet connection reliably?

Post by rsts »

UserOfPure wrote:It's for something I wish to distribute. Basically, my app loads a web page in a webgadget but then waits for the page to load in it, but goes into a never-ending loop and waits forever, because no connection is available. I never would have known this could occur if my ISP hadn't been down, so it was kind of a blessing that they did, as it highlighted this problem for me before distribution. :) So now I need to find a better way to determine if web sites can be accessed.
If it loads a web page which may be down, how is checking google going to help you if your ISP is down? You'll still have to wait for the time-out, thus it will still exhibit the condition you experienced earlier. I do the same thing with my program to see if my on-line help function is available for the users. BTW, it is not a never ending loop when you can't reach the site, it's a connection timeout which, while it can take quite a while, does always end eventually.

To check a url you can also use InternetCheckConnection_ - this is what I am using.

cheers
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Test internet connection reliably?

Post by UserOfPure »

Baldrick wrote:is there any real reason why you would not use this dns ping idea I suggested in my 1st post?
I am using your idea now, using netmaestro's code. See my comment above to him? ;)

@Thorium: The API function you linked to will return TRUE or FALSE, which is 1 or 0 in PureBasic. So, I don't get your point? It returns 1 for me when no internet access is available, which is wrong. It's returning 1 simply because my router is on, but has not handshaked anything and has not gotten a line to the internet.

Can someone show how to use the InternetCheckConnection API call to test a connection?
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Re: Test internet connection reliably?

Post by Baldrick »

Unfortunately UserOfPure, my idea relies on the end users isp not blocking ping requests, which apparently many do. So my idea of using your own isp allocated dns address is buggered. :(
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Test internet connection reliably?

Post by netmaestro »

Can someone show how to use the InternetCheckConnection API call to test a connection?
This is returning 1 with my computer plugged into the router, 0 when it's not:

Code: Select all

Debug InternetCheckConnection_("http://www.google.com", 1, 0 )
BERESHEIT
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Test internet connection reliably?

Post by UserOfPure »

Baldrick wrote:I would seriously question the idea of 'hardcoding' anything such as "google.com" into my app for fear of them coming at me with outstretched hand claiming things such as Royalties for the use of their website in a hardaceded fashion
All you need to do is have the user specify a web address to use as the ping target, and store that address as a user preference; your app is then free of any liability.

@netmaestro: Thanks for that, works here like that too. Next time my ISP goes down (which is often enough) I will test all solutions posted in this thread to see which ones worked correctly. :)
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Re: Test internet connection reliably?

Post by Baldrick »

UserOfPure wrote: @netmaestro: Thanks for that, works here like that too. Next time my ISP goes down (which is often enough) I will test all solutions posted in this thread to see which ones worked correctly. :)
Assuming you are using a physical landline connection you could just unplug that from your router WAN connection to take your public IP & carrier signal out without losing your LAN connection which should up to a point replicate an isp fault. :)

i.e. you will still be able to access any other computers / devices on your router that are inside the LAN boundary, but anything going through the NAT will fail
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Test internet connection reliably?

Post by UserOfPure »

Baldrick wrote:Assuming you are using a physical landline connection you could just unplug that from your router WAN connection to take your public IP & carrier signal out without losing your LAN connection which should up to a point replicate an isp fault. :)
Okay, I just unplugged my phone line from my router and tested the following code snippets to see which would correctly report no internet connection. Interestingly, only the last one, the InternetCheckConnection API call, was correct in the shortest time period (and smallest in size to code), but it took about 15 seconds to determine it. That's far too long to be practical, but since it's the only correct reliable method, I'll have to use it. :( I wonder if a shorter timeout can be set for it somehow?

Code: Select all

Procedure Online()
  d$="test" : b$=Space(SizeOf(ICMP_ECHO_REPLY)+Len(d$)+SizeOf(character)) : hIcmpFile=IcmpCreateFile_()
  ProcedureReturn IcmpSendEcho_(hIcmpFile,1776375249,@d$,Len(d$),0,@b$,Len(b$)+SizeOf(ICMP_ECHO_REPLY),1000)
EndProcedure

Procedure TestInternetConnection()
  connection=OpenNetworkConnection("www.google.com", 80)
  If connection
    CloseNetworkConnection(connection)
    ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf
EndProcedure

InitNetwork()

connectionName$=Space(256)
tf=InternetGetConnectedStateEx_(@lpdwFlags.l,@connectionName$,256,0)
If tf
  conn$=connectionName$
  If lpdwFlags & #INTERNET_CONNECTION_CONFIGURED : conn$+", valid connection but might/might not be currently connected" : EndIf
  If lpdwFlags & #INTERNET_CONNECTION_OFFLINE : conn$+", is in offline mode" : EndIf
  If lpdwFlags & #INTERNET_RAS_INSTALLED : conn$+", RAS installed" : EndIf
  If lpdwFlags & #INTERNET_CONNECTION_PROXY : conn$+", uses a proxy server" : EndIf
  If lpdwFlags & #INTERNET_CONNECTION_LAN : conn$+", uses a local area network" : EndIf
  If lpdwFlags & #INTERNET_CONNECTION_MODEM : conn$+", uses a modem" : EndIf
Else
  conn$="No internet connection detected"
EndIf

Debug conn$ ; Returns "LAN Connection, RAS installed, uses a local area network".

Debug InternetGetConnectedState_(0,0) ; Returns 1 (wrong!).

Debug Online() ; Returns 1 (wrong!).

Debug TestInternetConnection() ; Returns 0 after 20 seconds.

Debug InternetCheckConnection_("http://www.google.com",1,0) ; Returns 0 after 15 seconds -- THE WINNER! :)
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Test internet connection reliably?

Post by UserOfPure »

Okay, I just came up with the ultimate solution which is 100% reliable in 3 seconds or less. :) Observe:

Code: Select all

Procedure Online(url$="www.google.com")

  If InternetGetConnectedState_(0,0)=1 ; Only check further if modem is switched on.
    p=RunProgram("ping.exe",url$,"",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
    If p : While ProgramRunning(p) : o$+ReadProgramString(p) : Wend : CloseProgram(p) : EndIf
    If o$<>"" And FindString(o$,"unreachable",1)=0 And FindString(o$,"could not find host",1)=0 : status=1 : EndIf
  EndIf

  ProcedureReturn status

EndProcedure

Debug Online()
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: Test internet connection reliably? [Resolved]

Post by Rescator »

My advise is to use InternetCheckConnection and check the site that the webpage in the webgadget is served from, it's silly to test google.com if pages are to be served from example.com

PS! Personally I'd just skip connectivity check, since the webgadget uses the system, if it fails to connect a IE-like failed to connect error is shown i the webgadget if I recall correctly with possibly a help link and more.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: Test internet connection reliably? [Resolved]

Post by Rescator »

Yup! On Windows 7 I get:
This program cannot display the webpage

Most likely causes:
You are not connected to the Internet.
The website is encountering problems.
There might be a typing error in the address.

What you can try:
Check your Internet connection. Try visiting another website to make sure you are connected.

Retype the address.

Go back to the previous page.

More information

This problem can be caused by a variety of issues, including:

Internet connectivity has been lost.
The website is temporarily unavailable.
The Domain Name Server (DNS) is not reachable.
The Domain Name Server (DNS) does not have a listing for the website's domain.
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Test internet connection reliably? [Resolved]

Post by UserOfPure »

InternetCheckConnection takes 15 seconds to realize that there's no connection. Too long.

As for the WebGadget, yes, it'll return an error, but I don't want that error. I want to display something else if no connection is made. So you see my problem and why I need to test for a valid connection.
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Test internet connection reliably?

Post by Thorium »

UserOfPure wrote: @Thorium: The API function you linked to will return TRUE or FALSE, which is 1 or 0 in PureBasic. So, I don't get your point? It returns 1 for me when no internet access is available, which is wrong. It's returning 1 simply because my router is on, but has not handshaked anything and has not gotten a line to the internet.
My point is that you don't read the description. It returns TRUE and thats correct. If you read the description you will find out that it returns TRUE if a modem is configured, if a LAN connection is available or if a proxy is configured, no matter if there is a internet connection.
Post Reply