Page 1 of 4
Test internet connection reliably?
Posted: Sat Feb 13, 2010 2:36 pm
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?
Re: Test internet connection reliably?
Posted: Sat Feb 13, 2010 3:16 pm
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.
Re: Test internet connection reliably?
Posted: Sat Feb 13, 2010 4:05 pm
by TerryHough
Do a search of the forum for InternetGetConnectedState
There are several useful tips.
Re: Test internet connection reliably?
Posted: Sat Feb 13, 2010 4:10 pm
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.
Re: Test internet connection reliably?
Posted: Sat Feb 13, 2010 5:13 pm
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.
Re: Test internet connection reliably?
Posted: Sat Feb 13, 2010 6:30 pm
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()
Re: Test internet connection reliably?
Posted: Sat Feb 13, 2010 11:56 pm
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.
Re: Test internet connection reliably?
Posted: Sun Feb 14, 2010 12:21 am
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

Re: Test internet connection reliably?
Posted: Sun Feb 14, 2010 12:26 am
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.
Re: Test internet connection reliably?
Posted: Sun Feb 14, 2010 12:57 am
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.
Re: Test internet connection reliably?
Posted: Sun Feb 14, 2010 1:13 am
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.
Re: Test internet connection reliably?
Posted: Sun Feb 14, 2010 1:26 am
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()
Re: Test internet connection reliably?
Posted: Sun Feb 14, 2010 1:38 am
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.
Re: Test internet connection reliably?
Posted: Sun Feb 14, 2010 1:52 am
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?
Re: Test internet connection reliably?
Posted: Sun Feb 14, 2010 2:08 am
by srod
When I ping my dns server I always get a timeout.