Determining if a client PC has an Internet connection

C

Cliff Wells

Hi,

I'm writing an application that needs to know if an Internet connection
is available. Basically, I want to have something similar to what a lot
of email clients have, where the app can work either in "online" or
"offline" mode (it keeps a cache of downloaded info, so it can work
without a connection if needed).

The basic problem is this: it downloads info (RSS feeds) from a variety
of sources. Any one (or more) of these could conceivably fail to
download, so simply waiting for a timeout isn't sufficient (not easy to
differentiate between having a bad server and the client not having a
connection). Further, if it waits (say 30s) for the timeout to occur,
this is going to be a bit annoying to the user.

I've considered trying to connect to a stable host (i.e. Google) and if
that fails then assume the client can't connect to the internet, but
this seems like a sub-optimal solution.

Is there any way to reliably determine the state of the client's
internet connectivity?

Regards,
Cliff
 
J

Jarek Zgoda

Cliff Wells said:
I'm writing an application that needs to know if an Internet connection
is available. Basically, I want to have something similar to what a lot
of email clients have, where the app can work either in "online" or
"offline" mode (it keeps a cache of downloaded info, so it can work
without a connection if needed).

The basic problem is this: it downloads info (RSS feeds) from a variety
of sources. Any one (or more) of these could conceivably fail to
download, so simply waiting for a timeout isn't sufficient (not easy to
differentiate between having a bad server and the client not having a
connection). Further, if it waits (say 30s) for the timeout to occur,
this is going to be a bit annoying to the user.

I've considered trying to connect to a stable host (i.e. Google) and if
that fails then assume the client can't connect to the internet, but
this seems like a sub-optimal solution.

Is there any way to reliably determine the state of the client's
internet connectivity?

No, there's no such way. You can try to connect to some "usually
working & available" host, but there always may be some periods, when
this host is not available (scheduled downtime, power outage, you name
it). The hosts known for its greatest stability are root DNS servers.
 
T

Thorsten Kampe

* Jarek Zgoda (2004-09-19 10:14 +0200)
No, there's no such way.

I think there has to be (or at least there is a method that many
coders use because "testing if you're online" is a very frequent
problem).

Thorsten
 
J

Jarek Zgoda

Thorsten Kampe said:
I think there has to be (or at least there is a method that many
coders use because "testing if you're online" is a very frequent
problem).

There has to be, but isn't. I often see this question on many forums
with single answer. Let me cite ICS FAQ:

"""
There is no perfect solution. Why? Because the Internet is just another
network. Your computer does not distinguish between LAN and Internet.
They are both networks. One of them just happens to be very big.

The Internet is no different to your local network. It is just a matter
of size.

Think about your question. "Am I connected to the Internet?". Try
rephrasing your question to what you really want to ask. "Can I connect
to a specific remote host?" With the question worded like this, you can
start to tackle the problem. How do you find out if you can connect to
the remote host? Try to connect. Simple as that. If the connection
attempt fails, there is no path between you and the remote host or the
remote host is refusing connections.

But, I hear you cry, I do not want the auto-dial the pop up. How do we
get around this? Ask the user how to make a connection. We see this all
the time in other Internet applications. Outlook Express, WinAmp, etc.
They ask the user what sort of network connection they have. From this
information, the applications can make the best choice about how to
connect to a remote host.

There is no proof way. However some things can help.

There is an InternetGetConnectedState function in the WinINet.DLL that
returns true if it detects a connection to the internet. However, the
only thing you can be sure about when using this funtion is that the
computer will not start dialing and not pop up any dialog box when the
function returned true and you afterwards try to do anything on the
internet. In some cases the function may return false although the
computer is connected.
"""

Although it is Win32-only.
 
C

Chris S.

Cliff said:
Hi,

I'm writing an application that needs to know if an Internet connection
is available. Basically, I want to have something similar to what a lot
of email clients have, where the app can work either in "online" or
"offline" mode (it keeps a cache of downloaded info, so it can work
without a connection if needed).

The basic problem is this: it downloads info (RSS feeds) from a variety
of sources. Any one (or more) of these could conceivably fail to
download, so simply waiting for a timeout isn't sufficient (not easy to
differentiate between having a bad server and the client not having a
connection). Further, if it waits (say 30s) for the timeout to occur,
this is going to be a bit annoying to the user.

I've considered trying to connect to a stable host (i.e. Google) and if
that fails then assume the client can't connect to the internet, but
this seems like a sub-optimal solution.

Is there any way to reliably determine the state of the client's
internet connectivity?

Regards,
Cliff

As others have mentioned, there's no clean-cut paradigm in the language
that can distinguish between the Internet and your local LAN. Your best
bet is to simple go about your business can try accessing hosts like
normal. When several of your requests timeout, then you can safely
assume the local host to be offline. That's what you see in those
applications when they say you're "offline"; an educated guess. After
all, if you can't reach your target, then you are, for all intents and
purposes, offline.
 
C

Cliff Wells

As others have mentioned, there's no clean-cut paradigm in the language
that can distinguish between the Internet and your local LAN. Your best
bet is to simple go about your business can try accessing hosts like
normal. When several of your requests timeout, then you can safely
assume the local host to be offline. That's what you see in those
applications when they say you're "offline"; an educated guess. After
all, if you can't reach your target, then you are, for all intents and
purposes, offline.

Okay, at this point I suppose the better question would be how to make
an "educated guess". As I mentioned, I've considered trying to open a
socket to one or more relatively stable hosts (i.e. Google, Yahoo, etc)
and if this fails, assume there is no Internet link. However, this
clearly has it's flaws (those services, as unlikely as it seems, could
potentially be down, or they might be blocked by a corporate firewall,
etc). Further, tying the application to an outside source that is, for
all intents, unrelated to the app seems a bit flaky.

Anyway, it occurs to me that a better "guess" might consist of whether
or not the app can reach the PC's primary/secondary DNS servers. If
these are unreachable then it's a fair assumption that we aren't going
to get anywhere anyhow.

So... is there a way of determining the DNS search path of a PC?

Regards,
Cliff
 
?

=?Windows-1252?Q?Michel_Claveau_-_abstraction_m=E9

Hi !

Not only Internet, but with good suspicion : test if IP-passerelle is
present and answer to ping.

Then Ping one or two IP "externals and stables".
 
C

Cliff Wells

Hi !

Not only Internet, but with good suspicion : test if IP-passerelle is
present and answer to ping.

Sorry, you lost me here: passerelle == bridge?
Then Ping one or two IP "externals and stables".

Again, I don't follow.

Regards,
Cliff
 
T

Tuure Laurinolli

Cliff said:
I'm writing an application that needs to know if an Internet connection
is available. Basically, I want to have something similar to what a lot
of email clients have, where the app can work either in "online" or
"offline" mode (it keeps a cache of downloaded info, so it can work
without a connection if needed).

I do not see why there would have to be specific online and offline
modes. They are annoying. Especially when they educatedly guess they are
not on the Internet and refuse to believe the user who says they are.
The basic problem is this: it downloads info (RSS feeds) from a variety
of sources. Any one (or more) of these could conceivably fail to
download, so simply waiting for a timeout isn't sufficient (not easy to
differentiate between having a bad server and the client not having a
connection). Further, if it waits (say 30s) for the timeout to occur,
this is going to be a bit annoying to the user.

So why not assume you are always connected to the Internet. Download
updated info into local cache whenever possible and always show local
cached information. Of course you could somehow indicate the age of the
cached information, so the user doesn't think that local cached
information is up-to-date when it actually isn't.
 
C

Cameron Laird

On Sun, 2004-09-19 at 09:03 +0000, Chris S. wrote: .
.
.
So... is there a way of determining the DNS search path of a PC?
.
.
.
This, like everything else in the current thread, will end up
being more heuristic than definitive; worse, it's OS-specific.
I hope the comp.protocols* experts can answer more precisely
than I.
 
C

Cliff Wells

I do not see why there would have to be specific online and offline
modes. They are annoying. Especially when they educatedly guess they are
not on the Internet and refuse to believe the user who says they are.

It doesn't refuse. If you tell it that it's online it will faithfully
try to download whether it thinks it is or not. You are making as many
assumptions as you claim my software will ;)
So why not assume you are always connected to the Internet. Download
updated info into local cache whenever possible and always show local
cached information. Of course you could somehow indicate the age of the
cached information, so the user doesn't think that local cached
information is up-to-date when it actually isn't.

Because it's a customer requirement. Besides, as I outlined in a
separate post, there are actually a couple of good reasons for the app
to know when it's offline:

'''
More importantly, if the app is "online", timing out is returned as an
error to the user, whereas in the "offline" state, as long as the data
is cached, there is no error. Also, in the case of being offline and
there is no data in the cache, the error message is to be different than
if the app is online and the connection fails.
'''

Regards,
Cliff
 
C

Chris S.

Cliff said:
Okay, at this point I suppose the better question would be how to make
an "educated guess". As I mentioned, I've considered trying to open a
socket to one or more relatively stable hosts (i.e. Google, Yahoo, etc)
and if this fails, assume there is no Internet link. However, this
clearly has it's flaws (those services, as unlikely as it seems, could
potentially be down, or they might be blocked by a corporate firewall,
etc). Further, tying the application to an outside source that is, for
all intents, unrelated to the app seems a bit flaky.
>
Anyway, it occurs to me that a better "guess" might consist of whether
or not the app can reach the PC's primary/secondary DNS servers. If
these are unreachable then it's a fair assumption that we aren't going
to get anywhere anyhow.

So... is there a way of determining the DNS search path of a PC?

My solution was far more mundane. I meant simply try and access the
internet the way you normally would. If it's a mail client, try and
access your users POP server. If it's a web-browser, try and retrieve a
web page the user wants. If you can't access them, then there's
essentially no "internet connection". In other words, don't worry about
whether or not there's an available connection. Instead, assume there is
a connection and worry about the status of your requests on the network.
 
C

Cliff Wells

My solution was far more mundane. I meant simply try and access the
internet the way you normally would. If it's a mail client, try and
access your users POP server. If it's a web-browser, try and retrieve a
web page the user wants. If you can't access them, then there's
essentially no "internet connection". In other words, don't worry about
whether or not there's an available connection. Instead, assume there is
a connection and worry about the status of your requests on the network.

Well, this is the fundamental problem. It's an RSS aggregator, so there
is no single server it will be connecting to. Further, because servers
can be added by the user or simply "disappear" because some blogger
decides he doesn't want to maintain a feed anymore, getting connection
errors will probably be quite common.

Additionally, because I'm using Twisted, fetching the sites is
asynchronous, so I'm updating the cache and determining error messages
long before I have a chance to determine that all of the connections are
failing. By the time I have that information, the damage has been done,
so to speak.

Given this situation, and the fact that there are specific requirements
(mentioned elsewhere) leaves me little choice but to use some guesswork
to determine whether or not a connection is present *prior* to fetching
feeds.

Anyway, at this point I'm using the rather lame "check if both google
and yahoo are unavailable" method. I'll find something better when I
have more time to devote to it. I may use the effbot's suggestion for
Windows (InternetCheckConnection) and some other method for Linux and
OS/X.

Regards,
Cliff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top