Test internet connection reliably?

Just starting out? Need help? Post your questions and find answers here.
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Test internet connection reliably?

Post by UserOfPure »

Hi, what is the best way to reliably check if an internet connection is available?

I was using this, but found out today that it returns 1 even if no connection is established:

Code: Select all

online=InternetGetConnectedState_(0,0)
It should return 0 if no connection is established, but it wasn't. My ISP had an outage and even though my router was on, I couldn't connect to any web sites or email or anything. But the code above returned 1 anyway, which is wrong, and I suspect due to my router being powered on.

I'm guessing I should just try to connect to a big web site (Google?) or such, and see if it can be reached?
Last edited by UserOfPure on Wed Aug 31, 2011 1:53 am, edited 2 times in total.
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Re: Test internet connection reliably?

Post by Baldrick »

Just a guess UserOfPure, but I would say what you are seeing is your own lan address that is active, which will be the case regardless of isp status if you are going through a router using NAT.
Maybe you could try IcmpSendEcho_() to ping the dns server supplied by your isp which will normally be a static address.

I guess this depends on exactly what it is you are wishing to achieve. e.g. Something just for yourself or some code you are writing which you wish to distribute which would mean you would need to gain information on dns server addresses, etc through your code to make this approach work.
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Re: Test internet connection reliably?

Post by TerryHough »

Do a search of the forum for InternetGetConnectedState

There are several useful tips.
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Test internet connection reliably?

Post by UserOfPure »

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.
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 »

InternetGetConnectedState will return true even if your connected to a router or your interface as long as something on the other side of the router or interface is alive, or announces its presence, even if not actually connected to the actual internet.

I've resorted to using a ping to someplace like yahoo. If it returns sucessfully then your connected to the internet.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
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 »

This should be effective in practical use. It pings google.com and waits one second for reply. If no reply in that time it returns 0. It's based on the Platform SDK code sample for IcmpSendEcho:

Code: Select all

Procedure TestInetConnection()
  SendData$ = "Test"
  ReplyBuffer$ = Space(SizeOf(ICMP_ECHO_REPLY) + Len(SendData$) + SizeOf(character))
  hIcmpFile = IcmpCreateFile_()
  dwRetVal = IcmpSendEcho_(hIcmpFile, inet_addr_("209.85.225.105"), @SendData$, Len(SendData$), #Null, @ReplyBuffer$, Len(ReplyBuffer$) + SizeOf(ICMP_ECHO_REPLY), 1000)

  If dwRetVal
    ProcedureReturn 1
  Else 
    ProcedureReturn 0
  EndIf
  
EndProcedure

Debug TestInetConnection()
BERESHEIT
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Test internet connection reliably?

Post by UserOfPure »

Thanks netmaestro; works great. A question: will the address of 209.85.225.105 always exist? Will my app need a way for the user to update it, in case it needs changing? I assume I should allow for that.
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 »

UserOfPure wrote:Thanks netmaestro; works great. A question: will the address of 209.85.225.105 always exist? Will my app need a way for the user to update it, in case it needs changing? I assume I should allow for that.
209.85.225.105 is www.google.com. I'm pretty sure it will be there for a while, they better cause i own some of their stock :)
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Test internet connection reliably?

Post by UserOfPure »

And if someone views my exe as a hex dump and sees 209.85.225.105 in there, won't they panic and start calling it spyware? I guess I should encrypt it.
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 »

UserOfPure wrote:And if someone views my exe as a hex dump and sees 209.85.225.105 in there, won't they panic and start calling it spyware? I guess I should encrypt it.
Use the following instead :

Code: Select all

  dwRetVal = IcmpSendEcho_(hIcmpFile, 1776375249, @SendData$, Len(SendData$), #Null, @ReplyBuffer$, Len(ReplyBuffer$) + SizeOf(ICMP_ECHO_REPLY), 1000)
Nice example Netmaestro.
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 »

SFSxOI wrote:
UserOfPure wrote:Thanks netmaestro; works great. A question: will the address of 209.85.225.105 always exist? Will my app need a way for the user to update it, in case it needs changing? I assume I should allow for that.
209.85.225.105 is http://www.google.com. I'm pretty sure it will be there for a while, they better cause i own some of their stock :)
Google will exist, but they can change IP addresses easily enough. Not that they necessarily would, but I'm sure they already have quite a few they use.
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 »

Somewhat simpler, native and goes by the name (which won't go away- the only one more solid might be www.nxsoftware.com):

Code: Select all

InitNetwork()

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

Debug TestInternetConnection()
BERESHEIT
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Test internet connection reliably?

Post by Thorium »

UserOfPure wrote: It should return 0 if no connection is established, but it wasn't.
No it shouldnt.
Just read the description of the function: http://msdn.microsoft.com/en-us/library/aa384702.aspx
A return value of TRUE indicates that either the modem connection is active, or a LAN connection is active and a proxy is properly configured for the LAN. A return value of FALSE indicates that neither the modem nor the LAN is connected. If FALSE is returned, the INTERNET_CONNECTION_CONFIGURED flag may be set to indicate that autodial is configured to "always dial" but is not currently active. If autodial is not configured, the function returns FALSE.
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Re: Test internet connection reliably?

Post by Baldrick »

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.....
I am not able to check atm whether using your owns dns would work properly or not as I am not at home & currently using a wireless broadband modem which is direct connected to my notebook without any router. But apart from the difficulty of an end user finding their own dns address, is there any real reason why you would not use this dns ping idea I suggested in my 1st post?
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 »

When I ping my dns server I always get a timeout.
I may look like a mule, but I'm not a complete ass.
Post Reply